Created
October 18, 2019 03:35
-
-
Save JosephTLyons/343d337296bb03ffa6881471ea52d412 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for helping the lazy 🙂