Pages

Showing posts with label golden ratio. Show all posts
Showing posts with label golden ratio. Show all posts

Tuesday, September 8, 2009

Euler Problem 25 solution

Time (s): ~0.001
package margusmartseppcode.From_20_to_29;

public class Problem_25 {
 public static void main(String[] args) {
  // Binet's Fibonacci Number Formula
  double digits = 1000;
  double result = Math.ceil((digits - 1 + Math.log10(5) / 2)
    / Math.log10((1 + Math.sqrt(5)) / 2));
  System.out.println((int) result);
 }
}

Euler Problem 2 solution

Time (s): ~0.001
package margusmartseppcode.From_1_to_9;

public class Problem_2 {
 static final double gold_r = Math.pow((1 + Math.sqrt(5)) / 2, 3);

 public static void main(String[] args) {
  long size = 4000000, sum = 0;
  double f = 2;

  for (; f < size; f = Math.round(f * gold_r))
   sum += f;

  System.out.println(sum);
 }
}