- #light
- let fibs = Seq.unfold (fun (n0, n1) -> Some(n0, (n1, n0 + n1))) (1I,1I)
- let first n = Seq.take n fibs
Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.
Forum Search
Forum Statistics
UsersTotal Post History
- Posts:
- 78543
- Topics:
- 17948
7-Day Post History
- New Posts:
- 60
- New Topics:
- 26
- Active Topics:
- 31
Our newest member
Other
-
FAQ
All times are UTC [ DST ]
Google Ads
LTD Puzzle 5: Calculating the Fibonacci Sequence
Forum rules
Always post answers in a "Hidecode" tag, so that others have a chance to answer the question too.
Always post answers in a "Hidecode" tag, so that others have a chance to answer the question too.
17 posts • Page 1 of 1
Please wait...
LTD Puzzle 5: Calculating the Fibonacci Sequence
This weeks puzzle is a nice and simple one to ease all those with sore heads from the July 4th celebrations back into thinking code...
If you've read the Dan Brown book "The Da Vinci Code" then you'll be quite familiar with this already... we would like you to calculate 'X' numbers in the Fibonacci Sequence, which is essentially adding the previous 2 occurring numbers together to make the next .. e.g. 0,1,1,2,3,5,8,13,21,34,55,89,....etc.
have fun...
p.s. don't forget to use the new [hidecode] tag to hide your code and highlight the syntax
If you've read the Dan Brown book "The Da Vinci Code" then you'll be quite familiar with this already... we would like you to calculate 'X' numbers in the Fibonacci Sequence, which is essentially adding the previous 2 occurring numbers together to make the next .. e.g. 0,1,1,2,3,5,8,13,21,34,55,89,....etc.
- Code should accept a single parameter to set the amount of numbers in the sequence to calculate, e.g. first 10 numbers, first 100, etc
- Format of output should be simply a list of numbers separated by a comma between each number; e.g. 0,1,1,2,3,5,8 etc..
- Interesting Variations score more points of course

- Sequence always starts at 0,1 - so minimum sequence length is 2.
have fun...

p.s. don't forget to use the new [hidecode] tag to hide your code and highlight the syntax
a smile is worth a thousand kind words, so smile, it's easy! 
CODE: $5
WORKING CODE: $500
PROPERLY DESIGNED & WORKING CODE: Priceless

CODE: $5
WORKING CODE: $500
PROPERLY DESIGNED & WORKING CODE: Priceless
-

damber - LTD Admin

-









- Posts: 3117
- Joined: Tue Oct 09, 2007 1:48 pm
- Location: North Wales, UK
Re: LTD Puzzle 5: Calculating the Fibonacci Sequence
this is a quick and dirty example in vb.net.
Max iterations is apparently with int64
Max iterations is apparently with int64
Code is hidden, SHOW
pink fuzzy slippers
-

chrissie1 - Senior Guru

-











- Posts: 9106
- Joined: Wed Oct 10, 2007 7:18 pm
- Location: Belgium
Re: LTD Puzzle 5: Calculating the Fibonacci Sequence
pink fuzzy slippers
-

chrissie1 - Senior Guru

-











- Posts: 9106
- Joined: Wed Oct 10, 2007 7:18 pm
- Location: Belgium
Re: LTD Puzzle 5: Calculating the Fibonacci Sequence
Code is hidden, SHOW
Say what you like about the tenets of National Socialism Dude, at least it's an ethos
-

AlexCuse - LTD Admin

-










- Posts: 5263
- Joined: Tue Oct 09, 2007 5:26 pm
- Location: Pennsylvania, US
Re: LTD Puzzle 5: Calculating the Fibonacci Sequence
A not particularly exciting VB6 version...
Code is hidden, SHOW
Andy
-

HarleyQuinn - LTD Admin

-
- Posts: 308
- Joined: Tue Dec 18, 2007 3:14 pm
- Location: Sheffield, UK
Re: LTD Puzzle 5: Calculating the Fibonacci Sequence
I have two entries, since neither approach has been posted yet.
First, a C# .NET 3.5 approach using IEnumerable.
And, here's a C++/CLI version that uses template meta-programming. This causes the compiler to compute all the math and unroll the loops, so the actual executed code is doing nothing more than printing the numbers. Changing to native C++ would be a matter of changing Console::WriteLine() to printf() function, updating the main() signature to main(int argc, const char *argv[]), and replacing "using ..." with "#include <stdlib.h>".
First, a C# .NET 3.5 approach using IEnumerable.
Code is hidden, SHOW
And, here's a C++/CLI version that uses template meta-programming. This causes the compiler to compute all the math and unroll the loops, so the actual executed code is doing nothing more than printing the numbers. Changing to native C++ would be a matter of changing Console::WriteLine() to printf() function, updating the main() signature to main(int argc, const char *argv[]), and replacing "using ..." with "#include <stdlib.h>".
Code is hidden, SHOW
-

spoulson - Senior Apprentice

-


- Posts: 205
- Joined: Mon Jun 02, 2008 2:37 am
- Location: Middletown, DE, USA
Re: LTD Puzzle 5: Calculating the Fibonacci Sequence
Another VB6 version.
Unlike all the other submissions, this version can calculate fibonacci numbers that are very long without losing any significant digits (like using the double/float data type). I tested this with calculating the first 10,000 fibonacci number. This version is probably limited to the amount of text that can fit in to a vb6 text box.
Unlike all the other submissions, this version can calculate fibonacci numbers that are very long without losing any significant digits (like using the double/float data type). I tested this with calculating the first 10,000 fibonacci number. This version is probably limited to the amount of text that can fit in to a vb6 text box.
Code is hidden, SHOW
-George
-

gmmastros - LTD Admin

-











- Posts: 2221
- Joined: Tue Oct 09, 2007 5:19 pm
Re: LTD Puzzle 5: Calculating the Fibonacci Sequence
Nice one George, I originally went with an array solution but dropped it for the shorter code (I've given you a point as I didn't have time to make either of my solutions as nicely outputted as yours!) 
Plus, the limitation of a textbox is gotten around by outputting to a file and that's probably only one more line of code if you're going absolutely minimal

Plus, the limitation of a textbox is gotten around by outputting to a file and that's probably only one more line of code if you're going absolutely minimal

Andy
-

HarleyQuinn - LTD Admin

-
- Posts: 308
- Joined: Tue Dec 18, 2007 3:14 pm
- Location: Sheffield, UK
Re: LTD Puzzle 5: Calculating the Fibonacci Sequence
Hi,
what about a lazy functional solution written with F#?
Claudio
http://www.fsharp.it
what about a lazy functional solution written with F#?
Code is hidden, SHOW
Claudio
http://www.fsharp.it
- sembee
- Newbie

-
- Posts: 1
- Joined: Wed Jul 09, 2008 9:53 am
Re: LTD Puzzle 5: Calculating the Fibonacci Sequence
That's awesome sembee. That very thing came to mind last night when I was trying to focus on school work (I had tried going down that road for the prime number puzzle, but found that sequence was not really orderly enough to use that particular function effectively).
Say what you like about the tenets of National Socialism Dude, at least it's an ethos
-

AlexCuse - LTD Admin

-










- Posts: 5263
- Joined: Tue Oct 09, 2007 5:26 pm
- Location: Pennsylvania, US
Re: LTD Puzzle 5: Calculating the Fibonacci Sequence
Code is hidden, SHOW
it calculates up to 50095301248058391139327916261, the 140th term. That's 50 octillion!
God cries a little bit every time someone builds a database.
-

Emtucifor - Guru

-










- Posts: 2832
- Joined: Fri May 30, 2008 9:30 pm
- Location: California
Re: LTD Puzzle 5: Calculating the Fibonacci Sequence
There are 2 types of winner - a "People's Champion" (the one with the most rating points across all their posts) and the LTD Admin's Champion (selected with our special formula....honest...well, maybe it's just the ones that the admins thought were particularly good
)
So...
People's Champion: sembee scoring 13 points for his very short and sweet answer.
LTD Admin's Champion: George for using strings to get the job done.
Congratulations to all.. and good luck on next weeks puzzle
)So...
People's Champion: sembee scoring 13 points for his very short and sweet answer.
LTD Admin's Champion: George for using strings to get the job done.
Congratulations to all.. and good luck on next weeks puzzle

pink fuzzy slippers
-

chrissie1 - Senior Guru

-











- Posts: 9106
- Joined: Wed Oct 10, 2007 7:18 pm
- Location: Belgium
Re: LTD Puzzle 5: Calculating the Fibonacci Sequence
My solution in PHP:
Output (first 10 lines):
Code is hidden, SHOW
Output (first 10 lines):
- FibonacciSequence(2): 0,1,1
- FibonacciSequence(3): 0,1,1,2
- FibonacciSequence(4): 0,1,1,2,3
- FibonacciSequence(5): 0,1,1,2,3,5
- FibonacciSequence(6): 0,1,1,2,3,5,8
- FibonacciSequence(7): 0,1,1,2,3,5,8,13
- FibonacciSequence(8): 0,1,1,2,3,5,8,13,21
- FibonacciSequence(9): 0,1,1,2,3,5,8,13,21,34
- FibonacciSequence(10): 0,1,1,2,3,5,8,13,21,34,55
- FibonacciSequence(11): 0,1,1,2,3,5,8,13,21,34,55,89
- FibonacciSequence(12): 0,1,1,2,3,5,8,13,21,34,55,89,144
I try to improve my English language skills. Most things i do better than this.
- tisodotsk
- Apprentice

-

- Posts: 22
- Joined: Fri Aug 08, 2008 12:45 pm
- Location: Bratislava, Slovakia
Re: LTD Puzzle 5: Calculating the Fibonacci Sequence
- luckenbach
- Newbie

-
- Posts: 2
- Joined: Thu Jan 20, 2011 4:09 am
- janroesner
- Newbie

-
- Posts: 1
- Joined: Sat Aug 18, 2012 6:40 pm
Re: LTD Puzzle 5: Calculating the Fibonacci Sequence
This is a problem that dc(1) was made to solve. With its arbitrary precision, you're only bound by the size of your stack (recursive macro call) and not the size of your variables. I was able to calculate the values up to around the 12,900th term using this (I ran into issues after that). And those issues are related to the recursion. This could probably be solved to work with terms even larger than that by putting the loop itself into a shell script and using a temporary file to store the numbers between runs. But I've already taken longer writing this than it took to write the actual program and reworking it to handle as many terms as anyone could ever want is too much for me right now.
This time with comments:
OK, I'm not going to do every puzzle in dc. This was just one where it actually makes sense.
OK, I couldn't help myself. I was able to increase the overall length by breaking the calculation into chunks of smaller sequences to calculate. By using nested loops, I was able to reduce the stack usage. I ran it and was well over 50,000 values before I got bored watching it spit out values; I had changed it for the test run to number the output.
Code is hidden, SHOW
This time with comments:
Code is hidden, SHOW
OK, I'm not going to do every puzzle in dc. This was just one where it actually makes sense.
OK, I couldn't help myself. I was able to increase the overall length by breaking the calculation into chunks of smaller sequences to calculate. By using nested loops, I was able to reduce the stack usage. I ran it and was well over 50,000 values before I got bored watching it spit out values; I had changed it for the test run to number the output.
Code is hidden, SHOW
- frob23
- Newbie

-
- Posts: 2
- Joined: Tue Sep 04, 2012 11:04 pm
Re: LTD Puzzle 5: Calculating the Fibonacci Sequence
- whitekalashnikov
- Newbie

-
- Posts: 2
- Joined: Sat Nov 10, 2012 2:15 pm
17 posts • Page 1 of 1


LTD Social Sitings
Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.