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:

REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
Java Program to Implement Hash Tables with Double Hashing
Performance Difference Between save() and saveAll() in Spring Data
Quick Guide to Spring Controllers
Iterating over Enum Values in Java
Loại bỏ các phần tử trùng trong một ArrayList như thế nào?
Java Program to Implement DelayQueue API
Java Web Services – Jersey JAX-RS – REST và sử dụng REST API testing tools với Postman
Marker Interface trong Java
Java Program to Generate Random Hexadecimal Byte
Java Program to Implement Aho-Corasick Algorithm for String Matching
Java Program to Implement Solovay Strassen Primality Test Algorithm
Từ khóa static và final trong java
A Quick Guide to Spring Cloud Consul
Lập trình đa luồng với CompletableFuture trong Java 8
Introduction to Spring Boot CLI
Apache Commons Collections Bag
Vấn đề Nhà sản xuất (Producer) – Người tiêu dùng (Consumer) và đồng bộ hóa các luồng trong Java
Lớp Properties trong java
Introduction to Spring Cloud OpenFeign
Java Program to Implement the Binary Counting Method to Generate Subsets of a Set
Spring Boot - Internationalization
Biến trong java
Convert Hex to ASCII in Java
Java Program to Compute Discrete Fourier Transform Using Naive Approach
Java Program to Find the Connected Components of an UnDirected Graph
How to Read HTTP Headers in Spring REST Controllers
Ways to Iterate Over a List in Java
Java Program to Implement the MD5 Algorithm
Java Program to Implement IdentityHashMap API
Cơ chế Upcasting và Downcasting trong java
Java Program to Delete a Particular Node in a Tree Without Using Recursion