Created
January 17, 2014 00:37
-
-
Save brianseitel/8466308 to your computer and use it in GitHub Desktop.
Fibonacci implementation in Rust
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
fn main() { | |
let max: uint = 25; | |
let results = fibonacci(max, ~[]); | |
for i in results.iter() { | |
println!("{}", i.to_str()); | |
} | |
} | |
fn fibonacci(max: uint, mut list: ~[int]) -> ~[int] { | |
if list.len() == 0 { | |
list = ~[0,1]; | |
} else if list.len() == max { | |
return list; | |
} | |
// Get the last two items in the list | |
let f1 = *list.iter().nth(list.len() - 1).unwrap(); | |
let f2 = *list.iter().nth(list.len() - 2).unwrap(); | |
// Add them together, then add that result to the list | |
list.push(f1 + f2); | |
// Fibonacci-ize the list again! | |
return fibonacci(max, list); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment