Java Program to Implement Repeated Squaring Algorithm

This is a Java Program to Implement Repeated Squaring Algorithm. Repeated squaring algorithm is used to compute xn efficiently.

Here is the source code of the Java Program to Implement Repeated Squaring Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

/**
 ** Java Program to Implement Repeated Squaring Algorithm
 **/
 
import java.util.Scanner;
 
/** Class RepeatedSquaring **/
public class RepeatedSquaring
{
    /** Function for repeated squaring **/
    public double expBySquaring(double x, int n)
    {
        if (n < 0)
            return expBySquaring(1 / x, -n);
        else if (n == 0)
            return 1;
        else if (n == 1)
            return x;
        else if (n % 2 == 0)
            return expBySquaring(x * x, n / 2);
        else 
            return x * expBySquaring(x * x, (n - 1)/2);    
    }
    /** Main function **/
    public static void main (String[] args) 
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Repeated Squaring Algorithm Test\n");
        /** Make an object of RepeatedSquaring class **/
        RepeatedSquaring rs = new RepeatedSquaring();
 
        /** Accept n , k **/
        System.out.println("\nEnter n and k of (N ^ K)");
        double n = scan.nextDouble();
        int k = scan.nextInt();
        double result = rs.expBySquaring(n, k);
 
        System.out.println("\nResult : "+ result);        
    }
}

Output:

Repeated Squaring Algorithm Test
 
 
Enter n and k of (N ^ K)
3 19
 
Result : 1.162261467E9
 
 
Repeated Squaring Algorithm Test
 
 
Enter n and k of (N ^ K)
7 -4
 
Result : 4.1649312786339016E-4

Related posts:

OAuth2 for a Spring REST API – Handle the Refresh Token in Angular
Java Program to Implement Segment Tree
Java Program to Implement Euclid GCD Algorithm
Java Program to Implement the Program Used in grep/egrep/fgrep
Intro to the Jackson ObjectMapper
How to Manually Authenticate User with Spring Security
Java Program to Solve Travelling Salesman Problem for Unweighted Graph
Java Program to Implement Pollard Rho Algorithm
Hướng dẫn Java Design Pattern – Abstract Factory
Hamcrest Collections Cookbook
Java Program to Implement Lloyd’s Algorithm
RegEx for matching Date Pattern in Java
Hướng dẫn sử dụng Java String, StringBuffer và StringBuilder
Validate email address exists or not by Java Code
Convert Time to Milliseconds in Java
Một số nguyên tắc, định luật trong lập trình
Biểu thức Lambda trong Java 8 – Lambda Expressions
A Guide to TreeSet in Java
Java Program to Implement the Vigenere Cypher
Java Program to Implement Jarvis Algorithm
Prevent Brute Force Authentication Attempts with Spring Security
Java Program to find the maximum subarray sum using Binary Search approach
Java Program to Implement Min Heap
How to Iterate Over a Stream With Indices
Registration – Activate a New Account by Email
Java Program to Compute the Volume of a Tetrahedron Using Determinants
Java Program to Implement Quick Hull Algorithm to Find Convex Hull
Working with Network Interfaces in Java
A Guide to Queries in Spring Data MongoDB
Wiring in Spring: @Autowired, @Resource and @Inject
Bootstrapping Hibernate 5 with Spring
Java Program to Implement Segment Tree