Big Fib
-
- Posts: 106
- Joined: Thu Oct 29, 2009 9:21 pm
Big Fib
Anyone remember how they did this one? I forgot.
-
- Posts: 106
- Joined: Thu Oct 29, 2009 9:21 pm
Mathematica code, somewhat bloated...
the simple explanation being:
Find the digits which give 1 in mod 20000
Take this digit of Fibonacci[1500000]
Convert these to strings
Join them up
Unfortunately, this code is too complex for WolframAlpha. On my computer, this code takes almost two seconds; a couple of optimisations like precomputing Fibonacci[1500000] gives a speed increase to 0.000131 seconds.
Code: Select all
ToString/@(IntegerDigits[Fibonacci[1500000]][[#]]&/@Select[Range[313481],Mod[#,20000]==1&])//StringJoin
Find the digits which give 1 in mod 20000
Take this digit of Fibonacci[1500000]
Convert these to strings
Join them up
Unfortunately, this code is too complex for WolframAlpha. On my computer, this code takes almost two seconds; a couple of optimisations like precomputing Fibonacci[1500000] gives a speed increase to 0.000131 seconds.
Code: Select all
With[{n = Fibonacci[1500000] // IntegerDigits},
ToString /@ (n[[#]] & /@ Range[1, 313481, 20000]) // StringJoin]
There is no spoon.
-
- Posts: 4
- Joined: Wed Jul 20, 2011 9:04 pm
I like Ruby.
Took approx. 199.827814006 s with Ruby.
Using some Fib-identities can shorten run time even more: http://en.wikipedia.org/wiki/Fibonacci_ ... identities

Code: Select all
def fibn(m)
if m==0
out=0
else
n=1
fibn=1
fibnMinus1=0
while n<m do
n=n+1
h=fibn+fibnMinus1
fibnMinus1=fibn
fibn=h
end
out=fibn
end
return out
end
def efind x
k=fibn(x).to_s
s=""
for i in 0..k.length-1
if i%20000==0 then
s+=k[i].to_s
end
end
return s
end
puts efind 1500000
-
- Posts: 4
- Joined: Wed Jul 20, 2011 9:04 pm
I suppose it sort of iscakeeatinghaxxor wrote:Using Mathematica/Wolfram Alpha always looks like kind of cheating to me :plaz0r wrote:Actually,
StringTake[ToString@Fibonacci[1500000], {1, -1, 20000}]
is more concise

There is no spoon.
omitting mult ... standard matrix 2x2 multiplication
Code: Select all
private static void fibcalc(){
Result [0][0]=BigInteger.ONE; Result [0][1]=BigInteger.ZERO;
Result [1][0]=BigInteger.ZERO; Result [1][1]=BigInteger.ONE;
PowerTwo [0][0]=BigInteger.ZERO; PowerTwo [0][1]=BigInteger.ONE;
PowerTwo [1][0]=BigInteger.ONE; PowerTwo [1][1]=BigInteger.ONE;
int power = 1500000;
int powertwo = 1;
while (powertwo<=power) {
if ((power&powertwo)!=0) {
mult(Result,PowerTwo);
}
powertwo<<=1;
mult(PowerTwo,PowerTwo);
}
String resultstr = Result[0][1].toString();
for(int i=0;i<resultstr.length();i+=20000) {
write("fib2.txt",String.valueOf(resultstr.charAt(i)));
}
}
PARI/GP code took less than a second:
Code: Select all
big_fib(N=30,STEP=7)=
{
local(n=fibonacci(N),d,nd=ceil(log(n)/log(10)),nt=nd\STEP,ynd,str="");
ynd=STEP*nt+1;
n\=10^(nd-ynd);
forstep(i=1,ynd,STEP,
d=n%10;
n\=10^STEP;
str=concat(Str(d),str);
);
str
}
/*
? big_fib(1500000,20000)
%53 = "1161269686625383"
? ##
*** last result computed in 920 ms.
*/
-
- Forum Admin
- Posts: 497
- Joined: Sat May 28, 2011 9:14 am
- Location: Germany
Never heard of that before. Good to know it exists, could be quite useful.sinan wrote:PARI/GP code
https://en.wikipedia.org/wiki/PARI/GP
-
- Posts: 58
- Joined: Thu Nov 29, 2012 7:45 pm
- Location: Germany
pari/gp is great for calculating with big numbers - I even solved bigger fib directly with pari.
Another program is sage (sagemath.org): it's actually python, but with math related library functions like equation solving, integral, ... It uses pari for some calculations.
Gives the answer in a few milliseconds.
Another program is sage (sagemath.org): it's actually python, but with math related library functions like equation solving, integral, ... It uses pari for some calculations.
Code: Select all
fibonacci(1500000).str()[::20000]