Yes, it is unnecessary to create a sequence, then add it. Just simply add while counting up. Take a look at my question and this this answer; it will simply create the sequence while adding, without even having to store the sequence, just the result and the previous numbers. It takes the advantage of the fact that every three Fibonacci numbers in the sequence is an even number, so inside the while
loop, all it needs to do, is add the result, then skip 2 numbers. The code (for convenience) is down below:
public class EvenFibonacciFinder {
private static final int MAX_NUM = 4_000_000;
public static void main(String[] args)
{
long time = System.nanoTime();
int fib1 = 1;
int fib2 = 2;
long sum = 0;
while (f2 <= MAX_NUM) {
int fib3;
sum += fib2;
// This skips three ahead in the sequence.
fib3 = fib1 + fib2;
fib1 = fib2 + fib3;
fib2 = fib1 + fib3;
}
long end = System.nanoTime();
System.out.println("Result: " + sum +
"\nTime used for calculation in nanoseconds: " +
(end - time));
}
}
Yes, it is unnecessary to create a sequence, then add it. Just simply add while counting up. Take a look at my question and this answer; it will simply create the sequence while adding, without even having to store the sequence, just the result and the previous numbers. It takes the advantage of the fact that every three Fibonacci numbers in the sequence is an even number, so inside the while
loop, all it needs to do, is add the result, then skip 2 numbers. The code (for convenience) is down below:
public class EvenFibonacciFinder {
private static final int MAX_NUM = 4_000_000;
public static void main(String[] args)
{
long time = System.nanoTime();
int fib1 = 1;
int fib2 = 2;
long sum = 0;
while (f2 <= MAX_NUM) {
int fib3;
sum += fib2;
// This skips three ahead in the sequence.
fib3 = fib1 + fib2;
fib1 = fib2 + fib3;
fib2 = fib1 + fib3;
}
long end = System.nanoTime();
System.out.println("Result: " + sum +
"\nTime used for calculation in nanoseconds: " +
(end - time));
}
}
Yes, it is unnecessary to create a sequence, then add it. Just simply add while counting up. Take a look at my question and this answer; it will simply create the sequence while adding, without even having to store the sequence, just the result and the previous numbers. It takes the advantage of the fact that every three Fibonacci numbers in the sequence is an even number, so inside the while
loop, all it needs to do, is add the result, then skip 2 numbers. The code (for convenience) is down below:
public class EvenFibonacciFinder {
private static final int MAX_NUM = 4_000_000;
public static void main(String[] args)
{
long time = System.nanoTime();
int fib1 = 1;
int fib2 = 2;
long sum = 0;
while (f2 <= MAX_NUM) {
int fib3;
sum += fib2;
// This skips three ahead in the sequence.
fib3 = fib1 + fib2;
fib1 = fib2 + fib3;
fib2 = fib1 + fib3;
}
long end = System.nanoTime();
System.out.println("Result: " + sum +
"\nTime used for calculation in nanoseconds: " +
(end - time));
}
}
Yes, it is unnecessary to create a sequence, then add it. Just simply add while counting up. Take a look at my question and this answer; it will simply create the sequence while adding, without even having to store the sequence, just the result and the previous numbers. It takes the advantage of the fact that every three Fibonacci numbers in the sequence is an even number, so inside the forwhile
loop, all it needs to do, is add the result, then skip 2 numbers. The code (for convenience) is down below:
public class EvenFibonacciFinder {
private static final int MAX_NUM = 4_000_000;
public static void main(String[] args)
{
long time = System.nanoTime();
int fib1 = 1;
int fib2 = 2;
long sum = 0;
while (f2 <= MAX_NUM) {
int fib3;
sum += fib2;
// This skips three ahead in the sequence.
fib3 = fib1 + fib2;
fib1 = fib2 + fib3;
fib2 = fib1 + fib3;
}
long end = System.nanoTime();
System.out.println("Result: " + sum +
"\nTime used for calculation in nanoseconds: " +
(end - time));
}
}
Yes, it is unnecessary to create a sequence, then add it. Just simply add while counting up. Take a look at my question and this answer; it will simply create the sequence while adding, without even having to store the sequence, just the result and the previous numbers. It takes the advantage of the fact that every three Fibonacci numbers in the sequence is an even number, so inside the for
loop, all it needs to do, is add the result, then skip 2 numbers. The code (for convenience) is down below:
public class EvenFibonacciFinder {
private static final int MAX_NUM = 4_000_000;
public static void main(String[] args)
{
long time = System.nanoTime();
int fib1 = 1;
int fib2 = 2;
long sum = 0;
while (f2 <= MAX_NUM) {
int fib3;
sum += fib2;
// This skips three ahead in the sequence.
fib3 = fib1 + fib2;
fib1 = fib2 + fib3;
fib2 = fib1 + fib3;
}
long end = System.nanoTime();
System.out.println("Result: " + sum +
"\nTime used for calculation in nanoseconds: " +
(end - time));
}
}
Yes, it is unnecessary to create a sequence, then add it. Just simply add while counting up. Take a look at my question and this answer; it will simply create the sequence while adding, without even having to store the sequence, just the result and the previous numbers. It takes the advantage of the fact that every three Fibonacci numbers in the sequence is an even number, so inside the while
loop, all it needs to do, is add the result, then skip 2 numbers. The code (for convenience) is down below:
public class EvenFibonacciFinder {
private static final int MAX_NUM = 4_000_000;
public static void main(String[] args)
{
long time = System.nanoTime();
int fib1 = 1;
int fib2 = 2;
long sum = 0;
while (f2 <= MAX_NUM) {
int fib3;
sum += fib2;
// This skips three ahead in the sequence.
fib3 = fib1 + fib2;
fib1 = fib2 + fib3;
fib2 = fib1 + fib3;
}
long end = System.nanoTime();
System.out.println("Result: " + sum +
"\nTime used for calculation in nanoseconds: " +
(end - time));
}
}
Yes, it is unnecessary to create a sequence, then add it. Just simply add while counting up. Take a look at my question and this answer; it will simply create the sequence while adding, without even having to store the sequence, just the result and the previous numbers. It takes the advantage of the fact that every three Fibonacci numbers in the sequence is an even number, so inside the for
loop, all it needs to do, is add the result, then skip 2 numbers. The code (for convenience) is down below:
public class EvenFibonacciFinder {
private static final int MAX_NUM = 4_000_000;
public static void main(String[] args)
{
long time = System.nanoTime();
int fib1 = 1;
int fib2 = 2;
long sum = 0;
while (f2 <= MAX_NUM) {
int fib3;
sum += fib2;
// This skips three ahead in the sequence.
fib3 = fib1 + fib2;
fib1 = fib2 + fib3;
fib2 = fib1 + fib3;
}
long end = System.nanoTime();
System.out.println("Result: " + sum +
"\nTime used for calculation in nanoseconds: " +
(end - time));
}
}