Created
June 24, 2013 17:10
-
-
Save tlands/5851735 to your computer and use it in GitHub Desktop.
Fibonacci numbers : True or False
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def is_fibonacci?(i) | |
| a = 0 | |
| b = 1 | |
| while true | |
| c = a + b | |
| return true if c == i # the more I look at this... it's a little iffy and should be redone. | |
| return false if c > i | |
| a,b = b, c # eval right assign left...'b' (r) is assigned to 'a'(l), 'c'(r) is assigned to 'b'(l) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here are things that should smell funny to you:
while(true)loop in a situation where you know beforehand when the loop will terminate, e.g., you might use awhile(true)loop while reading and parsing user input because you don't know beforehand when a user will type "exit"returnin the middle of a methodreturnin the middle of a loopcHere's a suggested refactoring:
or equivalently (some folks might argue this reads better but YMMV — they're equivalent):