Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java binary exponentiation

/**
* Calculate a^n in O(logn) time, instead of O(n) time with naive approach.
**/
public static long powerRecursive(int base, int exponent){
  if(exponent == 0) return 1;
  return (exponent % 2 == 0) ?
    powerRecursive(base, exponent / 2) * powerRecursive(base, exponent / 2)
    : base * powerRecursive(base, (exponent - 1) / 2) *
      powerRecursive(base, (exponent - 1) / 2);
}
Comment

PREVIOUS NEXT
Code Example
Java :: list remove duplicates java 
Java :: terminate function calling in java 
Java :: meaning of instantiated in java 
Java :: can we override the overloaded method in java 
Java :: how to check size of arrayin java 
Java :: implement queue using array in java 
Java :: zoneddatetime java 
Java :: java startswith regex 
Java :: default constructor 
Java :: is it possible to declare two conditions in for loop in javascrip 
Java :: how to declare a interface in java 
Java :: POST method) in spring rest api 
Java :: how to initialize a variable in java 
Java :: meter to cm 
Java :: package javafx.fxml does not exist 
Java :: classpath in java 
Java :: " meaning in java 
Java :: create fragment constructor in arrayadapter 
Java :: intellij error for new project 
Java :: how to declare a variable when using loops Java 
Java :: what is a callable in java 
Java :: android adb is using too much cpu 
Java :: Deque interface in Java 
Java :: java stream list order lambda 
Java :: format string precision double java 
Java :: jmonkey shapes 
Java :: difference between final and constant in java 
Java :: Java Labeled continue Statement Java 
Java :: change order of words in string java 
Java :: Creating strings using the new keyword Java 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =