4
I found this task here.
Given the ith (1<=i<=35) Fibonacci number F(i) calculate the sum of the ith till i+9th number F(i)+F(i+1)+...+F(i+9) and the last digit of the i+246th one F(i+246)
I have been trying to solve this using python and some tricks(Binnet's formula and a tricky recurrence):
f=lambda n:((1+5**.5)**n-(1-5**.5)**n)/(2**n*5**.5)
exec"n=input();print int(55*f(n)+88*f(n+1)+f(n+6)%10);"*input()
but I didn't yet managed to squeeze thought the give source code limit which is 111 and mine is 115,any hints how to improve my solution?
I am a rather newbie to python so any sort of help resulting in a successful solution will be much appreciated.
Thanks,
PS:After posting the same question in stackoverflow I thought that this should be more appropriate here as this site is precisely for Puzzles and Codegolf.
Can you really not factor out
5**.5
? – Peter Taylor – 2011-04-06T12:34:41.303@Peter:Thanks,I missed that somehow :-( – Quixotic – 2011-04-06T12:37:01.677
2Don't double post, if the mods on SO judge your question to be better suited for this site they will move it here. – aaaaaaaaaaaa – 2011-04-06T14:05:12.137
1As for golfing, you are doing it wrong, you don't need the fast formula, you need the short formula, and there is incidentally some extremely short formula to be found for this problem. Binet's is probably longer than the normal recurrence implementation, and if you really must use Binet's you can skip the
-(1-5**.5)**n
part and just round the result. – aaaaaaaaaaaa – 2011-04-06T15:02:38.307