Skip to content

Commit 1c51061

Browse files
committed
feat(math): adding prime? to determine if a number is prime
1 parent c3d9c40 commit 1c51061

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Math.ark

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,25 @@
6262
(impl (- n 1) c (+ p c))))))
6363
(impl n 0 1) }))
6464

65+
# @brief Check if a given number is prime
66+
# @param n the number
67+
# @author https://github.com/SuperFola
68+
(let prime? (fun (n)
69+
(if (= 2 n)
70+
true
71+
(if (or (= 0 (mod n 2)) (= 1 n))
72+
false
73+
{
74+
(let k (math:ceil (+ 1 (sqrt n))))
75+
(mut i 3)
76+
(mut continue true)
77+
78+
(while (and continue (< i k)) {
79+
(if (= 0 (mod n i))
80+
(set continue false))
81+
(set i (+ 2 i)) })
82+
continue }))))
83+
6584
# @brief Returns the list of a number's divisors
6685
# @param n the number
6786
# @author https://github.com/Wafelack

0 commit comments

Comments
 (0)