fibo
Full source code
C++
// -*- mode: c++ -*-
// $Id$
// http://www.bagley.org/~doug/shootout/
#include <iostream>
#include <stdlib.h>
using namespace std;
unsigned long fib(unsigned long n) {
if (n < 2)
return(1);
else
return(fib(n-2) + fib(n-1));
}
int main(int argc, char *argv[]) {
#ifdef SMALL_PROBLEM_SIZE
#define LENGTH 40
#else
#define LENGTH 43
#endif
int n = ((argc == 2) ? atoi(argv[1]) : LENGTH);
cout << fib(n) << endl;
return(0);
}
Rust
// Adapted from https://github.com/llvm/llvm-test-suite and // http://www.bagley.org/~doug/shootout/ use std::env; #[cfg(feature = "small_problem_size")] const LENGTH: i32 = 40; #[cfg(not(feature = "small_problem_size"))] const LENGTH: i32 = 43; fn fib(n: u64) -> u64 { if n < 2 { return 1; } else { return fib(n - 2) + fib(n - 1); } } fn main() { let mut args = env::args(); let n = if args.len() == 2 { args.nth(1).unwrap().parse::<i32>().unwrap() } else { LENGTH }; println!("{}", fib(n as u64)); }
Porting notes
None.