Last active
December 22, 2015 09:58
-
-
Save MuffinTheMan/6455207 to your computer and use it in GitHub Desktop.
Check if a list contains a fibonacci sequence in Io.
This file contains 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
fibonacci := method(num, | |
if (num < 0, return "oops...negative number") | |
if (num == 0, return 0) | |
if (num == 1, return 1) | |
if (num == 2, return 1) | |
return fibonacci(num - 1) + fibonacci(num - 2) | |
) | |
isFibonacci := method(num, | |
if (num < 0, return false) | |
for (i, 0, 20, | |
fib := fibonacci(i) | |
if (num == fib, return true) | |
if (num < fib, return false) | |
) | |
) | |
List isFib := method( | |
if (self sort != self, return false) | |
foreach(i, | |
if (isFibonacci(i),continue,return false) | |
) | |
return true | |
) | |
a := list(9, 2, 3) | |
if (a isFib, (a .. " is a part of a fibonacci sequence.") println, (a .. " is NOT part of a fibonacci sequence.") println) | |
b := list(1, 2, 3) | |
if (b isFib, (b .. " is a part of a fibonacci sequence.") println, (b .. " is NOT part of a fibonacci sequence.") println) | |
c := list(5, 8, 13) | |
if (c isFib, (c .. " is a part of a fibonacci sequence.") println, (c .. " is NOT part of a fibonacci sequence.") println) | |
d := list(5, 98, 13) | |
if (d isFib, (d .. " is a part of a fibonacci sequence.") println, (d .. " is NOT part of a fibonacci sequence.") println) | |
e := list(5, 8, 13, 14) | |
if (e isFib, (e .. " is a part of a fibonacci sequence.") println, (e .. " is NOT part of a fibonacci sequence.") println) | |
f := list(5, 8, 13, 2) | |
if (f isFib, (f .. " is a part of a fibonacci sequence.") println, (f .. " is NOT part of a fibonacci sequence.") println) | |
g := list("blah", 8, 13) | |
if (g isFib, (g .. " is a part of a fibonacci sequence.") println, (g .. " is NOT part of a fibonacci sequence.") println) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment