23
5
Background
Most of you know what a Fibonacci number is. Some of you may know that all positive integers can be represented as a sum of one or more distinct Fibonacci numbers, according to Zeckendorf's Theorem. If the number of terms in the optimal Zeckendorf Representation of an integer n
is itself a Fibonacci number, we will call n
"secretly" Fibonacci.
For example:
139 = 89 + 34 + 13 + 3
This is a total of 4 integers. Since 4 is not a Fibonacci number, 139 is not secretly Fibonacci
140 = 89 + 34 + 13 + 3 + 1
This is a total of 5 integers. Since 5 is a Fibonacci number, 140 is secretly Fibonacci
Notes
- The optimal Zeckendorf Representation can be found using a greedy algorithm. Simply take the largest Fibonacci number <= n and subtract it from n until you reach 0
- All Fibonacci numbers can be represented as a sum of 1 Fibonacci number (itself). Since 1 is a Fibonacci number, all Fibonacci numbers are also secretly Fibonacci.
Challenge
Your challenge is to write a program or function that takes an integer and returns whether that integer is secretly Fibonacci.
Input
You may take input in any reasonable format. You may assume the input will be a single positive integer.
Output
Output one of two distinct results for whether the input is secretly Fibonacci. Examples include True
/False
, 1
/0
, etc.
Scoring
This is code-golf, so shortest answer in bytes wins! Standard loopholes are forbidden.
Test Cases
Truthy (secretly Fibonacci)
1
2
4
50
140
300099
Falsey (NOT secretly Fibonacci)
33
53
54
139
118808
6Does this mean they're fi-curious? – kevin – 2018-10-30T09:09:36.483
2In case it is of use to anybody: The optimal sum is the unique solution which does not utilize two consecutive Fibonacci numbers. – kasperd – 2018-10-30T09:13:09.767
2@kasperd You're correct, which makes sense if you think about it. If the solution had two consecutive Fibonacci numbers, they could be added together to form the next one. If your solution contained 5 and 8, it would be less optimal than having a single 13. – Cowabunghole – 2018-10-31T13:46:04.113
@Cowabunghole That's the intuition. A full proof is a bit more complicated than that. If the solution already contained 5, 8, and 13 you'd add 8+13 not 5+8. And the other direction needs to be proven as well. – kasperd – 2018-10-31T14:05:02.180