Project Euler 2: Even Fibonacci numbers Solution

1const solution = () => {
2 const LIMIT = 4 * 1e6
3
4 function* fib() {
5 let a = 0
6 let b = 1
7 while (1) {
8 yield a
9 ;[a, b] = [b, a + b]
10 }
11 }
12
13 const fibGen = fib()
14 let res = 0
15 while (1) {
16 const val = fibGen.next().value
17 if (val > LIMIT) break
18 if (val % 2 === 0) res += val
19 }
20 console.log(res)
21}
22
23solution()

Comments

Loading comments...

Tags

projecteuler

Apply and earn a $2,500 bonus once you're hired on your first job!

Clients from the Fortune 500 to Silicon Valley startups

Choose your own rate, get paid on time

From hourly, part-time, to full-time positions

Flexible remote working environment

A lot of open JavaScript jobs!!

Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.

Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.

Next Post

Project Euler 1: Multiples of 3 and 5

Previous Post

HoningJS

Search Posts