Draft

Zeckendorf Cards Generator

Description:

#Source

This kata follows the application of the Zeckendorf magic trick presented here: http://www.codewars.com/kata/magic-zeckendorf. If you haven't completed the first one I strongly suggest you to do it first (it's pretty easy).


#Objective

In the previous kata, the cards used to find the number were given. These cards could find any number between 0 and 100. Your objective is to write a function cardZ(n) that can generate the cards used to guess a number between 0 and n. In coding term the function cardZ(n) should return an array of array. The list of "card" (one card is an array) returned should be in ascending order (e.g. based on the first number of the "card").

##Example: The cards to find a number between 0 and 15 (with the correct index) are:

cardZ(15) === 
[
  [1,4,6,9,12,14],
  [2,7,10,15],
  [3,4,11,12],
  [5,6,7],
  [8,9,10,11,12],
  [13,14,15]
]

#Method It may exist many method to do it but one of the simpliest one is to use a Fibonnacy representation of the numbers. Any number can be represented in a Fibonnacy/Zeckendorf representation (http://en.wikipedia.org/wiki/Fibonacci_coding) :

To encode an integer N:

  1. Find the largest Fibonacci number equal to or less than N; subtract this number from N, keeping track of the remainder.
  2. If the number subtracted was the ith Fibonacci number F(i), put a 1 in place i in the code word (counting the right most digit as place 0).
  3. Repeat the previous steps, substituting the remainder for N, until a remainder of 0 is reached.

Example with 10:

Fib = [1,2,3,5,8,13]
                                 |8|7|6|5|4|3|2|1|0|
First let's fill with zero: 10 ~= 0 0 0 0 0 0 0 0 0
Then closest Fibonacci number equal or smaller to 10 is 8. 8 === F(4) so
     |9|8|7|6|5|4|3|2|1|
10 ~= 0 0 0 0 0 1 0 0 0
Now 10 - 8 === 2
2 is a Fibonacci number 2 === F(1) then
     |8|7|6|5|4|3|2|1|0|
10 == 0 0 0 0 1 0 0 1 0
2 - 2 === 0 so we are finished:
      |4|3|2|1|0|
10 === 1 0 0 1 0

Based on this representation we know that the number 10 would be on the card index number 1 and 4. You just need to apply this method to all numbers between 0 and n.

Good Luck, Don't forget to up vote if you like, thanks

IVBakker

Mathematics
Binary

Stats:

CreatedDec 19, 2014
Warriors Trained21
Total Skips0
Total Code Submissions34
Total Times Completed5
JavaScript Completions5
Total Stars0
% of votes with a positive feedback rating0% of 0
Total "Very Satisfied" Votes0
Total "Somewhat Satisfied" Votes0
Total "Not Satisfied" Votes2
Ad
Contributors
  • IVBakker Avatar
Ad