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:

Java Program to Implement Adjacency Matrix
Updating your Password
Java Program to Perform Searching in a 2-Dimension K-D Tree
Spring Security Login Page with React
Reactive Flow with MongoDB, Kotlin, and Spring WebFlux
Java Program to Solve the Fractional Knapsack Problem
Serialize Only Fields that meet a Custom Criteria with Jackson
OAuth2 for a Spring REST API – Handle the Refresh Token in AngularJS
Spring Security – Reset Your Password
Display Auto-Configuration Report in Spring Boot
Add Multiple Items to an Java ArrayList
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
Java Program to do a Depth First Search/Traversal on a graph non-recursively
Java Program to Implement Word Wrap Problem
New in Spring Security OAuth2 – Verify Claims
Guide to BufferedReader
Feign – Tạo ứng dụng Java RESTful Client
Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Conne...
Java Program to Show the Duality Transformation of Line and Point
Guide to @ConfigurationProperties in Spring Boot
Spring Boot - Code Structure
Java Program to Implement Attribute API
Hướng dẫn sử dụng biểu thức chính quy (Regular Expression) trong Java
Guide to Selenium with JUnit / TestNG
Java Program to Perform String Matching Using String Library
Converting Between a List and a Set in Java
Xử lý ngoại lệ trong Java (Exception Handling)
Chuyển đổi từ HashMap sang ArrayList
Spring Security with Maven
ThreadPoolTaskExecutor corePoolSize vs. maxPoolSize
Java Program to Find the Longest Path in a DAG
Java Program to Implement Circular Doubly Linked List