-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfib.rs
More file actions
36 lines (31 loc) · 666 Bytes
/
fib.rs
File metadata and controls
36 lines (31 loc) · 666 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
extern crate time;
use std::os;
use time::precise_time_ns;
fn fib_rec(qianVal:i32,zuoVal:i32,rem:i32)->i32{
if rem == 0{
zuoVal
}else{
fib_rec(zuoVal,qianVal+zuoVal,rem-1)
}
}
fn fib(x:i32)->i32{
if x < 2{
x
}else{
fib_rec(0,1,x-1)
}
}
fn do_work(acc:i32,rem:i32)->i32{
if rem == 0 {
acc
}else{
do_work(acc + fib((acc+1) % 50), rem-1)
}
}
fn main() {
let n : i32 = from_str(os::args().get(1).as_slice()).expect("First argument must be an i32");
let startTime = precise_time_ns();
println!("{}",do_work(0,n));
let duration = (precise_time_ns() - startTime) / 1000000;
println!("LANGUAGE Rust {}", duration);
}