Skip to main content

Zeckendorf’s Theorem

The problem are presented centers around Zeckendorf's Theorem, a fascinating mathematical concept that involves representing a given positive integer as a sum of distinct Fibonacci numbers. The theorem offers an intriguing way to express a number using a unique set of Fibonacci numbers. Your Java code demonstrates the implementation of Zeckendorf's Theorem to find this unique representation.

Problem Description

Zeckendorf's Theorem states that any positive integer can be represented as a sum of non-consecutive Fibonacci numbers (Fibonacci sequence starts with 0 and 1, and each subsequent number is the sum of the two preceding ones). The goal is to find the representation with the fewest terms.

Example

Consider the example of finding the Zeckendorf representation of 64:

  • The Fibonacci sequence starts with 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...
  • The largest Fibonacci number that is less than or equal to 64 is 55.
  • After subtracting 55 from 64, we are left with 9.
  • The largest Fibonacci number less than 9 is 8.
  • After subtracting 8 from 9, we are left with 1.
  • The largest Fibonacci number less than 1 is 1.
  • After subtracting 1 from 1, we are left with 0.

So, the Zeckendorf representation of 64 is 55 + 8 + 1.

Idea to Solve

To implement Zeckendorf's Theorem, follow these steps:

  1. Initialize a loop to find the largest Fibonacci number (Fibonacci term) less than or equal to the given number.

  2. Starting from the highest Fibonacci term, subtract it from the given number. Add the term to the Zeckendorf representation.

  3. Repeat steps 1 and 2 until the given number becomes 0.

Pseudocode

function fibNumber(n: int) -> int:
    a = 0
    b = 1
    c = 1
    while c <= n:
        a = b
        b = c
        c = a + b
    return b

procedure zeckendorfTheorem(n: int):
    if n <= 0:
        return
    
    v = 0
    print("Given n:", n)
    print("Result:", end = " ")
    
    while n > 0:
        v = fibNumber(n)
        print(v, end = " ")
        n = n - v

Algorithm Explanation

  1. The fibNumber function finds the largest Fibonacci number that is less than or equal to a given number n. It iterates through the Fibonacci sequence to find the appropriate term.

  2. The zeckendorfTheorem procedure takes an integer n and implements Zeckendorf's Theorem. It iterates through the process of finding the largest Fibonacci term less than or equal to n, subtracting it from n, and adding it to the representation.

Code Solution

Time Complexity

The fibNumber function iterates through the Fibonacci sequence until it finds the largest term less than or equal to n. This takes O(log n) time due to the exponential growth of Fibonacci numbers. The zeckendorfTheorem procedure loops through this process, so the time complexity is also O(log n).





Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment