Skip to main content
Modified both original and new program to measure the end time before the call to println(), to be more precise.
Source Link
JS1
  • 28.6k
  • 3
  • 41
  • 83

Is this the most efficient way of doing it?

Sometimes the simplest solution is also the most efficient solution. I'm not sure why you decided to use Math.pow and PHI to compute something that can be easily (and more quickly) be computed with simple addition:

public class EvenFibonacciFinder {
    private static final int MAX_NUM = 4_000_000;

    public static void main(String[] args)
    {
        long time = System.nanoTime();
        int  f1   = 1;
        int  f2   = 2;
        long sum  = 0;

        while (f2 <= MAX_NUM) {
            int f3;
            sum += f2;
            // This skips three ahead in the sequence.
            f3 = f1 + f2;
            f1 = f2 + f3;
            f2 = f1 + f3;
        }
        long end = System.nanoTime();
        System.out.println("Result: " + sum +
                "\nTime used for calculation in nanoseconds: " +
                (System.nanoTime()end - time));
    }
}

Output:

Original code using pow:
Result: 4613732
Time used for calculation in nanoseconds: 2966949952

Using addition instead of pow:
Result: 4613732
Time used for calculation in nanoseconds: 45406934

(Note: both programs were modified to measure the end time before the println(), otherwise some of the string concatentation time was being counted).

Is this the most efficient way of doing it?

Sometimes the simplest solution is also the most efficient solution. I'm not sure why you decided to use Math.pow and PHI to compute something that can be easily (and more quickly) be computed with simple addition:

public class EvenFibonacciFinder {
    private static final int MAX_NUM = 4_000_000;

    public static void main(String[] args)
    {
        long time = System.nanoTime();
        int  f1   = 1;
        int  f2   = 2;
        long sum  = 0;

        while (f2 <= MAX_NUM) {
            int f3;
            sum += f2;
            // This skips three ahead in the sequence.
            f3 = f1 + f2;
            f1 = f2 + f3;
            f2 = f1 + f3;
        }
        System.out.println("Result: " + sum +
                "\nTime used for calculation in nanoseconds: " +
                (System.nanoTime() - time));
    }
}

Output:

Original code using pow:
Result: 4613732
Time used for calculation in nanoseconds: 296694

Using addition instead of pow:
Result: 4613732
Time used for calculation in nanoseconds: 45406

Is this the most efficient way of doing it?

Sometimes the simplest solution is also the most efficient solution. I'm not sure why you decided to use Math.pow and PHI to compute something that can be easily (and more quickly) be computed with simple addition:

public class EvenFibonacciFinder {
    private static final int MAX_NUM = 4_000_000;

    public static void main(String[] args)
    {
        long time = System.nanoTime();
        int  f1   = 1;
        int  f2   = 2;
        long sum  = 0;

        while (f2 <= MAX_NUM) {
            int f3;
            sum += f2;
            // This skips three ahead in the sequence.
            f3 = f1 + f2;
            f1 = f2 + f3;
            f2 = f1 + f3;
        }
        long end = System.nanoTime();
        System.out.println("Result: " + sum +
                "\nTime used for calculation in nanoseconds: " +
                (end - time));
    }
}

Output:

Original code using pow:
Result: 4613732
Time used for calculation in nanoseconds: 9952

Using addition instead of pow:
Result: 4613732
Time used for calculation in nanoseconds: 934

(Note: both programs were modified to measure the end time before the println(), otherwise some of the string concatentation time was being counted).

Source Link
JS1
  • 28.6k
  • 3
  • 41
  • 83

Is this the most efficient way of doing it?

Sometimes the simplest solution is also the most efficient solution. I'm not sure why you decided to use Math.pow and PHI to compute something that can be easily (and more quickly) be computed with simple addition:

public class EvenFibonacciFinder {
    private static final int MAX_NUM = 4_000_000;

    public static void main(String[] args)
    {
        long time = System.nanoTime();
        int  f1   = 1;
        int  f2   = 2;
        long sum  = 0;

        while (f2 <= MAX_NUM) {
            int f3;
            sum += f2;
            // This skips three ahead in the sequence.
            f3 = f1 + f2;
            f1 = f2 + f3;
            f2 = f1 + f3;
        }
        System.out.println("Result: " + sum +
                "\nTime used for calculation in nanoseconds: " +
                (System.nanoTime() - time));
    }
}

Output:

Original code using pow:
Result: 4613732
Time used for calculation in nanoseconds: 296694

Using addition instead of pow:
Result: 4613732
Time used for calculation in nanoseconds: 45406