Skip to the code
cf.
+25 XP

#659 Fibonacci Far Ahead

medium

A generative-art page sizes its spiral tiles with Fibonacci numbers: F(0) = 0, F(1) = 1, and F(n) = F(n - 1) + F(n - 2). A stress test asks for tiles very far along the sequence.

Write fibonacciFar(n) that returns F(n) modulo 1000000007. n can be up to 10^15, so a loop over every step is too slow.

Example 1

Input: n = 10
Output: 55

The sequence goes 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55.

Example 2

Input: n = 0
Output: 0

F(0) = 0 by definition.

Constraints

  • 0 <= n <= 10^15
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).