The Fibonacci numbers are
F
1
=
1
,
F
2
=
1
,
F
3
=
2
,
F
4
=
3
,
F
5
=
5
,
F
6
=
8
,
F
7
=
1
3
…
where the first two are both equal to 1 , and from then on, each one is the sum of the two preceding it. Of the first 2 3 2 4 Fibonacci numbers, how many have 3 as their last digit?
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.
Note that the Fibonacci sequence is an immediately periodic sequence mod any number m [thanks to Calvin Lin].
you actually write 60 unit digits... omg...
Python one-liner that is pretty much unreadable:
>>> [reduce(lambda x, _: (x[1] % 10, (x[0]+x[1]) % 10), range(i), (1,1))[0] for i in range(2324)].count(3)
309
A more mathematical approach :
Observe that the last digits of the Fibonacci sequence repeats. This is easy to notice: There are only 1 0 0 (read: a finite number) pairs of last digits of F n − 1 , F n − 2 , and the sequence of the last digits is uniquely determined from each pair. So once the pair repeats, we have found the cycle.
By bruteforcing from 1 , 1 , we see that the sequence of last digits repeat with period 6 0 . We can thus mark the terms that have a last digit of 3 . In this case, there are 8 such terms in a cycle, and 5 terms appear up to the 4 4 th term.
Since 2 3 2 4 = 3 8 ⋅ 6 0 + 4 4 , we know that there are 3 8 ⋅ 8 terms ending with a 3 in the first 3 8 ⋅ 6 0 terms, plus 5 more for the last 4 4 terms, thus we have 3 8 ⋅ 8 + 5 = 3 0 9 terms ending with 3 among the first 2 3 2 4 Fibonacci numbers.
nice... but can u help me ..whats wrong with this c code...int i,o=1,p=1,q,sum=0; for(i=0; i<2322; i++) { q=o+p; o=p; p=q; if(p%10==3) sum++; } printf("%d\n", sum); return 0;
Log in to reply
In C++, you might get an overflow. The 47th Fibonacci term is
2971215073
, which exceeds the capacity of
int
(only
2147483647
), and thus you get something like
-1323752223
instead. Furthermore, this is compounded with the fact that C++'s modulo operator uses the same sign as the dividend (for example,
-4 % 3 = -1
and not
2
). This causes a lot of weird things.
As a simple fix, just note that
(
a
+
b
)
m
o
d
1
0
=
(
(
a
m
o
d
1
0
)
+
(
b
m
o
d
1
0
)
)
m
o
d
1
0
, and so you can do the modulo operation on
q = o+p;
instead (making it
q = (o+p) % 10;
).
That one liner is cool,Ive seen reduce used before but what's the __: about?
Log in to reply
_
is a valid variable name in Python. Generally, you use
_
when you don't care about its value, just that it needs to be present for some reason.
:
is part of the
lambda
construct:
lambda [variables]: [result]
.
We start listing the last digits of the first several Fibonacci numbers (this is not as long as it sounds):
1 , 1 , 2 , ( 3 ) , 5 , 8 , ( 3 ) , 1 , 4 , 5 , 9 , 4 , ( 3 ) , 7 , 0 , 7 , 7 , 4 , 1 , 5 , 6 , 1 , 7 , 8 , 5 , ( 3 ) , 8 , 1 , 9 , 0 , 9 , 9 , 8 , 7 , 5 , 2 , 7 , 9 , 6 , 5 , 1 , 6 , 7 , ( 3 ) , 0 , ( 3 ) , ( 3 ) , 6 , 9 , 5 , 4 , 9 , ( 3 ) , 2 , 5 , 7 , 2 , 9 , 1 , 0 , ( 1 , 1 , 2 , 3 , . . . . )
We notice that the pattern repeats every 60 digits. For every 60 digits, there are eight 3s. For every 60 Fibonacci numbers, there are 8 Fibonacci numbers ending with three. From here, the rest of the problem is simple.
2 3 2 4 ÷ 6 0 = 3 8 R 4 4
There are 38 complete sets of 8 3s and 44 extra digits. We see from the above list that the first 44 digits of the sequence contain five 3s.
Hence, the total number of Fibonacci numbers ending with 3 is given by:
3 8 × 8 + 5 = 3 0 9
This problem can be solved with the following Mathematica command: Total[Array[If[Mod[Fibonacci[#1], 10] == 3, 1, 0] &, {2324}]] .
That said, this problem makes us think about the period of the Fibonacci numbers modulo m -- a fascinating, well-studied number-theoretic object called the Pisano period . In the case m = 1 0 , the Pisano period is 6 0 ; an alternate solution thus proceeds from the observation that 2 3 2 4 = 3 8 ⋅ 6 0 + 4 4 .
Q E D
write down the last digits of the first few fibonacci numbers. we can be sure that a repeating sequence will be found after 100 numbers because max no. of distinct ordered pairs (a,b) where ab are single digit =100.
the sequence repeats after 60th term (61st therm =1,62nd=1).
counting 3s firs 60have 8.
thus first 2280 have 8*38=304.
first 44 consist of 5 3s thus 2281to2324 also have 5 3s.
answer =304+5=309
Why waste time and Ink of your pen when you've got Python. I simply wrote a code and Saved it as Fib2324.py and Ran it. The code is given below:
1 2 3 4 5 6 7 |
|
And thus you get your answer as output = 309
In Python, a more readable approach than the one-liner. First, iterate through all the Fibonacci numbers and check if the last digit ends with 3. Here is the code:
a = 0
b = 1
n = 0
print a
print b
for i in range(1,2325):
c = a + b
a = b
b = c
if(c % 10 == 3):
n +=1
print n
The result is:
0
1
309
Therefore, answer is 309!
Assuming g(0) = 0. Fibonacci auxiliar recurrence is cyclic each 60 times, in other hands, periodic each 60 times. I will use a auxiliar recurrence function for look patterns. For example: g(n) = f(n) mod 10,
g(7) = 13 mod 10 = 3. g(1) = 1, g(2) = 1, g(3) = 2 .... g(10) = 5 ... g(59) = . Finish cycle at g(59). g(60) = g(0) = 0 g(60) = g(0)
So, generalizing: g(n) = g(60 * 1+k) = g(60 * 2+k) = . . . = g(60*p+k).
k is cycle position of g(n) and p number of cycles.
The number of 3's in g(n) is 8 per cycle.
2324 mod 60 = 44. Then, g(2324) = g(60 38+44) -> 38 8 + number of 3's between 0 and 44. -> 38*8 + 5 = 309
Put this in python
list=[]
k=eval(input("fibonacci"))
d=1
a=1
b=1
c=a+b
for i in range(k-2)
c=a+b
a=b
b=c
if c%10==3:
list.append(d)
e=list.count(d)
print(e)
last digit of fib(n) = last digit of fib(n-1) + last digit (n-2) MOD 10 keep tracing the last digits and you'll find fib(61) =1 and fib(62) = 1 , this means a cycle will be repeated over and over again in the first 60 Fibonacci numbers there are 8 numbers has their last digit = 3, so in the first 2340 there will be 8*39 = 312 but we need to remove all solutions from fib(2324) till fib(2340) they are equivalent to solutions from fib(44) to fib(60) which equal 3 so answer = 312 - 3 = 309
Note that F 5 8 ≡ 9 m o d 1 0 , F 5 9 ≡ 1 m o d 1 0 . Thus, modulo 10, the Fibonacci numbers are periodic with period 60. In the first 60 Fibonacci numbers, there are 8 which are congruent to 3 modulo 10. There are 38 such sets of 60 in the first 2324 Fibonacci numbers. Because 2 3 2 4 ≡ 4 4 m o d 6 0 , we must count the number of fibonacci numbers congruent to 3 modulo 10 in the first 44 (5). Hence, there are 3 8 ⋅ 8 + 5 = 3 0 9 Fibonacci numbers congruent to 3 modulo 10 in the first 2324.
The Pisano period mod 10 π ( n ) is 60 (with 8 3's in a period)and for the 44 left we have 5 more. 8*38 + 5 = 309
Problem Loading...
Note Loading...
Set Loading...
Examining the last digits of the numbers in the Fibonacci Sequence, we can see very clearly that they are periodic with period 60 (the last digits repeat themselves every 60 numbers). Hence, we can immediately conclude that, since 2 3 2 4 = ( 3 8 × 6 0 ) + 4 4 , there are ( 3 8 × 8 ) + 5 = 3 0 9 Fibonacci numbers (of the first 2 3 2 4 ) which have 3 as their last digit.
In fact, we can easily establish a general formula: every 6 0 consecutive Fibonacci numbers will have 8 numbers ending in a certain digit x ∣ x ≅ 1 ( m o d 2 ) and 4 numbers ending in a digit y ∣ y ≅ 0 ( m o d 2 ) , where 0 ≤ x , y ≤ 9 .