cl-interpret: Give the interpreter a little love
And stop copying strings around.
This commit is contained in:
@@ -1,16 +1,29 @@
|
||||
// Calculate Fibonacci numbers
|
||||
|
||||
fn main() {
|
||||
for num in 0..=30 {
|
||||
print("fib(", num, ") = ", fib_iterative(num))
|
||||
}
|
||||
for num in 0..=30 {
|
||||
print("fib(", num, ") = ", fib(num))
|
||||
}
|
||||
}
|
||||
|
||||
/// Implements the classic recursive definition of fib()
|
||||
/// The classic recursive definition of fib()
|
||||
fn fib(a: i64) -> i64 {
|
||||
if a > 1 {
|
||||
fib(a - 1) + fib(a - 2)
|
||||
} else {
|
||||
1
|
||||
}
|
||||
} else a
|
||||
}
|
||||
|
||||
/// The classic iterative algorithm for fib()
|
||||
fn fib_iterative(n: i64) -> i64 {
|
||||
let mut a = 0;
|
||||
let mut b = 1;
|
||||
let mut c = 1;
|
||||
for _ in 0..n {
|
||||
a = b;
|
||||
b = c;
|
||||
c = a + b;
|
||||
} else a
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user