diff --git a/DIRECTORY.md b/DIRECTORY.md index 8cdfb96..48983d6 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -7,6 +7,7 @@ - [Max](math/max.jule) - [Median](math/median.jule) - [Min](math/min.jule) +- [Prime](math/prime.jule) - [Q Rsqrt](math/q_rsqrt.jule) - [Sum](math/sum.jule) diff --git a/math/prime.jule b/math/prime.jule new file mode 100644 index 0000000..a03fde9 --- /dev/null +++ b/math/prime.jule @@ -0,0 +1,20 @@ +fn CheckPrime(n: int): bool { + if n <= 1 { + return false + } + if n == 2 { + return true + } + if n%2 == 0 { + return false + } + + let mut i = 3 + for i*i <= n { + if n%i == 0 { + return false + } + i += 2 + } + return true +} \ No newline at end of file diff --git a/math/prime_test.jule b/math/prime_test.jule new file mode 100644 index 0000000..c07ff22 --- /dev/null +++ b/math/prime_test.jule @@ -0,0 +1,15 @@ +#build test + +use "std/testing" + +#test +fn testCheckPrime(t: &testing::T) { + t.Assert(!CheckPrime(-7), "-7 should not be prime") + t.Assert(!CheckPrime(0), "0 should not be prime") + t.Assert(!CheckPrime(1), "1 should not be prime") + t.Assert(CheckPrime(2), "2 should be prime") + t.Assert(CheckPrime(3), "3 should be prime") + t.Assert(!CheckPrime(4), "4 should not be prime") + t.Assert(CheckPrime(97), "97 should be prime") + t.Assert(!CheckPrime(100), "100 should not be prime") +} \ No newline at end of file