OSDN Git Service

buggy bif_img, but it works if you're careful and don't mind things sliding a line...
[bif-6809/bif-6809.git] / testsource / rs_sieve_bif.fs
diff --git a/testsource/rs_sieve_bif.fs b/testsource/rs_sieve_bif.fs
new file mode 100644 (file)
index 0000000..41e0895
--- /dev/null
@@ -0,0 +1,60 @@
+( from rosetta code )
+: prime? ( n -- ? )
+  HERE + C@ 0= ;
+
+: composite! ( n -- )
+  HERE + 1 SWAP C! ;
+
+( : 2dup OVER OVER ; )
+
+: showPrimes
+  ." Primes: "
+  2 DO I prime?
+    IF I . ENDIF
+  LOOP ;
+
+: countPrimes
+  ." Prime count: "
+  0 SWAP
+  2 DO I prime?
+    IF 1+ ENDIF
+  LOOP
+  . ;
+
+-->
+
+
+
+
+
+
+
+
+
+: sieve ( n -- )
+  HERE OVER ERASE
+  2
+  BEGIN
+    2dup DUP * >
+  WHILE
+    DUP prime? IF
+      2dup DUP * DO
+        I composite!
+      DUP +LOOP
+    ENDIF
+    1+
+  REPEAT
+  DROP
+  ;
+
+
+100 sieve
+
+dup 
+
+showPrimes
+
+countPrimes
+
+
+