Quantcast
Channel: Go4Expert
Viewing all articles
Browse latest Browse all 1987

Algorithms

$
0
0
Analyzing the efficiency of algorithms

1 1 1 1 1
e = --- + --- + --- + --- + --- + ...
0! 1! 2! 3! 4!

Code:
public static void e1(int n) {
   double sum = 0.0;
   for (int i = 0; i < n; i++) {
      sum = sum + 1.0 / factorial(i);
   }
   System.out.println("e is approximately " + sum);
}

public static int factorial(int k) {
   int product = 1;
   for (int i = 1; i <= k; i++) {
      product = product * i;
   }
   return product;...
Algorithms

Viewing all articles
Browse latest Browse all 1987

Trending Articles