Skip to content

Commit 07c7ec2

Browse files
authored
math: add PrimeCheck (#48)
1 parent e08aa58 commit 07c7ec2

3 files changed

Lines changed: 36 additions & 0 deletions

File tree

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- [Max](math/max.jule)
88
- [Median](math/median.jule)
99
- [Min](math/min.jule)
10+
- [Prime](math/prime.jule)
1011
- [Q Rsqrt](math/q_rsqrt.jule)
1112
- [Sum](math/sum.jule)
1213

math/prime.jule

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
fn CheckPrime(n: int): bool {
2+
if n <= 1 {
3+
return false
4+
}
5+
if n == 2 {
6+
return true
7+
}
8+
if n%2 == 0 {
9+
return false
10+
}
11+
12+
let mut i = 3
13+
for i*i <= n {
14+
if n%i == 0 {
15+
return false
16+
}
17+
i += 2
18+
}
19+
return true
20+
}

math/prime_test.jule

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#build test
2+
3+
use "std/testing"
4+
5+
#test
6+
fn testCheckPrime(t: &testing::T) {
7+
t.Assert(!CheckPrime(-7), "-7 should not be prime")
8+
t.Assert(!CheckPrime(0), "0 should not be prime")
9+
t.Assert(!CheckPrime(1), "1 should not be prime")
10+
t.Assert(CheckPrime(2), "2 should be prime")
11+
t.Assert(CheckPrime(3), "3 should be prime")
12+
t.Assert(!CheckPrime(4), "4 should not be prime")
13+
t.Assert(CheckPrime(97), "97 should be prime")
14+
t.Assert(!CheckPrime(100), "100 should not be prime")
15+
}

0 commit comments

Comments
 (0)