

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program for Zeckendorf's Theorem?
Here we will see how to check whether the given sum is found by adding some nonneighbouring Fibonacci numbers or not, if so, what are the numbers? For example if the give sum value is 10, this is sum of 8 and 2. Both 8 and 2 are Fibonacci terms and they are not adjacent. Let us see the algorithm to get the idea.
Algorithm
nonNeighbourFibo(sum)
Begin while sum > 0, do fibo := greatest Fibonacci term but not greater than sum print fibo sum := sum - fibo done End
Example
#include<iostream> using namespace std; int fibonacci(int n) { if (n == 0 || n == 1) return n; // get the greatest Fibonacci Number smaller than n. int prev = 0, curr = 1, next = 1; while (next <= n) { prev = curr; curr = next; next = prev + curr; } return curr; } void nonNeighbourFibo(int sum) { while (sum > 0) { int fibo = fibonacci(sum); cout << fibo << " "; sum = sum - fibo; } } int main() { int sum = 120; cout << "Sum is same as Non-adjacent Fibonacci terms: "; nonNeighbourFibo(sum); }
Output
Sum is same as Non-adjacent Fibonacci terms: 89 21 8 2
- Related Questions & Answers
- Kirchoff's Theorem
- C++ Program to Implement Fermat’s Little Theorem
- C++ Program to Implement the Vizing’s Theorem
- Midy’s theorem in C++
- Construct RE for given finite automata with Arden’s theorem
- Signals and Systems – Parseval’s Theorem for Laplace Transform
- Extended Midy's theorem in C++
- Fermat's Last Theorem in C++
- Fermat's little theorem in C++
- Explain Arden’s Theorem in TOC.
- Lagrange’s four square theorem in C++
- What is Kleene’s Theorem in TOC?
- Thevenin’s Theorem and Thevenin Equivalent Circuit
- Parseval’s Theorem & Parseval’s Identity of Fourier Transform
- Parseval’s Theorem in Continuous-Time Fourier Series
Advertisements