|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||
|
|||
|
Fibonacci Sequence
Well, suffice it to say I was bored, so I wrote a little (5 line) python program to calculate terms of the Fibonacci sequence.
The 1001th term is: 70330367711422815821835254877183549770181269836358 73274260490508715453711819693357974224949456261173 34877504492417659910881863632654502236471060120533 74121273867339111198139373125598767690091902245245 323403501 It's still computing the 1000 000th term or I'd have that for you, too. It has 110 000 terms to go. Last edited by ComradeNikolai : 23-06-2008 at 08:10. Reason: 1001th, not 1000th |
|
#2
|
|||
|
|||
|
Re: Fibonacci Sequence
I believe that is the 1001th term, not the 1000th term.
The 1000th term is 43466557686937456435688527675040625802564660517371 78040248172908953655541794905189040387984007925516 92959225930803226347752096896232398733224711616429 96440906533187938298969649928516003704476137795166 849228875. |
|
#3
|
||||
|
||||
|
Re: Fibonacci Sequence
Can your 5 lines of python beat my 3 lines of perl? =P
Code:
use bignum; $b=1; $x++,($a,$b)=($b,$a+$b),print"$x|$a\n"while(1); Can we see your program? =] (I like perl *glances at his signature =D*) |
|
#4
|
|||
|
|||
|
Re: Fibonacci Sequence
Oh, yeah, my bad; it is the 1001th term. That makes the one my computer chugged all night for the million and first term, and it was bigger than the gnome terminal can display
... so I don't have it.Here's the program, as requested Code:
#! /usr/bin/python a,b,c = 0,1,1000 while c > 0: a,b,c = b, a+b, c-1 print b Perl impresses me. |
|
#5
|
|||
|
|||
|
Re: Fibonacci Sequence
Python can do it in 1:
Code:
for z in range(N-1): print vars().setdefault('a',[1,1]) and a.append(a.pop(0)+a[0]) or a[0][0]
That being shown, this is the antithesis of the language and good coding practice. As one who has had to work with tools coded in Perl, all I will say is that obfuscation isn't nearly as cool as perlers think it is. I'd rather work with something written in a hundred lines of python than in 5 lines of perl. |
|
#6
|
|||
|
|||
|
Re: Fibonacci Sequence
I did it in java, because thats what I use regularly, along with Easy C for FIRST. I think though I can write a program that can find the millionth term that runs in only a couple minutes. I'll test it out later.
I also need to output to a file instead of a screen. Last edited by tennispro9911 : 23-06-2008 at 15:08. |
|
#7
|
|||
|
|||
|
Re: Fibonacci Sequence
Mine probably could run in less than hours or a few minutes, but I had a horribly bad computer which I was using it; Windows 98 lags the computer, so it's running Ubuntu.
|
|
#8
|
|||
|
|||
|
Re: Fibonacci Sequence
The millionth term is way too long to display. I tried, took up a couple thousand lines on my browser. I also can't include a file in the post because the limit for a txt file is 100 kb and my file is 204.1 kb. For anyone who wants to check if their program works correctly, the first few digits are 195328212870775773163201494759625.....
It took me 0:29:843 (min:sec:millis) to calculate it, and 1:21:467 total to calculate it and output it to a file. The number is so fricken large it took longer to write it to a file. How often can you say that? Last edited by tennispro9911 : 23-06-2008 at 16:59. Reason: Removed number |
|
#9
|
|||
|
|||
|
Re: Fibonacci Sequence
So I changed my program to break up the files to about 90 kb each file. Here are my files. First one contains first x digits and so on and so forth. 90k digits in the first two files.
Last edited by tennispro9911 : 23-06-2008 at 17:17. Reason: Explaining files |
|
#10
|
|||
|
|||
|
Re: Fibonacci Sequence
What language did you use for that? I would try it in C++, but I know that C++ has roundoff due to not allowing such large numbers...
|
|
#11
|
|||
|
|||
|
Re: Fibonacci Sequence
A bad computer slows it down by a linear multiplier. Say if my computer is 2.0 Ghz and your computer is 200 MHz then it would take your comp about 10 times longer to run the same program, depending on things running in the background. I doubt your computer is slower than 200 MHz, so if it takes a whole night to run, then it would still take an hour or so to run on a "fast" computer. That said, obviously if you don't calculate every term in between, you can calculate the nth term much much faster, which is why I could calculate the millionth in 29 sec instead of a few hours.
|
|
#12
|
|||
|
|||
|
Re: Fibonacci Sequence
I used Java. I used the BigInteger class. Otherwise I would have roundoff errors as you said.
|
|
#13
|
|||
|
|||
|
Re: Fibonacci Sequence
Ah, yes, Java. I should learn that sometime... after I finish learning C++ and learn Python. Maybe if I hook myself up with the AP CS exam next year, I'll be motivated enough to learn Java (our school doesn't offer AP CS, but I was able to take AP English without the class this year, so I don't see a difference there).
What kind of hardware do you have on the computer you did that on? Oh, just FYI, I think the computer I did this on originally has 256 MB RAM and less than a 1 GHz processor... not fun... |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Mechanics of a 3 speed shifting sequence | sanddrag | Technical Discussion | 3 | 27-02-2005 00:34 |
| [FVG]: Opening Sequence | Joshua May | FIRST-related Organizations | 16 | 24-06-2004 00:00 |
| RC bootup sequence | Venkatesh | Programming | 17 | 18-11-2003 20:56 |
| PB_Mode Sequence | DKolberg | Programming | 3 | 17-03-2003 10:54 |
| Competition Sequence for PBasic | ttedrow | Programming | 12 | 23-01-2003 08:49 |