r/dailyprogrammer • u/oskar_s • Jul 09 '12
[7/9/2012] Challenge #74 [easy]
The Fibonacci numbers, which we are all familiar with, start like this:
0,1,1,2,3,5,8,13,21,34,...Where each new number in the sequence is the sum of the previous two.
It turns out that by summing different Fibonacci numbers with each other, you can create every single positive integer. In fact, a much stronger statement holds:
Every single positive integer can be represented in one and only one way as a sum of non-consecutive Fibonacci numbers. This is called the number's "Zeckendorf representation".
For instance, the Zeckendorf representation of the number 100 is 89 + 8 + 3, and the Zeckendorf representation of 1234 is 987 + 233 + 13 + 1. Note that all these numbers are Fibonacci numbers, and that they are non-consecutive (i.e. no two numbers in a Zeckendorf representation can be next to each other in the Fibonacci sequence).
There are other ways of summing Fibonacci numbers to get these numbers. For instance, 100 is also equal to 89 + 5 + 3 + 2 + 1, but 1, 2, 3, 5 are all consecutive Fibonacci numbers. If no consecutive Fibonacci numbers are allowed, the representation is unique.
Finding the Zeckendorf representation is actually not very hard. Lets use the number 100 as an example of how it's done:
First, you find the largest fibonacci number less than or equal to 100. In this case that is 89. This number will always be of the representation, so we remember that number and proceed recursively, and figure out the representation of 100 - 89 = 11.
The largest Fibonacci number less than or equal to 11 is 8. We remember that number and proceed recursively with 11 - 8 = 3.
3 is a Fibonacci number itself, so now we're done. The answer is 89 + 8 + 3.
Write a program that finds the Zeckendorf representation of different numbers.
What is the Zeckendorf representation of 315 ?
- Thanks to SwimmingPastaDevil for suggesting this problem in /r/dailyprogrammer_ideas! Do you have a problem you think would be good for us? Why not head over there and post it?
2
u/SwimmingPastaDevil 0 0 Jul 09 '12
Output:
Strangely, its quite fast. Computes under 0.1s for 10100
14
u/Fustrate 0 0 Jul 09 '12 edited Jul 09 '12
Python:
Output:
2
u/zane17 Jul 10 '12
3 can be represented 2 ways, is that statement incorrect?
0,1,1,2,3,5,8,13,21,34,...
0,1,1,2,3,5,8,13,21,34,...
4
u/sleepingsquirrel Jul 09 '12 edited Jul 09 '12
Python:
Results:
OK, that implementation isn't 100% accurate, since it is afflicted by floating point round-off error, but I wanted to throw out a non-recursive/non-iterative solution. Maybe someone has a better closed-form analytic solution? You lose a lot of precision with the logarithm.
Updated with a better formula for the inverse Fibonacci, eliminating the fudge factors.
4
u/Gix Jul 09 '12 edited Mar 05 '19
[Deleted]
1
u/aimlessdrive Jul 10 '12
C++: I think I can optimize the algorithm if I made a separate container for storing the Zeckendorf. What bugs me more is that I don't know how I could make this handle big numbers like the 10100 that some of you are testing. As always, criticism/comment is welcome.
3
u/H4ggis Jul 09 '12
Haskell:
Gives:
2
u/ixid 0 0 Jul 09 '12 edited Jul 10 '12
In D:
Solves 10100 in 26 milliseconds.
This version takes ~2.8 milliseconds, including fibonnaci sequence building (not shown) up to the first term greater than than the target number.
2
u/DAVENP0RT Jul 09 '12
C#
2
u/appleade280 Jul 09 '12
In D:
2
u/Koldof 0 0 Jul 09 '12
Python:
2
u/cougarpoop Jul 10 '12
Java
2
u/snatchinyopeopleup Jul 10 '12 edited Jul 10 '12
VBA (hey, i use it a lot!)
2
u/[deleted] Jul 10 '12 edited Jul 10 '12
Labview
I don't have access to any of my normal compilers or interpreters here at work. But I did have access to Labview and thought, "Eh... Why not?"
Surprisingly, it gives < 1 ms of timing for even the highest 32-bit number.
2
u/[deleted] Jul 10 '12
z80. This only works for really small numbers but I'm bad at ASM.
Disassembled (OUTPUT and FIBS are $0000 and $0100 above):
Translated to C: http://codepad.org/c68p2u7N (using 50 as an example input)
2
u/Scroph 0 0 Jul 11 '12 edited Jul 11 '12
My D solution :
2
u/taterNuts Jul 17 '12 edited Jul 17 '12
A little late to the game, but decided to give it a shot since I haven't worked with recursion yet.
C#:
I know I should generate the fib sequence out past the input value but I got lazy
3
u/SlamminAtWork Jul 09 '12
Common Lisp:
1
u/liam_jm Jul 09 '12
Python:
Output:
1
u/emcoffey3 0 0 Jul 09 '12
Output:
1
u/[deleted] Jul 09 '12
C++, this ones a little messy too, I'm also doing more work than I have to, recomputing the fib sequence over and over:
Output:
1
u/[deleted] Jul 09 '12
Javascript (node.js)
1
u/02471 Jul 09 '12 edited Jul 29 '12
In C; instantaneous. #include <stdio.h>
Output:
1
u/[deleted] Jul 10 '12
Yet another Python solution, from someone who doesn't actually know Python:
Which should output
1
u/bh3 Jul 10 '12 edited Jul 10 '12
Python:
1
u/rickster88 Jul 10 '12
Ruby:
1
u/scurvebeard 0 0 Jul 10 '12 edited Jul 10 '12
My first successful entry (as far as I can tell) in this subreddit. I'm still learning the basics of Python via Udacity so things are obviously pretty rough as I don't know that many terms, but I think this works. I used to be really annoyed constructing Fibonnaci sequences until I learned about simultaneous assignment. That makes this code a lot easier to read.
I'm calling it 14 lines. I'm sure that could be simplified without even changing the process, but it's late.
1
u/tashiwwww 0 0 Jul 10 '12 edited Jul 10 '12
My sad sloppy python solution:
Output:
1
u/CouponTheMovie Jul 10 '12
Javascript:
1
u/fancysuit Jul 10 '12
Perl:
I'm brand new to Perl, so feedback is very welcomed.
1
u/flowblok Jul 11 '12
My somewhat longish Python solution:
1
u/semicolondash Jul 11 '12 edited Jul 11 '12
C++ (it's my first C++ program ever haha, so I'm still adjusting to the language)
I tried to make it as efficient as possible. Criticisms by those more experienced are always appreciated. And I realize that the empty for loop is a bit silly, but it gets the job done well and efficiently.
1
u/[deleted] Jul 11 '12
First post. I'm a beginner so any pointers would be great! C:
1
u/zoidbergLOL Jul 12 '12
New to Ruby Pastebin
1
u/T_D_K Jul 12 '12
This is the first challenge I've attempted, and I'm a novice, so I'm sure my code is a bit rough around the edges. I'd love any feedback :)
Output for zeck(3**15):
1
u/[deleted] Jul 12 '12
Java:
}
Output:
The Zeckendorf representation for 14348907 is: 9227465 + 3524578 + 1346269 + 196418 + 46368 + 6765 + 987 + 55 + 2
1
u/5outh 1 0 Jul 12 '12
I chose to actually output the result as a readable string, lol (also, first post here!)
zeckendorf (315) outputs:
1
u/refringence Jul 12 '12
Ruby:
1
u/rsubasic Jul 14 '12 edited Nov 24 '17
MUMPS (Actually Caché Object Script -- a super-set of MUMPS):
Output:
89, 8, 3
9227465, 3524578, 1346269, 196418, 46368, 6765, 987, 55, 2
1
u/ademus4 Jul 23 '12
Python:
OK not the cleanest, took me abut 30 mins.
1
u/Amndeep7 Jul 26 '12 edited Jul 26 '12
Edited Java version that now works for both smaller values (within the size of an int) and extremely large values:
1
u/cdelahousse Jul 29 '12
Javascript
1
u/reallyhoopyfrood Sep 05 '12
I know I'm very late, but I think the most concise python yet: