Big Fib

Discussion of challenges you have already solved
DaymItzJack
Posts: 106
Joined: Thu Oct 29, 2009 9:21 pm

Big Fib

Post by DaymItzJack »

Anyone remember how they did this one? I forgot.
User avatar
CodeX
Posts: 350
Joined: Fri Oct 17, 2008 5:28 pm

Post by CodeX »

you could use a closed expression function to get the answer or just use mathematica/wolfram alpha
DaymItzJack
Posts: 106
Joined: Thu Oct 29, 2009 9:21 pm

Post by DaymItzJack »

I've been trying to use wolframalpha to get the answer again, it's not working.
User avatar
CodeX
Posts: 350
Joined: Fri Oct 17, 2008 5:28 pm

Post by CodeX »

you could drop mathematica code into wolfram alpha to get the answer completely but I found it gave me both a closed expression function for the Fibonacci sequence and also the value of F1500000 which happens to be 313482 digits
User avatar
laz0r
Posts: 290
Joined: Thu Feb 04, 2010 4:18 pm
Location: Within the depths of Unix

Post by laz0r »

Mathematica code, somewhat bloated...

Code: Select all

ToString/@(IntegerDigits[Fibonacci[1500000]][[#]]&/@Select[Range[313481],Mod[#,20000]==1&])//StringJoin
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

With[{n = Fibonacci[1500000] // IntegerDigits}, 
 ToString /@ (n[[#]] & /@ Range[1, 313481, 20000]) // StringJoin]
There is no spoon.
gsjfoe
Posts: 19
Joined: Sat Nov 27, 2010 9:36 am

Post by gsjfoe »

i find that fib in python...
it took 15 minutes... :(
cakeeatinghaxxor
Posts: 4
Joined: Wed Jul 20, 2011 9:04 pm

I like Ruby.

Post by cakeeatinghaxxor »

Took approx. 199.827814006 s with Ruby. 8)

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
Using some Fib-identities can shorten run time even more: http://en.wikipedia.org/wiki/Fibonacci_ ... identities
User avatar
laz0r
Posts: 290
Joined: Thu Feb 04, 2010 4:18 pm
Location: Within the depths of Unix

Post by laz0r »

Actually,
StringTake[ToString@Fibonacci[1500000], {1, -1, 20000}]
is more concise :)
There is no spoon.
cakeeatinghaxxor
Posts: 4
Joined: Wed Jul 20, 2011 9:04 pm

Post by cakeeatinghaxxor »

laz0r wrote:Actually,
StringTake[ToString@Fibonacci[1500000], {1, -1, 20000}]
is more concise :)
Using Mathematica/Wolfram Alpha always looks like kind of cheating to me :p
User avatar
laz0r
Posts: 290
Joined: Thu Feb 04, 2010 4:18 pm
Location: Within the depths of Unix

Post by laz0r »

cakeeatinghaxxor wrote:
laz0r wrote:Actually,
StringTake[ToString@Fibonacci[1500000], {1, -1, 20000}]
is more concise :)
Using Mathematica/Wolfram Alpha always looks like kind of cheating to me :p
I suppose it sort of is :P a command like Solve or Integrate is far too powerful for us mere mortals!
There is no spoon.
Ingrater
Posts: 1
Joined: Sun Jun 23, 2013 9:11 am

Post by Ingrater »

Isn't the challange description wrong?

It says it wants the 1500000th member of the fibonaci sequence. Because the fibonaci sequence starts at zero that would be fib(1499999) but the correct answer is fib(1500000)?
User avatar
Hippo
Posts: 339
Joined: Sat Feb 01, 2014 12:05 am
Location: Praha 5

Post by Hippo »

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)));
		}
	}
sinan
Posts: 1
Joined: Thu Feb 25, 2016 10:41 am
Contact:

Post by sinan »

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.
*/
AMindForeverVoyaging
Forum Admin
Posts: 497
Joined: Sat May 28, 2011 9:14 am
Location: Germany

Post by AMindForeverVoyaging »

sinan wrote:PARI/GP code
Never heard of that before. Good to know it exists, could be quite useful.

https://en.wikipedia.org/wiki/PARI/GP
eulerscheZahl
Posts: 58
Joined: Thu Nov 29, 2012 7:45 pm
Location: Germany

Post by eulerscheZahl »

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.

Code: Select all

fibonacci(1500000).str()[::20000]
Gives the answer in a few milliseconds.
Post Reply