Fibonacci for 2016! Hard core version!

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


The answer is 1935.

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

Bill Bell
Feb 20, 2016

Python 3. May be compared with the Scheme solution.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
def Fib():
    t0=1
    t1=1
    yield 1,t0
    yield 2,t1
    n=2
    while True:
        t0,t1=t1,t0+t1
        n+=1
        yield n,t1

for n,f in Fib():
    if n==2016:
        print (sum([int(d) for d in str(f)]))
        break

same way! +1

Department 8 - 5 years, 3 months ago
Brian Riccardi
Feb 14, 2016

Scheme is very useful with this questions :D

1
2
3
4
5
6
7
8
9
(define f
  (lambda (n N n1 n2)
    (if (= n N) n1 (f (+ n 1) N (+ n1 n2) n1)
)))

(define dig-sum
  (lambda (s)
    (if (string=? s "") 0 (+ (- (char->integer (string-ref s 0)) 48) (dig-sum (substring s 1)))
)))

and run

1
(dig-sum (number->string (f 2 2016 1 1)))

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...