Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
20 changes: 20 additions & 0 deletions math/prime.jule
Original file line number Diff line number Diff line change
@@ -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
}
15 changes: 15 additions & 0 deletions math/prime_test.jule
Original file line number Diff line number Diff line change
@@ -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")
}
Loading