summaryrefslogtreecommitdiff
path: root/prime/prime.scm
diff options
context:
space:
mode:
authorbd <bdunahu@operationnull.com>2024-05-18 20:55:32 -0600
committerbd <bdunahu@operationnull.com>2024-05-18 21:09:26 -0600
commitb2b79ad10e1d7559f95db18af1a47f4725272aa6 (patch)
treeb710440a1de001956554883a5c760c6980efb404 /prime/prime.scm
parent0e0ab335087338d208eb3d1780e2de2c4ad0fc08 (diff)
Add prime? code and tests
Diffstat (limited to 'prime/prime.scm')
-rwxr-xr-xprime/prime.scm17
1 files changed, 17 insertions, 0 deletions
diff --git a/prime/prime.scm b/prime/prime.scm
new file mode 100755
index 0000000..e12dfb4
--- /dev/null
+++ b/prime/prime.scm
@@ -0,0 +1,17 @@
+(define-module (prime))
+
+
+(define-public (prime? n)
+ "Returns #t if N is prime, else #f."
+ (if (< n 2)
+ #f
+ (prime-helper n (quotient n 2))))
+
+(define (prime-helper n d)
+ "Recursively checks if N is a prime number
+by attempting division by number D,D-1,D-2..."
+ (if (< d 2)
+ #t
+ (if (equal? (remainder n d) 0)
+ #f
+ (prime-helper n (- d 1)))))