Gapful numbers
Numbers (expressed in base ten) that are (evenly) divisible by the number formed by the decimal digits of their first and last digit are known as gapful numbers.
All one─ and two─digit numbers have this property and are trivially excluded. Only
numbers ≥ 100 will be considered for this Rosetta Code task.
- Example
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.
- Task
-
- Generate and show all sets of numbers (below) on one line (horizontally) with a title, here on this page
- Show the first 30 gapful numbers
- Show the first 15 gapful numbers ≥ 1,000,000
- Show the first 10 gapful numbers ≥ 1,000,000,000
- Also see
-
- The OEIS entry: A108343 gapful numbers.
Factor
<lang factor>USING: formatting kernel lists lists.lazy math math.functions math.text.utils sequences ; IN: rosetta-code.gapful-numbers
- gapful? ( n -- ? )
dup 1 digit-groups [ first ] [ last 10 * + ] bi divisor? ;
30 100 15 1,000,000 10 1,000,000,000 [
[ "%d gapful numbers starting at %d:\n" printf ] [ lfrom [ gapful? ] lfilter ltake list>array "%[%d, %]\n\n" printf ] 2bi
] 2tri@</lang>
- Output:
30 gapful numbers starting at 100: { 100, 105, 108, 110, 120, 121, 130, 132, 135, 140, 143, 150, 154, 160, 165, 170, 176, 180, 187, 190, 192, 195, 198, 200, 220, 225, 231, 240, 242, 253 } 15 gapful numbers starting at 1000000: { 1000000, 1000005, 1000008, 1000010, 1000016, 1000020, 1000021, 1000030, 1000032, 1000034, 1000035, 1000040, 1000050, 1000060, 1000065 } 10 gapful numbers starting at 1000000000: { 1000000000, 1000000001, 1000000005, 1000000008, 1000000010, 1000000016, 1000000020, 1000000027, 1000000030, 1000000032 }
Perl 6
<lang perl6>use Lingua::EN::Numbers;
for (1e2, 30, 1e6, 15, 1e9, 10)».Int -> $start, $count {
put "\nFirst $count gapful numbers starting at {comma $start}:\n" ~ ($start..*).grep( { $_ %% .comb[0, *-1].join } )[^$count];
}</lang>
- Output:
First 30 gapful numbers starting at 100: 100 105 108 110 120 121 130 132 135 140 143 150 154 160 165 170 176 180 187 190 192 195 198 200 220 225 231 240 242 253 First 15 gapful numbers starting at 1,000,000: 1000000 1000005 1000008 1000010 1000016 1000020 1000021 1000030 1000032 1000034 1000035 1000040 1000050 1000060 1000065 First 10 gapful numbers starting at 1,000,000,000: 1000000000 1000000001 1000000005 1000000008 1000000010 1000000016 1000000020 1000000027 1000000030 1000000032
Python
<lang python>from itertools import islice, count for start, n in [(100, 30), (1_000_000, 15), (1_000_000_000, 10)]:
print(f"\nFirst {n} gapful numbers from {start:_}") print(list(islice(( x for x in count(start) if (x % (int(str(x)[0]) * 10 + (x % 10)) == 0) ) , n)))</lang>
- Output:
First 30 gapful numbers from 100 [100, 105, 108, 110, 120, 121, 130, 132, 135, 140, 143, 150, 154, 160, 165, 170, 176, 180, 187, 190, 192, 195, 198, 200, 220, 225, 231, 240, 242, 253] First 15 gapful numbers from 1_000_000 [1000000, 1000005, 1000008, 1000010, 1000016, 1000020, 1000021, 1000030, 1000032, 1000034, 1000035, 1000040, 1000050, 1000060, 1000065] First 10 gapful numbers from 1_000_000_000 [1000000000, 1000000001, 1000000005, 1000000008, 1000000010, 1000000016, 1000000020, 1000000027, 1000000030, 1000000032]
REXX
<lang rexx>/*REXX program computes and displays gapful numbers and also palindromic gapful numbers.*/ numeric digits 20 /*ensure enough decimal digits gapfuls.*/ parse arg gapfuls /*obtain optional arguments from the CL*/ if gapfuls= then gapfuls= 30 15@10000000 10@100000000000 /*assume defaults. */
do until gapfuls=; parse var gapfuls stuff gapfuls; call gapful stuff end /*until*/
exit /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ gapful: procedure; parse arg n '@' sp; #= 0; if sp== then sp= 100
say center(' 'n " gapful numbers starting at: " sp' ', 140, "═") $= /*initialize $ list.*/ do j=sp until #==n /*SP: start point. */ parse var j a 2 -1 b /*get 1st & last dig*/ if j // (a||b) \== 0 then iterate /*perform ÷ into J.*/ #= # + 1; $= $ j /*bump #; append──►$*/ end /*j*/ say strip($); say; return</lang>
- output when using the default inputs:
(Shown at 5/6 size.)
═══════════════════════════════════════════════════ 30 gapful numbers starting at: 100 ═══════════════════════════════════════════════════ 100 105 108 110 120 121 130 132 135 140 143 150 154 160 165 170 176 180 187 190 192 195 198 200 220 225 231 240 242 253 ════════════════════════════════════════════════ 15 gapful numbers starting at: 10000000 ═════════════════════════════════════════════════ 10000000 10000001 10000003 10000004 10000005 10000008 10000010 10000016 10000020 10000030 10000032 10000035 10000040 10000050 10000060 ══════════════════════════════════════════════ 10 gapful numbers starting at: 100000000000 ═══════════════════════════════════════════════ 100000000000 100000000001 100000000005 100000000008 100000000010 100000000016 100000000020 100000000030 100000000032 100000000035