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:
Lớp Collections trong Java (Collections Utility Class)
Spring Boot - Introduction
Java Program to Solve the Fractional Knapsack Problem
Java Program to Check whether Graph is a Bipartite using DFS
Spring Security OAuth Login with WebFlux
Guide to java.util.concurrent.BlockingQueue
HTTP Authentification and CGI/Servlet
Biến trong java
Guide to CopyOnWriteArrayList
Hướng dẫn Java Design Pattern – Composite
Java Program to Perform LU Decomposition of any Matrix
Java Program to subtract two large numbers using Linked Lists
Java Program to Check the Connectivity of Graph Using BFS
Hướng dẫn Java Design Pattern – Visitor
Java Program to Perform Insertion in a 2 Dimension K-D Tree
Java Program to Perform Preorder Recursive Traversal of a Given Binary Tree
Java Program to Find kth Smallest Element by the Method of Partitioning the Array
Java Program to do a Breadth First Search/Traversal on a graph non-recursively
Java Program to Check Whether an Undirected Graph Contains a Eulerian Cycle
Java Program to Check whether Graph is a Bipartite using 2 Color Algorithm
Java Program to Implement HashMap API
Limiting Query Results with JPA and Spring Data JPA
Spring 5 and Servlet 4 – The PushBuilder
Java Program to Implement Self Balancing Binary Search Tree
Pagination and Sorting using Spring Data JPA
How to Round a Number to N Decimal Places in Java
Java IO vs NIO
Java Program to Search Number Using Divide and Conquer with the Aid of Fibonacci Numbers
Java – Delete a File
Kiểu dữ liệu Ngày Giờ (Date Time) trong java
Java Program to Implement the Binary Counting Method to Generate Subsets of a Set
How to Count Duplicate Elements in Arraylist