Skip to content

Instantly share code, notes, and snippets.

@menjaraz
Last active February 8, 2023 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save menjaraz/af0a5297574206709a0ef30acf817b29 to your computer and use it in GitHub Desktop.
Save menjaraz/af0a5297574206709a0ef30acf817b29 to your computer and use it in GitHub Desktop.
Project Euler Solution of Question 2
/*
* 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;
}
@menjaraz
Copy link
Author

menjaraz commented Feb 8, 2023

A quick and dirty code written in DMD64 D Compiler v2.102, solution to the Question 2 of Project Euler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment