HACKER RANK Project Euler 25 - N-Digit Fibonacci Number - Solution.'Py'
Project Euler Challenges-25 - N-Digit Fibonacci Number Solution
Problem Statement
This problem is a programming version of Problem 25 from projecteuler.net
The Fibonacci sequence is defined by the recurrence relation:
Fn=Fn−1+Fn−2, where F1=1 and F2=1
.
Hence the first 12 terms will be:
F1=1F2=1F3=2F4=3F5=5F6=8F7=13F8=21F9=34F10=55F11=89F12=144
The 12th term, F12 , is the first term to contain three digits.
What is the first term in the Fibonacci sequence to contain N digits?
Input Format
The first line contains an integer T , i.e., number of test cases.
Next T lines will contain an integer N .
Output Format
Print the values corresponding to each test case.
Constraints
1≤T≤5000
2≤N≤5000
Sample Input
2
3
4
Sample Output
12
17
Problem Statement
This problem is a programming version of Problem 25 from projecteuler.net
The Fibonacci sequence is defined by the recurrence relation:
Hence the first 12 terms will be:
The 12th term, F12 , is the first term to contain three digits.
What is the first term in the Fibonacci sequence to containN digits?
What is the first term in the Fibonacci sequence to contain
Input Format
The first line contains an integerT , i.e., number of test cases.
NextT lines will contain an integer N .
The first line contains an integer
Next
Output Format
Print the values corresponding to each test case.
Print the values corresponding to each test case.
Constraints
1≤T≤5000
2≤N≤5000
Sample Input
2
3
4
Sample Output
12
17
Solution:
from math import log10, ceil, sqrt
t = input()
while (t>0):
t -= 1
phi = (1+sqrt(5))/2
print int(ceil((input()-1 + log10(5)/2) / log10(phi)))
from math import log10, ceil, sqrt
t = input()
while (t>0):
t -= 1
phi = (1+sqrt(5))/2
print int(ceil((input()-1 + log10(5)/2) / log10(phi)))
Comments
Post a Comment