Skip to content

Instantly share code, notes, and snippets.

@JosephTLyons
Created October 18, 2019 03:35
Show Gist options
  • Save JosephTLyons/343d337296bb03ffa6881471ea52d412 to your computer and use it in GitHub Desktop.
Save JosephTLyons/343d337296bb03ffa6881471ea52d412 to your computer and use it in GitHub Desktop.
const std = @import("std");
pub fn factorial(n: u32) u32 {
var result: u32 = 1;
var i: u32 = 1;
while (i <= n): (i += 1)
result *= i;
return result;
}
test "Test Factorial Base Case 1" {
std.debug.assert(factorial(0) == 1);
}
test "Test Factorial Base Case 2" {
std.debug.assert(factorial(1) == 1);
}
test "Test Factorial Normal Cases" {
std.debug.assert(factorial(2) == 2);
std.debug.assert(factorial(3) == 6);
std.debug.assert(factorial(4) == 24);
std.debug.assert(factorial(5) == 120);
std.debug.assert(factorial(6) == 720);
std.debug.assert(factorial(7) == 5040);
}
@JosephTLyons
Copy link
Author

No problem :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment