Posts

Showing posts with the label Math

Java program for Tower of Hanoi

 class TowerOfHanoi {     // Time Complexity O(n) and Space Complexity O(1)     void tower_recursive(int numberOfDisks, int sourceTower, int additionalTower, int destinationTower) {         if(numberOfDisks > 0){             tower_recursive(numberOfDisks - 1, sourceTower, destinationTower, additionalTower);             System.out.println("Moving disk from tower " + sourceTower + " to " + destinationTower);             tower_recursive(numberOfDisks - 1, additionalTower, sourceTower, destinationTower);         }     } public static void main (String[] args) {     TowerOfHanoi obj = new TowerOfHanoi();     obj.tower_recursive(3, 1, 2, 3); } }

Java program for combination | nCr

Image
  class NCR {     // Time Complexity O(n) and Space Complexity O(1)     int nCr (int n, int r) {         // Formula n!/(r!*(n-r)!)         int factorialN = factorial(n);         int factorialR = factorial(r);         int factorialNMinusR = factorial(n-r);         return factorialN / (factorialR * factorialNMinusR);     }         // Time Complexity O(n) and Space Complexity O(n)     int nCr_pascalTriangle (int n, int r) {         if(r == 0 || n == r) {             return 1;         }         return nCr_pascalTriangle(n-1, r-1) + nCr_pascalTriangle(n-1, r);     }     // Time Complexity O(n) and Space Complexity O(1)     int factorial (int n) {         if(n <= 1) { //To return 1st ...

Java program for Fibonacci Series

 class FibonacciSeries {     int[] dp;     // Time Complexity O(2^n) and Space Complexity O(n)     int fibonacci_recursive (int n) {         if(n <= 1) { //To return 1st and 2nd term             return n;         }         return fibonacci_recursive(n-2) + fibonacci_recursive(n-1);     }     // Time Complexity O(n) and Space Complexity O(n)     int fibonacci_recursive_memoization (int n) {         if(n <= 1) { //To return 1st and 2nd term             return n;         }         if(dp[n-2] == -1) {             dp[n-2] = fibonacci_recursive(n-2);         }         if(dp[n-1] == -1) {             dp[n-1] = fibonacci_recursive(n-1);    ...

Java program for Taylor Series

Image
class TaylorSeries {     int powerOfX;     int factorialOfN;     double result;     // Time Complexity O(n) and Space Complexity O(n)     double taylor_recursive (int x, int n) {         if(x <= 0 || n < 0) { // Corner case             return -1;         }         if(n == 0) {              return 1;         }         double result = taylor_recursive(x, n - 1);         powerOfX *= x;         factorialOfN *= n;         return (double)powerOfX / factorialOfN + result;     }          // Time Complexity O(n) and Space Complexity O(1)     double taylor_iterative (int x, int n) {         if(x <= 0 || n < 0) { // Corner case       ...

Java program to calculate the power of a number | exponential (m^n)

 // Time Complexity O(n) and Space Complexity O(n) int pow_recursive (int m, int n) {     if(n == 0) {         return 1;     }     return pow_recursive(m, n - 1) * m; } // Time Complexity O(n/2) and Space Complexity O(n) int pow_recursive_improved(int m, int n) {     if(n < 0) {        return 0;     }     if(n == 0) {         return 1;     }     if(n % 2 == 0) { // Even         return pow_recursive_improved(m*m, n/2);     }     return m * pow_recursive(m*m, (n - 1) / 2); } // Time Complexity O(n) and Space Complexity O(1) int pow_iterative(int m, int n) {     if(n < 0) {         return 0;     }     if(n == 0) {         return 1;     }     int result = 1;     for(int i = 0; i < n; i++) {   ...

Write program to calculate factorial of a number

 0! = 1 1! = 1 //Time complexity O(n) and Space complexity O(n) int factorial_recursive(int n) {     if(n < 0) {          return 0;     }     if(n == 0) {         return 1;     }     return factorial_recursive(n - 1) * n; }    //Time complexity O(n) and Space complexity O(1) int factorial_iterative(int n) {     if(n < 0) {         return 0;     }     if(n == 0) {         return 1;     }     int result = 1;     for(int i = 1; i <= n; i++) {         result *= i;     }     return result; }

Sum of natural numbers

//Time complexity O(n) and Space complexity O(n) int sum_recursive(int n) {     if(n == 0) {         return 0;     }     return sum_recursive(n - 1) + n; } //Time complexity O(n) and Space complexity O(1) int sum_iterative(int n) {     int sum = 0;     for(int i = 1; i <= n; i++) {         sum += i;     }     return sum; }      //Time complexity O(1) and Space complexity O(1) int sum_usingFormula(int n) {     return n * (n + 1) / 2; }

Logarithm Properties

Image