Skip to main content
added 163 characters in body
Source Link
bowmore
  • 5.4k
  • 1
  • 20
  • 23

The first trick of this exercise is recognizing you don't need to calculate the odd fibonacci numbers.

The series of only the even numbers is :

0, 2, 8, 34, 144, 610, 2584, 10946, 46368, 196418, 832040, 3524578, ...

So starting with 0 and 2 the next element is always 4 times the preceding one plus the one before that :

34 = 8*4 +2
144 = 34*4 + 8
...

Now the exercise is simply caluclating this series and keeping a running total, until you hit 4_000_000. If you do that using primitive long the performance is a lot better than what you have so far.

Output on my PC :

Yours     : Result: 4613732. Time used for calculation in nanoseconds: 530009.
Suggested : Result: 46137344613732. Time used for calculation in nanoseconds: 3603.

Edit : when I replaced 4*b + a by b + b + b + b + a performance got even better :

Result: 4613732. Time used for calculation in nanoseconds: 1201.

The first trick of this exercise is recognizing you don't need to calculate the odd fibonacci numbers.

The series of only the even numbers is :

0, 2, 8, 34, 144, 610, 2584, 10946, 46368, 196418, 832040, 3524578, ...

So starting with 0 and 2 the next element is always 4 times the preceding one plus the one before that :

34 = 8*4 +2
144 = 34*4 + 8
...

Now the exercise is simply caluclating this series and keeping a running total, until you hit 4_000_000. If you do that using primitive long the performance is a lot better than what you have so far.

Output on my PC :

Yours     : Result: 4613732. Time used for calculation in nanoseconds: 530009.
Suggested : Result: 4613734. Time used for calculation in nanoseconds: 3603.

The first trick of this exercise is recognizing you don't need to calculate the odd fibonacci numbers.

The series of only the even numbers is :

0, 2, 8, 34, 144, 610, 2584, 10946, 46368, 196418, 832040, 3524578, ...

So starting with 0 and 2 the next element is always 4 times the preceding one plus the one before that :

34 = 8*4 +2
144 = 34*4 + 8
...

Now the exercise is simply caluclating this series and keeping a running total, until you hit 4_000_000. If you do that using primitive long the performance is a lot better than what you have so far.

Output on my PC :

Yours     : Result: 4613732. Time used for calculation in nanoseconds: 530009.
Suggested : Result: 4613732. Time used for calculation in nanoseconds: 3603.

Edit : when I replaced 4*b + a by b + b + b + b + a performance got even better :

Result: 4613732. Time used for calculation in nanoseconds: 1201.
Source Link
bowmore
  • 5.4k
  • 1
  • 20
  • 23

The first trick of this exercise is recognizing you don't need to calculate the odd fibonacci numbers.

The series of only the even numbers is :

0, 2, 8, 34, 144, 610, 2584, 10946, 46368, 196418, 832040, 3524578, ...

So starting with 0 and 2 the next element is always 4 times the preceding one plus the one before that :

34 = 8*4 +2
144 = 34*4 + 8
...

Now the exercise is simply caluclating this series and keeping a running total, until you hit 4_000_000. If you do that using primitive long the performance is a lot better than what you have so far.

Output on my PC :

Yours     : Result: 4613732. Time used for calculation in nanoseconds: 530009.
Suggested : Result: 4613734. Time used for calculation in nanoseconds: 3603.