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:

Getting the Size of an Iterable in Java
Java Program to Construct an Expression Tree for an Postfix Expression
Java Program to Find the Longest Subsequence Common to All Sequences in a Set of Sequences
Spring Cloud AWS – S3
Spring Data – CrudRepository save() Method
How to Use if/else Logic in Java 8 Streams
“Stream has already been operated upon or closed” Exception in Java
Generating Random Dates in Java
Period and Duration in Java
Spring @RequestParam Annotation
Java Program to Implement Variable length array
Phương thức forEach() trong java 8
Giới thiệu Java Service Provider Interface (SPI) – Tạo các ứng dụng Java dễ mở rộng
Java Program to Implement Binomial Tree
Autoboxing và Unboxing trong Java
Java Program to Perform Quick Sort on Large Number of Elements
Hướng dẫn Java Design Pattern – Chain of Responsibility
Lập trình đa luồng với Callable và Future trong Java
Java Program to Implement ScapeGoat Tree
Spring Data JPA Delete and Relationships
Java Program to Implement AttributeList API
Java Program to Implement Sorting of Less than 100 Numbers in O(n) Complexity
Java Program to Implement Binary Search Tree
Spring Security Login Page with React
Default Password Encoder in Spring Security 5
Java Program to Represent Graph Using Linked List
Derived Query Methods in Spring Data JPA Repositories
Object cloning trong java
Java Program to Perform Searching Based on Locality of Reference
Spring Boot - Logging
Java Program to Construct an Expression Tree for an Infix Expression
Java Program to Perform Cryptography Using Transposition Technique