From 3f31d25152babb0e48bf25a4d5f289acfc0cb866 Mon Sep 17 00:00:00 2001 From: whiteand Date: Thu, 7 Oct 2021 00:35:22 +0300 Subject: [PATCH 1/3] docs: added example program that reads number from stdin and writes if it's a prime number or not --- examples/is-prime.porth | 102 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 examples/is-prime.porth diff --git a/examples/is-prime.porth b/examples/is-prime.porth new file mode 100644 index 00000000..254bf506 --- /dev/null +++ b/examples/is-prime.porth @@ -0,0 +1,102 @@ +include "std.porth" + +// Probably need to add to standard library. +// It's useful to have such definition of true, instead of +// `macro true 1 end` +// because it will allow appropriate typechecking +macro true + 0 0 = +end + +macro false + 0 0 != +end + +macro divisible + % 0 = +end + +// memory +// 0 => 11 bytes for chars of the number +// 11 => result number (8 bytes) +// 19 => + +macro CHAR_CAPACITY 11 end +macro CHARS_PTR mem end +macro DIGITS_LEN_PTR CHARS_PTR CHAR_CAPACITY + end +macro NUM_PTR DIGITS_LEN_PTR 1 + end +macro FREE_SPACE NUM_PTR 8 + end + +// digit on the stack +macro increase_digits_len + drop + DIGITS_LEN_PTR DIGITS_LEN_PTR , 1 + . +end + +macro digits_to_number + CHARS_PTR swap while dup 1 > do + swap dup , // read char + '0' - + // dig bool + dup 0 >= over 10 < band if + NUM_PTR ,64 10 * + NUM_PTR swap .64 + 1 + swap 1 - + else + drop + 1 + swap dup - + end + end drop drop +end + +// returns number read from stdint +macro read_number + NUM_PTR 0 .64 + DIGITS_LEN_PTR 0 .64 + CHAR_CAPACITY CHARS_PTR stdin read + dup 0 <= if + "ERROR: could not read your name, sorry ( ._.)\n" stderr write drop + 1 exit + end + digits_to_number + NUM_PTR ,64 +end + +// expects number +macro is_prime + dup 2 = over + 3 = bor over + 5 = bor + if + drop true + else + dup 2 divisible over 3 divisible bor over 5 divisible bor if + + drop false + else + true swap 5 while over over dup * >= do + // RES NUMBER POSSIBLE_DIVIDER + over over divisible if + 2drop drop false 0 1 + // false 0 1 + else + 2 + + end + over over dup * >= if + over over divisible if + 2drop drop false 0 1 + else + 6 + + end + end + end drop drop + end + end +end + +"Enter number?\n" stdout write drop + +read_number + +is_prime + +if "It's prime\n" else "It's not a prime\n" end stdout write drop \ No newline at end of file From cea1e361ba4f20ff36ff17af1fa2071ec5c268fa Mon Sep 17 00:00:00 2001 From: whiteand Date: Thu, 7 Oct 2021 00:39:30 +0300 Subject: [PATCH 2/3] removed unnecessary code --- examples/is-prime.porth | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/examples/is-prime.porth b/examples/is-prime.porth index 254bf506..3f94c3d4 100644 --- a/examples/is-prime.porth +++ b/examples/is-prime.porth @@ -12,26 +12,10 @@ macro false 0 0 != end -macro divisible - % 0 = -end - -// memory -// 0 => 11 bytes for chars of the number -// 11 => result number (8 bytes) -// 19 => macro CHAR_CAPACITY 11 end macro CHARS_PTR mem end -macro DIGITS_LEN_PTR CHARS_PTR CHAR_CAPACITY + end -macro NUM_PTR DIGITS_LEN_PTR 1 + end -macro FREE_SPACE NUM_PTR 8 + end - -// digit on the stack -macro increase_digits_len - drop - DIGITS_LEN_PTR DIGITS_LEN_PTR , 1 + . -end +macro NUM_PTR CHARS_PTR CHAR_CAPACITY + end macro digits_to_number CHARS_PTR swap while dup 1 > do @@ -51,7 +35,6 @@ end // returns number read from stdint macro read_number NUM_PTR 0 .64 - DIGITS_LEN_PTR 0 .64 CHAR_CAPACITY CHARS_PTR stdin read dup 0 <= if "ERROR: could not read your name, sorry ( ._.)\n" stderr write drop @@ -61,6 +44,11 @@ macro read_number NUM_PTR ,64 end +macro divisible + % 0 = +end + + // expects number macro is_prime dup 2 = over @@ -70,7 +58,6 @@ macro is_prime drop true else dup 2 divisible over 3 divisible bor over 5 divisible bor if - drop false else true swap 5 while over over dup * >= do @@ -99,4 +86,6 @@ read_number is_prime -if "It's prime\n" else "It's not a prime\n" end stdout write drop \ No newline at end of file +if "It's prime\n" else "It's not a prime\n" end stdout write drop + +// This algorithm works with O(sqrt N) \ No newline at end of file From 6e1b6012ce89a13ab5d29ce878d82017bf30ce63 Mon Sep 17 00:00:00 2001 From: whiteand Date: Thu, 7 Oct 2021 00:46:36 +0300 Subject: [PATCH 3/3] removed unnecessary comments. added useful one --- examples/is-prime.porth | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/examples/is-prime.porth b/examples/is-prime.porth index 3f94c3d4..494541d5 100644 --- a/examples/is-prime.porth +++ b/examples/is-prime.porth @@ -32,7 +32,7 @@ macro digits_to_number end drop drop end -// returns number read from stdint +// returns number read from stdin macro read_number NUM_PTR 0 .64 CHAR_CAPACITY CHARS_PTR stdin read @@ -44,6 +44,7 @@ macro read_number NUM_PTR ,64 end +// expects number divisor macro divisible % 0 = end @@ -61,10 +62,8 @@ macro is_prime drop false else true swap 5 while over over dup * >= do - // RES NUMBER POSSIBLE_DIVIDER over over divisible if 2drop drop false 0 1 - // false 0 1 else 2 + end