How to resolve the algorithm Gapful numbers step by step in the Forth programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Gapful numbers step by step in the Forth programming language
Table of Contents
Problem Statement
Numbers (positive integers expressed in base ten) that are (evenly) divisible by the number formed by the first and last digit are known as gapful numbers.
Evenly divisible means divisible with no remainder.
All one─ and two─digit numbers have this property and are trivially excluded. Only numbers ≥ 100 will be considered for this Rosetta Code task.
187 is a gapful number because it is evenly divisible by the number 17 which is formed by the first and last decimal digits of 187.
About 7.46% of positive integers are gapful.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Gapful numbers step by step in the Forth programming language
Source code in the forth programming language
variable cnt
: Int>Str s>d <# #s #> ;
: firstDigit C@ [char] 0 - ;
: lastDigit + 1- c@ [char] 0 - ;
: cnt++ cnt dup @ 1+ dup rot ! ;
: GapfulNumber? dup dup Int>Str
2dup drop firstDigit 10 *
-rot lastDigit +
/mod drop 0= ;
: main 0 cnt ! 2dup
cr ." First " . ." gapful numbers >= " .
begin dup cnt @ -
while swap GapfulNumber?
if dup cr cnt++ . ." : " . then
1+ swap
repeat 2drop ;
100 30 main cr
1000000 15 main cr
1000000000 10 main cr
You may also check:How to resolve the algorithm Linear congruential generator step by step in the Factor programming language
You may also check:How to resolve the algorithm Find the intersection of two lines step by step in the Maple programming language
You may also check:How to resolve the algorithm Terminal control/Clear the screen step by step in the Ada programming language
You may also check:How to resolve the algorithm Find limit of recursion step by step in the Wren programming language
You may also check:How to resolve the algorithm Call a foreign-language function step by step in the Clojure programming language