diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-03-16 00:06:38 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-03-16 00:06:38 +0000 |
| commit | fc6564872bbc5d8558fda016a6907fbe0cbac619 (patch) | |
| tree | dea522ce4bf3326fc9a855d1ef0e9495e10fedd3 /challenge-102 | |
| parent | a244217ac3b9c896253f5bcf624c837f43b763b7 (diff) | |
| download | perlweeklychallenge-club-fc6564872bbc5d8558fda016a6907fbe0cbac619.tar.gz perlweeklychallenge-club-fc6564872bbc5d8558fda016a6907fbe0cbac619.tar.bz2 perlweeklychallenge-club-fc6564872bbc5d8558fda016a6907fbe0cbac619.zip | |
Add Forth solution to challenge 102
Diffstat (limited to 'challenge-102')
| -rw-r--r-- | challenge-102/paulo-custodio/forth/ch-1.fs | 51 | ||||
| -rw-r--r-- | challenge-102/paulo-custodio/forth/ch-2.fs | 43 |
2 files changed, 94 insertions, 0 deletions
diff --git a/challenge-102/paulo-custodio/forth/ch-1.fs b/challenge-102/paulo-custodio/forth/ch-1.fs new file mode 100644 index 0000000000..9c6e3c61b0 --- /dev/null +++ b/challenge-102/paulo-custodio/forth/ch-1.fs @@ -0,0 +1,51 @@ +#! /usr/bin/env gforth + +\ Challenge 102 +\ +\ TASK #1 › Rare Numbers +\ Submitted by: Mohammad S Anwar +\ +\ You are given a positive integer $N. +\ +\ Write a script to generate all Rare numbers of size $N if exists. Please +\ checkout the page for more information about it. +\ Examples +\ +\ (a) 2 digits: 65 +\ (b) 6 digits: 621770 +\ (c) 9 digits: 281089082 + +\ 1234 -> 4321 +: invert_number { n -- } + 0 { n1 } + BEGIN n 0<> WHILE + n1 10 * n 10 MOD + TO n1 + n 10 / TO n + REPEAT + n1 ; + +\ check if number is perfect square +: is_perfect_square ( n -- f ) + S>F FSQRT + FDUP FLOOR F= IF TRUE ELSE FALSE THEN ; + +\ compute 10^n +: 10pow ( n -- 10^n ) + 1 SWAP + 0 ?DO + 10 * + LOOP ; + +: .rare { n -- } + n 10pow n 1- 10pow DO + I I invert_number { r r1 } + r r1 + is_perfect_square IF + r r1 >= IF + r r1 - is_perfect_square IF + r . CR + THEN THEN THEN + LOOP ; + +NEXT-ARG S>NUMBER? 0= THROW DROP +.rare +BYE diff --git a/challenge-102/paulo-custodio/forth/ch-2.fs b/challenge-102/paulo-custodio/forth/ch-2.fs new file mode 100644 index 0000000000..5b3f0c9499 --- /dev/null +++ b/challenge-102/paulo-custodio/forth/ch-2.fs @@ -0,0 +1,43 @@ +#! /usr/bin/env gforth + +\ Challenge 102 +\ +\ TASK #2 › Hash-counting String +\ Submitted by: Stuart Little +\ +\ You are given a positive integer $N. +\ +\ Write a script to produce Hash-counting string of that length. +\ +\ The definition of a hash-counting string is as follows: +\ - the string consists only of digits 0-9 and hashes, ‘#’ +\ - there are no two consecutive hashes: ‘##’ does not appear in your string +\ - the last character is a hash +\ - the number immediately preceding each hash (if it exists) is the position +\ of that hash in the string, with the position being counted up from 1 +\ +\ It can be shown that for every positive integer N there is exactly one such +\ length-N string. +\ Examples: +\ +\ (a) "#" is the counting string of length 1 +\ (b) "2#" is the counting string of length 2 +\ (c) "#3#" is the string of length 3 +\ (d) "#3#5#7#10#" is the string of length 10 +\ (e) "2#4#6#8#11#14#" is the string of length 14 + +\ hash-counting - use <# #> buffer to build string +: .hash_counting { i -- } + 0. <# + BEGIN i 0> WHILE + i { pos } + '#' HOLD i 1- TO i + BEGIN i 0> pos 0<> AND WHILE + 2DROP pos 0 # i 1- TO i + OVER TO pos + REPEAT + REPEAT + #> TYPE ; + +NEXT-ARG S>NUMBER? 0= THROW DROP +.hash_counting CR BYE |
