zig/test/cases/recursive_fibonacci.zig

25 lines
412 B
Zig
Raw Normal View History

2022-04-27 23:09:26 +01:00
pub fn main() void {
assert(fib(0) == 0);
assert(fib(1) == 1);
assert(fib(2) == 1);
assert(fib(3) == 2);
assert(fib(10) == 55);
assert(fib(20) == 6765);
}
fn fib(n: u32) u32 {
if (n < 2) {
return n;
} else {
return fib(n - 2) + fib(n - 1);
}
}
fn assert(ok: bool) void {
if (!ok) unreachable;
}
// run
2022-05-14 21:25:04 +01:00
// target=x86_64-linux,x86_64-macos,wasm32-wasi
2022-04-27 23:09:26 +01:00
//