call(back)
Algorithms & Data StructureseasySign in to star problems

An Exact Fibonacci Generator

Write fibonacci(): a generator that yields 0, 1, 1, 2, 3, 5, ... forever, exactly — the 100th value must be right to the last digit.

const it = fibonacci();
it.next().value, it.next().value, it.next().value   // 0, 1, 1

The grader takes as many values as each test needs and compares them as strings.

Hints

Hint 1

Keep the last two values and yield in an infinite loop: yield a; [a, b] = [b, a + b].

Hint 2

Past the 78th term, JavaScript numbers lose precision. Use BigInt (0n, 1n), or Python's integers, which never overflow.

Asked at