Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

A Technical Community for IT Professionals

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.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

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

Forum Statistics

Users
Members:
543
Members Online:
8
Guests Online:
6

Total Post History
Posts:
45705
Topics:
9398

7-Day Post History
New Posts:
343
New Topics:
81
Active Topics:
90

Our newest member
sangeeta

Other

FAQ
All times are UTC [ DST ]

Google Ads

LTD Puzzle 5: Calculating the Fibonacci Sequence

Mind Boggling Puzzles, to keep that grey matter in shape...
Forum rules
Always post answers in a "Hidecode" tag, so that others have a chance to answer the question too.
Please wait...

LTD Puzzle 5: Calculating the Fibonacci Sequence

Postby damber on Fri Jul 04, 2008 7:01 pm

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.

  • 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
User avatar
damber
LTD Admin
LTD Admin
LTD Silver - Rating: 342LTD Silver - Rating: 342LTD Silver - Rating: 342LTD Silver - Rating: 342LTD Silver - Rating: 342
LTD Silver - Rating: 342
 
Posts: 1959
Joined: Tue Oct 09, 2007 1:48 pm
Location: North Wales, UK
Unrated

Re: LTD Puzzle 5: Calculating the Fibonacci Sequence

Postby chrissie1 on Fri Jul 04, 2008 7:34 pm

this is a quick and dirty example in vb.net.

Max iterations is apparently with int64

Code is hidden, SHOW
User avatar
chrissie1
LTD Admin
LTD Admin
LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264
LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264
LTD Gold - Rating: 1264
 
Posts: 6038
Joined: Wed Oct 10, 2007 7:18 pm
Location: Belgium
Unrated

Re: LTD Puzzle 5: Calculating the Fibonacci Sequence

Postby chrissie1 on Fri Jul 04, 2008 7:37 pm

When using a double it reaches infinity at iteration



Code is hidden, SHOW
User avatar
chrissie1
LTD Admin
LTD Admin
LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264
LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264
LTD Gold - Rating: 1264
 
Posts: 6038
Joined: Wed Oct 10, 2007 7:18 pm
Location: Belgium

Re: LTD Puzzle 5: Calculating the Fibonacci Sequence

Postby AlexCuse on Sat Jul 05, 2008 4:48 pm

Code is hidden, SHOW
And we have contraptions like computers that cheat you out of becoming. Bill Gates says, "Wait till you see what your computer can become." But it's you who should be doing the becoming, not the damn fool computer.

My Crummy Web Page
User avatar
AlexCuse
LTD Admin
LTD Admin
LTD Silver - Rating: 642LTD Silver - Rating: 642LTD Silver - Rating: 642LTD Silver - Rating: 642LTD Silver - Rating: 642
LTD Silver - Rating: 642LTD Silver - Rating: 642LTD Silver - Rating: 642LTD Silver - Rating: 642
 
Posts: 3532
Joined: Tue Oct 09, 2007 5:26 pm
Location: Pennsylvania, US
Unrated

Re: LTD Puzzle 5: Calculating the Fibonacci Sequence

Postby HarleyQuinn on Tue Jul 08, 2008 1:28 pm

A not particularly exciting VB6 version...
Code is hidden, SHOW
Andy
User avatar
HarleyQuinn
LTD Admin
LTD Admin
LTD Bronze - Rating: 46
 
Posts: 308
Joined: Tue Dec 18, 2007 3:14 pm
Location: Sheffield, UK
Unrated

Re: LTD Puzzle 5: Calculating the Fibonacci Sequence

Postby spoulson on Tue Jul 08, 2008 4:03 pm

I have two entries, since neither approach has been posted yet.

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
User avatar
spoulson
Senior Apprentice
Senior Apprentice
LTD Bronze - Rating: 112LTD Bronze - Rating: 112LTD Bronze - Rating: 112
 
Posts: 203
Joined: Mon Jun 02, 2008 2:37 am
Location: Middletown, DE, USA

Re: LTD Puzzle 5: Calculating the Fibonacci Sequence

Postby gmmastros on Tue Jul 08, 2008 6:24 pm

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.

Code is hidden, SHOW
-George
User avatar
gmmastros
LTD Admin
LTD Admin
LTD Gold - Rating: 794LTD Gold - Rating: 794LTD Gold - Rating: 794LTD Gold - Rating: 794LTD Gold - Rating: 794
LTD Gold - Rating: 794LTD Gold - Rating: 794LTD Gold - Rating: 794LTD Gold - Rating: 794LTD Gold - Rating: 794
LTD Gold - Rating: 794
 
Posts: 1184
Joined: Tue Oct 09, 2007 5:19 pm

Re: LTD Puzzle 5: Calculating the Fibonacci Sequence

Postby HarleyQuinn on Wed Jul 09, 2008 12:00 am

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!) :D

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
User avatar
HarleyQuinn
LTD Admin
LTD Admin
LTD Bronze - Rating: 46
 
Posts: 308
Joined: Tue Dec 18, 2007 3:14 pm
Location: Sheffield, UK
Unrated

Re: LTD Puzzle 5: Calculating the Fibonacci Sequence

Postby sembee on Wed Jul 09, 2008 9:57 am

Hi,
what about a lazy functional solution written with F#?

Code is hidden, SHOW

Claudio
http://www.fsharp.it
sembee
Newbie
Newbie
LTD Bronze - Rating: 14
 
Posts: 1
Joined: Wed Jul 09, 2008 9:53 am

Re: LTD Puzzle 5: Calculating the Fibonacci Sequence

Postby AlexCuse on Wed Jul 09, 2008 1:47 pm

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).
And we have contraptions like computers that cheat you out of becoming. Bill Gates says, "Wait till you see what your computer can become." But it's you who should be doing the becoming, not the damn fool computer.

My Crummy Web Page
User avatar
AlexCuse
LTD Admin
LTD Admin
LTD Silver - Rating: 642LTD Silver - Rating: 642LTD Silver - Rating: 642LTD Silver - Rating: 642LTD Silver - Rating: 642
LTD Silver - Rating: 642LTD Silver - Rating: 642LTD Silver - Rating: 642LTD Silver - Rating: 642
 
Posts: 3532
Joined: Tue Oct 09, 2007 5:26 pm
Location: Pennsylvania, US
Unrated

Re: LTD Puzzle 5: Calculating the Fibonacci Sequence

Postby Emtucifor on Fri Jul 11, 2008 8:09 pm

[hide]Sub Fibonacci()
Dim A, B
A = CDec(0)
B = CDec(1)
On Error GoTo Fibonacci_Exit
Do
Debug.Print A
B = B + A
Debug.Print B
A = A + B
Loop
Fibonacci_Exit:
End Sub[/hide]
it calculates up to 50095301248058391139327916261, the 140th term. That's 50 octillion!
It's more cheesier if you do it right the first time!
User avatar
Emtucifor
Senior Sage
Senior Sage
LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753
LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753
LTD Gold - Rating: 753
 
Posts: 2152
Joined: Fri May 30, 2008 9:30 pm
Location: California

Re: LTD Puzzle 5: Calculating the Fibonacci Sequence

Postby chrissie1 on Fri Jul 18, 2008 10:30 am

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 :-)
User avatar
chrissie1
LTD Admin
LTD Admin
LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264
LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264
LTD Gold - Rating: 1264
 
Posts: 6038
Joined: Wed Oct 10, 2007 7:18 pm
Location: Belgium
Unrated

Re: LTD Puzzle 5: Calculating the Fibonacci Sequence

Postby tisodotsk on Fri Aug 08, 2008 7:59 pm

My solution in PHP:

Code is hidden, SHOW

Output (first 10 lines):
  1. FibonacciSequence(2): 0,1,1
  2. FibonacciSequence(3): 0,1,1,2
  3. FibonacciSequence(4): 0,1,1,2,3
  4. FibonacciSequence(5): 0,1,1,2,3,5
  5. FibonacciSequence(6): 0,1,1,2,3,5,8
  6. FibonacciSequence(7): 0,1,1,2,3,5,8,13
  7. FibonacciSequence(8): 0,1,1,2,3,5,8,13,21
  8. FibonacciSequence(9): 0,1,1,2,3,5,8,13,21,34
  9. FibonacciSequence(10): 0,1,1,2,3,5,8,13,21,34,55
  10. FibonacciSequence(11): 0,1,1,2,3,5,8,13,21,34,55,89
  11. 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
Apprentice
LTD Bronze - Rating: 59LTD Bronze - Rating: 59
 
Posts: 22
Joined: Fri Aug 08, 2008 12:45 pm
Location: Bratislava, Slovakia