How to resolve the algorithm Zeckendorf number representation step by step in the Scala programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Zeckendorf number representation step by step in the Scala programming language

Table of Contents

Problem Statement

Just as numbers can be represented in a positional notation as sums of multiples of the powers of ten (decimal) or two (binary); all the positive integers can be represented as the sum of one or zero times the distinct members of the Fibonacci series. Recall that the first six distinct Fibonacci numbers are: 1, 2, 3, 5, 8, 13. The decimal number eleven can be written as 013 + 18 + 05 + 13 + 02 + 01 or 010100 in positional notation where the columns represent multiplication by a particular member of the sequence. Leading zeroes are dropped so that 11 decimal becomes 10100. 10100 is not the only way to make 11 from the Fibonacci numbers however; 013 + 18 + 05 + 03 + 12 + 11 or 010011 would also represent decimal 11. For a true Zeckendorf number there is the added restriction that no two consecutive Fibonacci numbers can be used which leads to the former unique solution.

Generate and show here a table of the Zeckendorf number representations of the decimal numbers zero to twenty, in order.
The intention in this task to find the Zeckendorf form of an arbitrary integer. The Zeckendorf form can be iterated by some bit twiddling rather than calculating each value separately but leave that to another separate task.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Zeckendorf number representation step by step in the Scala programming language

Source code in the scala programming language

def zNum( n:BigInt ) : String = {

  if( n == 0 ) return "0"	// Short-circuit this and return zero if we were given zero


  val v = n.abs

  val fibs : Stream[BigInt] = { def series(i:BigInt,j:BigInt):Stream[BigInt] = i #:: series(j, i+j); series(1,0).tail.tail.tail }


  def z( v:BigInt ) : List[BigInt] = if(v == 0) List() else {val m = fibs(fibs.indexWhere(_>v) - 1); m :: z(v-m)}

  val zv = z(v)
  
  // Walk the list of fibonacci numbers from the number that matches the most significant down to 1,
  // if the zeckendorf matchs then yield '1' otherwise '0'
  val s = (for( i <- (fibs.indexWhere(_==zv(0)) to 0 by -1) ) yield {
  
    if( zv.contains(fibs(i))) "1" else "0"
	
  }).mkString
  
  if( n < 0 ) "-" + s		// Using a negative-sign instead of twos-complement 
  else s
}


// A little test...
(0 to 20) foreach( i => print( zNum(i) + "\n" ) )


  

You may also check:How to resolve the algorithm Ranking methods step by step in the D programming language
You may also check:How to resolve the algorithm Maze generation step by step in the Go programming language
You may also check:How to resolve the algorithm Split a character string based on change of character step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Fibonacci word step by step in the jq programming language
You may also check:How to resolve the algorithm Rot-13 step by step in the 6502 Assembly programming language