Last active
February 8, 2023 11:58
-
-
Save menjaraz/af0a5297574206709a0ef30acf817b29 to your computer and use it in GitHub Desktop.
Project Euler Solution of Question 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Project Euler Question 2 | |
* Even Fibonacci numbers | |
* Link: https://projecteuler.net/problem=2 | |
* | |
* Solution: 4_613_732 | |
*/ | |
import std.stdio, std.range, std.algorithm; | |
void main() { | |
// Knowing that... | |
// fib(34) is 5_702_887 and more than 4_000_000, too great! | |
// fib(33) is 3_524_578 and | |
// fib(32) is 2_178_309 | |
// ... all fibonacci numbers down from fib(33) should processed | |
int[2] fibArrHiBound = [3_524_578, 2_178_309]; | |
int[] evenFibArr =[3_524_578]; | |
while (fibArrHiBound[0] - fibArrHiBound[1] != 0) { | |
int candidateFib = fibArrHiBound[0] - fibArrHiBound[1]; | |
if (candidateFib%2 == 0) { | |
evenFibArr ~= candidateFib; | |
} | |
fibArrHiBound[0] = fibArrHiBound[1]; | |
fibArrHiBound[1] = candidateFib; | |
} | |
evenFibArr.sum.writeln; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A quick and dirty code written in DMD64 D Compiler v2.102, solution to the Question 2 of Project Euler.