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:

How to Store Duplicate Keys in a Map in Java?
Java Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm
Java Program to Implement JobStateReasons API
A Custom Data Binder in Spring MVC
Java Program to Implement Tarjan Algorithm
Spring – Injecting Collections
Template Engines for Spring
Java Program to Implement Uniform-Cost Search
Tổng quan về ngôn ngữ lập trình java
Java Program to Check whether Graph is Biconnected
Chuyển đổi giữa các kiểu dữ liệu trong Java
Java Program to Implement Euclid GCD Algorithm
Get and Post Lists of Objects with RestTemplate
Java Program to Implement the Bin Packing Algorithm
Setting the Java Version in Maven
Java Program to Implement Cubic convergence 1/pi Algorithm
Java – Write an InputStream to a File
Removing all Nulls from a List in Java
Java Program to Check whether Graph is a Bipartite using 2 Color Algorithm
Java Program to Perform String Matching Using String Library
Java Program to Implement Disjoint Sets
Tạo ứng dụng Java RESTful Client với thư viện Retrofit
Hướng dẫn Java Design Pattern – Strategy
Spring Cloud Connectors and Heroku
Concrete Class in Java
Spring’s RequestBody and ResponseBody Annotations
Java Program to Implement the Schonhage-Strassen Algorithm for Multiplication of Two Numbers
Introduction to Spring Cloud Stream
Java Program to Find Basis and Dimension of a Matrix
Automatic Property Expansion with Spring Boot
Cachable Static Assets with Spring MVC
Java Program to Check Whether an Undirected Graph Contains a Eulerian Path