summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Hall <hallmatthew314@gmail.com>2023-02-22 22:43:54 +1300
committerMatthew Hall <hallmatthew314@gmail.com>2023-02-22 22:43:54 +1300
commit06692c8e1754ac8d5d671160b839723e3610fcf1 (patch)
treeeaf67b37260d800c7cff06ef099cbb3640b6cc37
parentbad32a12573bf14968746ea9ad0f6c4f20b50cf1 (diff)
new example: prime factors
-rw-r--r--examples/factors-of-n.dsl19
1 files changed, 19 insertions, 0 deletions
diff --git a/examples/factors-of-n.dsl b/examples/factors-of-n.dsl
new file mode 100644
index 0000000..a5ef2fc
--- /dev/null
+++ b/examples/factors-of-n.dsl
@@ -0,0 +1,19 @@
+''' recursively pushes the prime factors of the int on the top of the stack'''
+PROC factors
+ ''' only proceed if the input is valid '''
+ IF DUP 1 < DO
+ ''' find the smallest number that divides n '''
+ 2 WHILE OVER OVER SWAP % 0 != DO 1 + END
+ ''' print it and find the next factor '''
+ DUP .
+ OVER OVER SWAP / factors
+ END
+END
+
+PROC MAIN
+ "Prime factors of 12:" . 12 factors
+ "Prime factors of 5:" . 5 factors
+ "Prime factors of 60:" . 60 factors
+ "Prime factors of 97:" . 97 factors
+END
+