MCPcopy Create free account
hub / github.com/douchuan/algorithm / fib_classic_iteration_loop

Function fib_classic_iteration_loop

src/dp/fib.rs:36–56  ·  view source on GitHub ↗
(n: usize)

Source from the content-addressed store, hash-verified

34/// 只保存前两个值,最节省内存和最快的方式
35#[allow(unused)]
36pub fn fib_classic_iteration_loop(n: usize) -> usize {
37 match n {
38 0 => 0,
39 1 | 2 => 1,
40 _ => {
41 let mut prev = 1;
42 let mut cur = 1;
43 let mut i = 3;
44 loop {
45 let sum = prev + cur;
46 prev = cur;
47 cur = sum;
48
49 i += 1;
50 if i > n {
51 return cur;
52 }
53 }
54 }
55 }
56}
57
58/// 只保存前两个值,最节省内存和最快的方式
59/// 但实际基准测试结果并没有预期的那么快, 有可能是for的原因

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected