Fibonacci for 2016!

Find the sum of last 9 digits of the 201 6 th 2016^\text{th} Fibonacci number .

Note: The first 2 fibonacci numbers are both 1.


The answer is 36.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

2 solutions

John Gilling
Feb 6, 2016

Here is a python solution I came up with:

fib = [1,1]

def update_fib(fib):
    return [fib[-1], fib[-1] + fib[-2]]

for i in range(3, 2017):
    fib = update_fib(fib)

print reduce(lambda x, y: int(x) + int(y), str(fib[-1])[-9:])

The idea behind it is that fib is always a list of two (since that's the only amount of space we need to compute the next value anyway), and it gets updated until the 2016th number is reached. The cheesy one liner at the bottom casts this huge number as a string, chops off all but the last nine digits, then performs a reduce by recasting each individual digit as an integer and summing. Full pass takes about 2ms on average.

I'd love to see what else you all come up with!

Moderator note:

We can simplify it slightly by taking the last 9 digits at each step. This way, we won't end up with a huge number, and could save some time in the process.

Vijay Kumar
Feb 7, 2016

a=slice(0,9) print sum(list(map(int,(str(fib[2016])[::-1])[a])))

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...