Gapful numbers
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
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.
- 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.
About 7.46% of positive integers are gapful.
- 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
- Related tasks
- Also see
-
- The OEIS entry: A108343 gapful numbers.
- numbersaplenty gapful numbers
AppleScript
<lang applescript>on isGapful(n)
set units to n mod 10 set temp to n div 10 repeat until (temp < 10) set temp to temp div 10 end repeat return (n mod (temp * 10 + units) = 0)
end isGapful
-- Task code: on getGapfuls(n, q)
set collector to {} repeat until ((count collector) = q) if (isGapful(n)) then set end of collector to n set n to n + 1 end repeat return collector
end getGapfuls
local output, astid set output to {} set astid to AppleScript's text item delimiters set AppleScript's text item delimiters to " " set end of output to "First 30 gapful numbers ≥ 100:" & linefeed & getGapfuls(100, 30) set end of output to "First 15 gapful numbers ≥ 1,000,000:" & linefeed & getGapfuls(1000000, 15) set end of output to "First 10 gapful numbers ≥ 100,000,000:" & linefeed & getGapfuls(1.0E+9, 10) set AppleScript's text item delimiters to linefeed & linefeed set output to output as text set AppleScript's text item delimiters to astid return output</lang>
- Output:
<lang applescript>"First 30 gapful numbers ≥ 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 ≥ 1,000,000: 1000000 1000005 1000008 1000010 1000016 1000020 1000021 1000030 1000032 1000034 1000035 1000040 1000050 1000060 1000065
First 10 gapful numbers ≥ 1,000,000,000: 1.0E+9 1.000000001E+9 1.000000005E+9 1.000000008E+9 1.00000001E+9 1.000000016E+9 1.00000002E+9 1.000000027E+9 1.00000003E+9 1.000000032E+9"</lang>
AutoHotkey
<lang AutoHotkey>Gapful_numbers(Min, Qty){ counter:= 0, output := "" while (counter < Qty){ n := A_Index+Min-1 d := SubStr(n, 1, 1) * 10 + SubStr(n, 0) if (n/d = Floor(n/d)) output .= ++counter ": " n "`t" n " / " d " = " Format("{:d}", n/d) "`n" } return output }</lang> Examples:<lang AutoHotkey>MsgBox, 262144, , % Gapful_numbers(100, 30) MsgBox, 262144, , % Gapful_numbers(1000000, 15) MsgBox, 262144, , % Gapful_numbers(1000000000, 10)</lang>
- Output:
1: 100 100 / 10 = 10 2: 105 105 / 15 = 7 3: 108 108 / 18 = 6 4: 110 110 / 10 = 11 5: 120 120 / 10 = 12 6: 121 121 / 11 = 11 7: 130 130 / 10 = 13 8: 132 132 / 12 = 11 9: 135 135 / 15 = 9 10: 140 140 / 10 = 14 11: 143 143 / 13 = 11 12: 150 150 / 10 = 15 13: 154 154 / 14 = 11 14: 160 160 / 10 = 16 15: 165 165 / 15 = 11 16: 170 170 / 10 = 17 17: 176 176 / 16 = 11 18: 180 180 / 10 = 18 19: 187 187 / 17 = 11 20: 190 190 / 10 = 19 21: 192 192 / 12 = 16 22: 195 195 / 15 = 13 23: 198 198 / 18 = 11 24: 200 200 / 20 = 10 25: 220 220 / 20 = 11 26: 225 225 / 25 = 9 27: 231 231 / 21 = 11 28: 240 240 / 20 = 12 29: 242 242 / 22 = 11 30: 253 253 / 23 = 11 --------------------------- 1: 1000000 1000000 / 10 = 100000 2: 1000005 1000005 / 15 = 66667 3: 1000008 1000008 / 18 = 55556 4: 1000010 1000010 / 10 = 100001 5: 1000016 1000016 / 16 = 62501 6: 1000020 1000020 / 10 = 100002 7: 1000021 1000021 / 11 = 90911 8: 1000030 1000030 / 10 = 100003 9: 1000032 1000032 / 12 = 83336 10: 1000034 1000034 / 14 = 71431 11: 1000035 1000035 / 15 = 66669 12: 1000040 1000040 / 10 = 100004 13: 1000050 1000050 / 10 = 100005 14: 1000060 1000060 / 10 = 100006 15: 1000065 1000065 / 15 = 66671 --------------------------- 1: 1000000000 1000000000 / 10 = 100000000 2: 1000000001 1000000001 / 11 = 90909091 3: 1000000005 1000000005 / 15 = 66666667 4: 1000000008 1000000008 / 18 = 55555556 5: 1000000010 1000000010 / 10 = 100000001 6: 1000000016 1000000016 / 16 = 62500001 7: 1000000020 1000000020 / 10 = 100000002 8: 1000000027 1000000027 / 17 = 58823531 9: 1000000030 1000000030 / 10 = 100000003 10: 1000000032 1000000032 / 12 = 83333336
AWK
<lang AWK>
- syntax: GAWK -f GAPFUL_NUMBERS.AWK
- converted from C++
BEGIN {
show_gapful(100,30) show_gapful(1000000,15) show_gapful(1000000000,10) show_gapful(7123,25) exit(0)
} function is_gapful(n, m) {
m = n while (m >= 10) { m = int(m / 10) } return(n % ((n % 10) + 10 * (m % 10)) == 0)
} function show_gapful(n, count,i) {
printf("first %d gapful numbers >= %d:",count,n) for (i=0; i<count; n++) { if (is_gapful(n)) { printf(" %d",n) i++ } } printf("\n")
} </lang>
- Output:
first 30 gapful numbers >= 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 >= 1000000: 1000000 1000005 1000008 1000010 1000016 1000020 1000021 1000030 1000032 1000034 1000035 1000040 1000050 1000060 1000065 first 10 gapful numbers >= 1000000000: 1000000000 1000000001 1000000005 1000000008 1000000010 1000000016 1000000020 1000000027 1000000030 1000000032 first 25 gapful numbers >= 7123: 7125 7140 7171 7189 7210 7272 7275 7280 7296 7350 7373 7420 7425 7474 7488 7490 7560 7575 7630 7632 7676 7700 7725 7770 7777
BASIC256
<lang BASIC256>function is_gapful(n) m = n l = n mod 10 while (m >= 10) m = int(m / 10) end while return (m * 10) + l end function
subroutine muestra_gapful(n, gaps) inc = 0 print "Primeros "; gaps; " números gapful >= "; n while inc < gaps if n mod is_gapful(n) = 0 then print " " ; n ; inc = inc + 1 end if n = n + 1 end while print chr(10) end subroutine
call muestra_gapful(100, 30) call muestra_gapful(1000000, 15) call muestra_gapful(1000000000, 10) call muestra_gapful(7123,25) end</lang>
- Output:
Igual que la entrada de Yabasic.
C
<lang C>
- include<stdio.h>
void generateGaps(unsigned long long int start,int count){
int counter = 0; unsigned long long int i = start; char str[100]; printf("\nFirst %d Gapful numbers >= %llu :\n",count,start);
while(counter<count){ sprintf(str,"%llu",i); if((i%(10*(str[0]-'0') + i%10))==0L){ printf("\n%3d : %llu",counter+1,i); counter++; } i++; }
}
int main() {
unsigned long long int i = 100; int count = 0; char str[21];
generateGaps(100,30); printf("\n"); generateGaps(1000000,15); printf("\n"); generateGaps(1000000000,15); printf("\n");
return 0;
} </lang> Output :
abhishek_ghosh@Azure:~/doodles$ ./a.out First 30 Gapful numbers >= 100 : 1 : 100 2 : 105 3 : 108 4 : 110 5 : 120 6 : 121 7 : 130 8 : 132 9 : 135 10 : 140 11 : 143 12 : 150 13 : 154 14 : 160 15 : 165 16 : 170 17 : 176 18 : 180 19 : 187 20 : 190 21 : 192 22 : 195 23 : 198 24 : 200 25 : 220 26 : 225 27 : 231 28 : 240 29 : 242 30 : 253 First 15 Gapful numbers >= 1000000 : 1 : 1000000 2 : 1000005 3 : 1000008 4 : 1000010 5 : 1000016 6 : 1000020 7 : 1000021 8 : 1000030 9 : 1000032 10 : 1000034 11 : 1000035 12 : 1000040 13 : 1000050 14 : 1000060 15 : 1000065 First 15 Gapful numbers >= 1000000000 : 1 : 1000000000 2 : 1000000001 3 : 1000000005 4 : 1000000008 5 : 1000000010 6 : 1000000016 7 : 1000000020 8 : 1000000027 9 : 1000000030 10 : 1000000032 11 : 1000000035 12 : 1000000039 13 : 1000000040 14 : 1000000050 15 : 1000000053
C++
<lang cpp>#include <iostream>
bool gapful(int n) {
int m = n; while (m >= 10) m /= 10; return n % ((n % 10) + 10 * (m % 10)) == 0;
}
void show_gapful_numbers(int n, int count) {
std::cout << "First " << count << " gapful numbers >= " << n << ":\n"; for (int i = 0; i < count; ++n) { if (gapful(n)) { if (i != 0) std::cout << ", "; std::cout << n; ++i; } } std::cout << '\n';
}
int main() {
show_gapful_numbers(100, 30); show_gapful_numbers(1000000, 15); show_gapful_numbers(1000000000, 10); return 0;
}</lang>
- Output:
First 30 gapful numbers >= 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 >= 1000000: 1000000, 1000005, 1000008, 1000010, 1000016, 1000020, 1000021, 1000030, 1000032, 1000034, 1000035, 1000040, 1000050, 1000060, 1000065 First 10 gapful numbers >= 1000000000: 1000000000, 1000000001, 1000000005, 1000000008, 1000000010, 1000000016, 1000000020, 1000000027, 1000000030, 1000000032
C#
<lang csharp> using System;
namespace GapfulNumbers {
class Program { static void Main(string[] args) { Console.WriteLine("The first 30 gapful numbers are: "); /* Starting at 100, find 30 gapful numbers */ FindGap(100, 30);
Console.WriteLine("The first 15 gapful numbers > 1,000,000 are: "); FindGap(1000000, 15);
Console.WriteLine("The first 10 gapful numbers > 1,000,000,000 are: "); FindGap(1000000000, 10);
Console.Read(); }
public static int firstNum(int n) { /*Divide by ten until the leading digit remains.*/ while (n >= 10) { n /= 10; } return (n); }
public static int lastNum(int n) { /*Modulo gives you the last digit. */ return (n % 10); }
static void FindGap(int n, int gaps) { int count = 0; while (count < gaps) {
/* We have to convert our first and last digits to strings to concatenate.*/ string concat = firstNum(n).ToString() + lastNum(n).ToString(); /* And then convert our concatenated string back to an integer. */ int i = Convert.ToInt32(concat);
/* Modulo with our new integer and output the result. */ if (n % i == 0) { Console.Write(n + " "); count++; n++; } else { n++; continue; } } } }
}
</lang>
- Output:
The first 30 gapful numbers are: 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 The first 15 gapful numbers > 1,000,000 are: 1000000 1000005 1000008 1000010 1000016 1000020 1000021 1000030 1000032 1000034 1000035 1000040 1000050 1000060 1000065 The first 10 gapful numbers > 1,000,000,000 are: 1000000000 1000000001 1000000005 1000000008 1000000010 1000000016 1000000020 1000000027 1000000030 1000000032
Crystal
With lazy iterator <lang ruby>struct Int
def gapful? a = self.to_s.chars.map(&.to_i) self % (a.first*10 + a.last) == 0 end
end
specs = {100 => 30, 1_000_000 => 15, 1_000_000_000 => 10, 7123 => 25}
specs.each do |start, count|
puts "first #{count} gapful numbers >= #{start}:" puts (start..).each.select(&.gapful?).first(count).to_a, "\n"
end</lang>
Alternative <lang ruby>struct Int
def gapful? a = self.to_s.chars.map(&.to_i) self % (a.first*10 + a.last) == 0 end
end
specs = {100 => 30, 1_000_000 => 15, 1_000_000_000 => 10, 7123 => 25}
specs.each do |start, count|
puts "first #{count} gapful numbers >= #{start}:" i, gapful = 0, [] of Int32 (start..).each { |n| n.gapful? && (gapful << n; i += 1); break if i == count } puts gapful, "\n"
end</lang>
- Output:
first 30 gapful numbers >= 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 >= 1000000: [1000000, 1000005, 1000008, 1000010, 1000016, 1000020, 1000021, 1000030, 1000032, 1000034, 1000035, 1000040, 1000050, 1000060, 1000065] first 10 gapful numbers >= 1000000000: [1000000000, 1000000001, 1000000005, 1000000008, 1000000010, 1000000016, 1000000020, 1000000027, 1000000030, 1000000032] first 25 gapful numbers >= 7123: [7125, 7140, 7171, 7189, 7210, 7272, 7275, 7280, 7296, 7350, 7373, 7420, 7425, 7474, 7488, 7490, 7560, 7575, 7630, 7632, 7676, 7700, 7725, 7770, 7777]
D
<lang d>import std.conv; import std.stdio;
string commatize(ulong n) {
auto s = n.to!string; auto le = s.length; for (int i = le - 3; i >= 1; i -= 3) { s = s[0..i] ~ "," ~ s[i..$]; } return s;
}
void main() {
ulong[] starts = [cast(ulong)1e2, cast(ulong)1e6, cast(ulong)1e7, cast(ulong)1e9, 7123]; int[] counts = [30, 15, 15, 10, 25]; for (int i = 0; i < starts.length; i++) { int count = 0; auto j = starts[i]; ulong pow = 100; while (true) { if (j < pow * 10) { break; } pow *= 10; } writefln("First %d gapful numbers starting at %s:", counts[i], commatize(starts[i])); while (count < counts[i]) { auto fl = (j / pow) * 10 + (j % 10); if (j % fl == 0) { write(j, ' '); count++; } j++; if (j >= 10 * pow) { pow *= 10; } } writeln("\n"); }
}</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 15 gapful numbers starting at 10,000,000: 10000000 10000001 10000003 10000004 10000005 10000008 10000010 10000016 10000020 10000030 10000032 10000035 10000040 10000050 10000060 First 10 gapful numbers starting at 1,000,000,000: 1000000000 1000000001 1000000005 1000000008 1000000010 1000000016 1000000020 1000000027 1000000030 1000000032 First 25 gapful numbers starting at 7,123: 7125 7140 7171 7189 7210 7272 7275 7280 7296 7350 7373 7420 7425 7474 7488 7490 7560 7575 7630 7632 7676 7700 7725 7770 7777
Delphi
See Pascal.
Factor
<lang factor>USING: formatting kernel lists lists.lazy math math.functions math.text.utils sequences ;
- gapful? ( n -- ? )
dup 1 digit-groups [ first ] [ last 10 * + ] bi divisor? ;
30 100 15 1,000,000 10 1,000,000,000 [
2dup lfrom [ gapful? ] lfilter ltake list>array "%d gapful numbers starting at %d:\n%[%d, %]\n\n" printf
] 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 }
FreeBASIC
<lang freebasic> function is_gapful( n as uinteger ) as boolean
if n<100 then return false dim as string ns = str(n) dim as uinteger gap = 10*val(mid(ns,1,1)) + val(mid(ns,len(ns),1)) if n mod gap = 0 then return true else return false
end function
dim as ulongint i = 100 dim as ushort c print "The first thirty gapful numbers:" while c<30
if is_gapful(i) then c += 1 print i;" "; end if i += 1
wend print : print i = 1000000 : c = 0 print "The first fifteen gapful numbers above 999,999:" while c<15
if is_gapful(i) then c += 1 print i;" "; end if i += 1
wend print : print i = 1000000000 : c = 0 print "The first ten gapful numbers above 999,999,999:" while c<10
if is_gapful(i) then c += 1 print i;" "; end if i += 1
wend print</lang>
- Output:
The first thirty gapful numbers: 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 The first fifteen gapful numbers above 999,999 1000000 1000005 1000008 1000010 1000016 1000020 1000021 1000030 1000032 1000034 1000035 1000040 1000050 1000060 1000065 The first ten gapful numbers above 999,999,999 1000000000 1000000001 1000000005 1000000008 1000000010 1000000016 1000000020 1000000027 1000000030 1000000032
Frink
<lang frink> // Create function to calculate gapful number gapful[num,totalCounter] := {
// Display a line explaining the current calculation. println["First $totalCounter gapful numbers over $num:"] // Start a counter to compare with the total count. counter = 0 while counter < totalCounter { numStr = toString[num] // Convert the integer to a string gapfulNumStr = left[numStr,1] + right[numStr,1] // Concatenate the first and last character of the number to form a two digit number gapfulNumInt = parseInt[gapfulNumStr] // Turn the concatenated string back into an integer. // If the concatenated two digit integer divides into the current num variable with no remainder, print it to the list and increase our counter if num mod gapfulNumInt == 0 { print[numStr + " "] counter = counter + 1 } // Increase the current number for the next cycle. num = num + 1 } println[] // Linkbreak
}
// Print the first 30 gapful numbers over 100, the top 15 over 1,000,000 and the first 10 over 1,000,000,000. gapful[100,30] gapful[1000000,15] gapful[1000000000,10] </lang>
- Output:
First 30 gapful numbers over 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 over 1000000: 1000000 1000005 1000008 1000010 1000016 1000020 1000021 1000030 1000032 1000034 1000035 1000040 1000050 1000060 1000065 First 10 gapful numbers over 1000000000: 1000000000 1000000001 1000000005 1000000008 1000000010 1000000016 1000000020 1000000027 1000000030 1000000032
Fōrmulæ
In this page you can see the solution of this task.
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text (more info). Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation —i.e. XML, JSON— they are intended for transportation effects more than visualization and edition.
The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.
Go
<lang go>package main
import "fmt"
func commatize(n uint64) string {
s := fmt.Sprintf("%d", n) le := len(s) for i := le - 3; i >= 1; i -= 3 { s = s[0:i] + "," + s[i:] } return s
}
func main() {
starts := []uint64{1e2, 1e6, 1e7, 1e9, 7123} counts := []int{30, 15, 15, 10, 25} for i := 0; i < len(starts); i++ { count := 0 j := starts[i] pow := uint64(100) for { if j < pow*10 { break } pow *= 10 } fmt.Printf("First %d gapful numbers starting at %s:\n", counts[i], commatize(starts[i])) for count < counts[i] { fl := (j/pow)*10 + (j % 10) if j%fl == 0 { fmt.Printf("%d ", j) count++ } j++ if j >= 10*pow { pow *= 10 } } fmt.Println("\n") }
}</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 15 gapful numbers starting at 10,000,000: 10000000 10000001 10000003 10000004 10000005 10000008 10000010 10000016 10000020 10000030 10000032 10000035 10000040 10000050 10000060 First 10 gapful numbers starting at 1,000,000,000: 1000000000 1000000001 1000000005 1000000008 1000000010 1000000016 1000000020 1000000027 1000000030 1000000032 First 25 gapful numbers starting at 7,123: 7125 7140 7171 7189 7210 7272 7275 7280 7296 7350 7373 7420 7425 7474 7488 7490 7560 7575 7630 7632 7676 7700 7725 7770 7777
Haskell
<lang haskell>{-# LANGUAGE NumericUnderscores #-}
gapful :: Int -> Bool gapful n = n `rem` firstLastDigit == 0
where firstLastDigit = read [head asDigits, last asDigits] asDigits = show n
main :: IO () main = do
putStrLn $ "\nFirst 30 Gapful numbers >= 100 :\n" ++ r 30 [100,101..] putStrLn $ "\nFirst 15 Gapful numbers >= 1,000,000 :\n" ++ r 15 [1_000_000,1_000_001..] putStrLn $ "\nFirst 10 Gapful numbers >= 1,000,000,000 :\n" ++ r 10 [1_000_000_000,1_000_000_001..] where r n = show . take n . filter gapful</lang>
- Output:
First 30 Gapful numbers >= 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 >= 1,000,000 : [1000000,1000005,1000008,1000010,1000016,1000020,1000021,1000030,1000032,1000034,1000035,1000040,1000050,1000060,1000065] First 10 Gapful numbers >= 1,000,000,000 : [1000000000,1000000001,1000000005,1000000008,1000000010,1000000016,1000000020,1000000027,1000000030,1000000032]
Or, defining the predicate in applicative terms, and wrapping the output:
<lang haskell>import Data.List.Split (chunksOf)
import Data.List (intercalate)
GAPFUL NUMBERS ---------------------
isGapful :: Int -> Bool isGapful = (0 ==) . (<*>) rem (read . (<*>) [head, last] . pure . show)
TEST --------------------------
main :: IO () main =
mapM_ (putStrLn . showSample) [ "First 30 gapful numbers >= 100" , "First 15 Gapful numbers >= 1000000" , "First 10 Gapful numbers >= 1000000000" ]
showSample :: String -> String showSample k =
let ws = words k in k <> ":\n\n" <> unlines (fmap (intercalate ", " . fmap show) $ chunksOf 5 $ take (read (ws !! 1)) [read (ws !! 5) :: Int ..])</lang>
- Output:
First 30 gapful numbers >= 100: 100, 101, 102, 103, 104 105, 106, 107, 108, 109 110, 111, 112, 113, 114 115, 116, 117, 118, 119 120, 121, 122, 123, 124 125, 126, 127, 128, 129 First 15 Gapful numbers >= 1000000: 1000000, 1000001, 1000002, 1000003, 1000004 1000005, 1000006, 1000007, 1000008, 1000009 1000010, 1000011, 1000012, 1000013, 1000014 First 10 Gapful numbers >= 1000000000: 1000000000, 1000000001, 1000000002, 1000000003, 1000000004 1000000005, 1000000006, 1000000007, 1000000008, 1000000009
J
<lang> gapful =: 0 = (|~ ({.,{:)&.(10&#.inv))
task =: 100&$: :(dyad define) NB. MINIMUM task TALLY
gn =. y {. (#~ gapful&>) x + i. y * 25 assert 0 ~: {: gn 'The first ' , (": y) , ' gapful numbers exceeding ' , (":<:x) , ' are ' , (":gn)
) </lang>
task 30 The first 30 gapful numbers exceeding 99 are 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 1e6 task 15 The first 15 gapful numbers exceeding 999999 are 1000000 1000005 1000008 1000010 1000016 1000020 1000021 1000030 1000032 1000034 1000035 1000040 1000050 1000060 1000065 1e9 task 10 The first 10 gapful numbers exceeding 999999999 are 1000000000 1000000001 1000000005 1000000008 1000000010 1000000016 1000000020 1000000027 1000000030 1000000032
Java
<lang java>import java.util.List;
public class GapfulNumbers {
private static String commatize(long n) { StringBuilder sb = new StringBuilder(Long.toString(n)); int le = sb.length(); for (int i = le - 3; i >= 1; i -= 3) { sb.insert(i, ','); } return sb.toString(); }
public static void main(String[] args) { List<Long> starts = List.of((long) 1e2, (long) 1e6, (long) 1e7, (long) 1e9, (long) 7123); List<Integer> counts = List.of(30, 15, 15, 10, 25); for (int i = 0; i < starts.size(); ++i) { int count = 0; Long j = starts.get(i); long pow = 100; while (j >= pow * 10) { pow *= 10; } System.out.printf("First %d gapful numbers starting at %s:\n", counts.get(i), commatize(starts.get(i))); while (count < counts.get(i)) { long fl = (j / pow) * 10 + (j % 10); if (j % fl == 0) { System.out.printf("%d ", j); count++; } j++; if (j >= 10 * pow) { pow *= 10; } } System.out.println('\n'); } }
}</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 15 gapful numbers starting at 10,000,000: 10000000 10000001 10000003 10000004 10000005 10000008 10000010 10000016 10000020 10000030 10000032 10000035 10000040 10000050 10000060 First 10 gapful numbers starting at 1,000,000,000: 1000000000 1000000001 1000000005 1000000008 1000000010 1000000016 1000000020 1000000027 1000000030 1000000032 First 25 gapful numbers starting at 7,123: 7125 7140 7171 7189 7210 7272 7275 7280 7296 7350 7373 7420 7425 7474 7488 7490 7560 7575 7630 7632 7676 7700 7725 7770 7777
JavaScript
Windows command line version
<lang javascript>// Function to construct a new integer from the first and last digits of another function gapfulness_divisor (number) { var digit_string = number.toString(10) var digit_count = digit_string.length var first_digit = digit_string.substring(0, 1) var last_digit = digit_string.substring(digit_count - 1) return parseInt(first_digit.concat(last_digit), 10) }
// Divisibility test to determine gapfulness function is_gapful (number) { return number % gapfulness_divisor(number) == 0 }
// Function to search for the least gapful number greater than a given integer function next_gapful (number) { do { ++number } while (!is_gapful(number)) return number }
// Constructor for a list of gapful numbers starting from given lower bound function gapful_numbers (start, amount) { var list = [], count = 0, number = start if (amount > 0 && is_gapful(start)) { list.push(start) } while (list.length < amount) { number = next_gapful(number) list.push(number) } return list }
// Formatter for a comma-separated list of gapful numbers function single_line_gapfuls (start, amount) { var list = gapful_numbers(start, amount) return list.join(", ") }
// Windows console output wrapper function print(message) { WScript.StdOut.WriteLine(message) }
// Main algorithm
function print_gapfuls_with_header(start, amount) { print("First " + start + " gapful numbers starting at " + amount) print(single_line_gapfuls(start, amount)) }
print_gapfuls_with_header(100, 30) print_gapfuls_with_header(1000000, 15) print_gapfuls_with_header(1000000000, 10)</lang>
- Output:
First 100 gapful numbers starting at 30 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 1000000 gapful numbers starting at 15 1000000, 1000005, 1000008, 1000010, 1000016, 1000020, 1000021, 1000030, 1000032, 1000034, 1000035, 1000040, 1000050, 1000060, 1000065 First 1000000000 gapful numbers starting at 10 1000000000, 1000000001, 1000000005, 1000000008, 1000000010, 1000000016, 1000000020, 1000000027, 1000000030, 1000000032
ES6
<lang javascript>(() => {
'use strict';
// ------------------ GAPFUL NUMBERS -------------------
// isGapful :: Int -> Bool const isGapful = n => compose( x => 0 === n % x, JSON.parse, concat, ap([head, last]), x => [x], JSON.stringify )(n);
// ----------------------- TEST ------------------------ const main = () => unlines([ 'First 30 gapful numbers >= 100', 'First 15 Gapful numbers >= 1E6', 'First 10 Gapful numbers >= 1E9' ].map(k => { const ws = words(k), mn = [1, 5].map( i => JSON.parse(ws[i]) ); return k + ':\n\n' + showList( take(mn[0])( filterGen(isGapful)( enumFrom(mn[1]) ) ) ); }));
// ----------------- GENERIC FUNCTIONS -----------------
// ap (<*>) :: [(a -> b)] -> [a] -> [b] const ap = fs => // The sequential application of each of a list // of functions to each of a list of values. xs => fs.flatMap(f => xs.map(x => f(x)));
// chunksOf :: Int -> [a] -> a const chunksOf = n => xs => enumFromThenTo(0)(n)( xs.length - 1 ).reduce( (a, i) => a.concat([xs.slice(i, (n + i))]), [] );
// compose (<<<) :: (b -> c) -> (a -> b) -> a -> c const compose = (...fs) => // A function defined by the right-to-left // composition of all the functions in fs. fs.reduce( (f, g) => x => f(g(x)), x => x );
// concat :: a -> [a] // concat :: [String] -> String const concat = xs => ( ys => 0 < ys.length ? ( ys.every(Array.isArray) ? ( [] ) : ).concat(...ys) : ys )(list(xs));
// enumFrom :: Enum a => a -> [a] function* enumFrom(x) { // A non-finite succession of enumerable // values, starting with the value x. let v = x; while (true) { yield v; v = succ(v); } }
// enumFromThenTo :: Int -> Int -> Int -> [Int] const enumFromThenTo = x1 => x2 => y => { const d = x2 - x1; return Array.from({ length: Math.floor(y - x2) / d + 2 }, (_, i) => x1 + (d * i)); };
// filterGen :: (a -> Bool) -> Gen [a] -> [a] const filterGen = p => xs => { function* go() { let x = xs.next(); while (!x.done) { let v = x.value; if (p(v)) { yield v; } x = xs.next(); } } return go(xs); };
// head :: [a] -> a const head = xs => ( ys => ys.length ? ( ys[0] ) : undefined )(list(xs));
// last :: [a] -> a const last = xs => ( // The last item of a list. ys => 0 < ys.length ? ( ys.slice(-1)[0] ) : undefined )(list(xs));
// list :: StringOrArrayLike b => b -> [a] const list = xs => // xs itself, if it is an Array, // or an Array derived from xs. Array.isArray(xs) ? ( xs ) : Array.from(xs || []);
// showList :: [a] -> String const showList = xs => unlines( chunksOf(5)(xs).map( ys => '\t' + ys.join(',') ) ) + '\n';
// succ :: Enum a => a -> a const succ = x => { const t = typeof x; return 'number' !== t ? (() => { const [i, mx] = [x, maxBound(x)].map(fromEnum); return i < mx ? ( toEnum(x)(1 + i) ) : Error('succ :: enum out of range.') })() : x < Number.MAX_SAFE_INTEGER ? ( 1 + x ) : Error('succ :: Num out of range.') };
// take :: Int -> [a] -> [a] // take :: Int -> String -> String const take = n => // The first n elements of a list, // string of characters, or stream. xs => 'GeneratorFunction' !== xs .constructor.constructor.name ? ( xs.slice(0, n) ) : [].concat.apply([], Array.from({ length: n }, () => { const x = xs.next(); return x.done ? [] : [x.value]; }));
// toEnum :: a -> Int -> a const toEnum = e => // The first argument is a sample of the type // allowing the function to make the right mapping x => ({ 'number': Number, 'string': String.fromCodePoint, 'boolean': Boolean, 'object': v => e.min + v } [typeof e])(x);
// unlines :: [String] -> String const unlines = xs => // A single string formed by the intercalation // of a list of strings with the newline character. xs.join('\n');
// words :: String -> [String] const words = s => // List of space-delimited sub-strings. s.split(/\s+/);
// MAIN --- return main();
})();</lang>
- Output:
First 30 gapful numbers >= 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 >= 1E6: 1000000,1000005,1000008,1000010,1000016 1000020,1000021,1000030,1000032,1000034 1000035,1000040,1000050,1000060,1000065 First 10 Gapful numbers >= 1E9: 1000000000,1000000001,1000000005,1000000008,1000000010 1000000016,1000000020,1000000027,1000000030,1000000032
Julia
<lang julia>using Lazy, Formatting
firstlast(a) = 10 * a[end] + a[1] isgapful(n) = (d = digits(n); length(d) < 3 || (m = firstlast(d)) != 0 && mod(n, m) == 0) gapfuls(start) = filter(isgapful, Lazy.range(start))
for (x, n) in [(100, 30), (1_000_000, 15), (1_000_000_000, 10)]
println("First $n gapful numbers starting at ", format(x, commas=true), ":\n", take(n, gapfuls(x)))
end
</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)
Kotlin
<lang scala>private fun commatize(n: Long): String {
val sb = StringBuilder(n.toString()) val le = sb.length var i = le - 3 while (i >= 1) { sb.insert(i, ',') i -= 3 } return sb.toString()
}
fun main() {
val starts = listOf(1e2.toLong(), 1e6.toLong(), 1e7.toLong(), 1e9.toLong(), 7123.toLong()) val counts = listOf(30, 15, 15, 10, 25) for (i in starts.indices) { var count = 0 var j = starts[i] var pow: Long = 100 while (j >= pow * 10) { pow *= 10 } System.out.printf( "First %d gapful numbers starting at %s:\n", counts[i], commatize(starts[i]) ) while (count < counts[i]) { val fl = j / pow * 10 + j % 10 if (j % fl == 0L) { System.out.printf("%d ", j) count++ } j++ if (j >= 10 * pow) { pow *= 10 } } println('\n') }
}</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 15 gapful numbers starting at 10,000,000: 10000000 10000001 10000003 10000004 10000005 10000008 10000010 10000016 10000020 10000030 10000032 10000035 10000040 10000050 10000060 First 10 gapful numbers starting at 1,000,000,000: 1000000000 1000000001 1000000005 1000000008 1000000010 1000000016 1000000020 1000000027 1000000030 1000000032 First 25 gapful numbers starting at 7,123: 7125 7140 7171 7189 7210 7272 7275 7280 7296 7350 7373 7420 7425 7474 7488 7490 7560 7575 7630 7632 7676 7700 7725 7770 7777
Lambdatalk
<lang Scheme> {def gapfuls
{lambda {:n :i :N} {if {>= :i :N} then else {if {= {% :n {W.first :n}{W.last :n}} 0} then :n {gapfuls {+ :n 1} {+ :i 1} :N} else {gapfuls {+ :n 1} :i :N}}}}}
-> gapfuls
{gapfuls 100 0 30} -> 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
{gapfuls 1000000 0 15} -> 1000000 1000005 1000008 1000010 1000016 1000020 1000021
1000030 1000032 1000034 1000035 1000040 1000050 1000060 1000065
{gapfuls 1000000000 0 10} -> 1000000000 1000000001 1000000005 1000000008 1000000010
1000000016 1000000020 1000000027 1000000030 1000000032
</lang>
Lua
<lang lua>function generateGaps(start, count)
local counter = 0 local i = start
print(string.format("First %d Gapful numbers >= %d :", count, start))
while counter < count do local str = tostring(i) local denom = 10 * tonumber(str:sub(1, 1)) + (i % 10) if i % denom == 0 then print(string.format("%3d : %d", counter + 1, i)) counter = counter + 1 end i = i + 1 end
end
generateGaps(100, 30) print()
generateGaps(1000000, 15) print()
generateGaps(1000000000, 15) print()</lang>
- Output:
First 30 Gapful numbers >= 100 : 1 : 100 2 : 105 3 : 108 4 : 110 5 : 120 6 : 121 7 : 130 8 : 132 9 : 135 10 : 140 11 : 143 12 : 150 13 : 154 14 : 160 15 : 165 16 : 170 17 : 176 18 : 180 19 : 187 20 : 190 21 : 192 22 : 195 23 : 198 24 : 200 25 : 220 26 : 225 27 : 231 28 : 240 29 : 242 30 : 253 First 15 Gapful numbers >= 1000000 : 1 : 1000000 2 : 1000005 3 : 1000008 4 : 1000010 5 : 1000016 6 : 1000020 7 : 1000021 8 : 1000030 9 : 1000032 10 : 1000034 11 : 1000035 12 : 1000040 13 : 1000050 14 : 1000060 15 : 1000065 First 15 Gapful numbers >= 1000000000 : 1 : 1000000000 2 : 1000000001 3 : 1000000005 4 : 1000000008 5 : 1000000010 6 : 1000000016 7 : 1000000020 8 : 1000000027 9 : 1000000030 10 : 1000000032 11 : 1000000035 12 : 1000000039 13 : 1000000040 14 : 1000000050 15 : 1000000053
min
<lang min>(() 0 shorten) :new (((10 mod) (10 div)) cleave) :moddiv ((dup 0 ==) (pop new) 'moddiv 'cons linrec) :digits (digits ((last 10 *) (first +)) cleave) :flnum (mod 0 ==) :divisor? (dup flnum divisor?) :gapful?
(
:target :n 0 :count "$1 gapful numbers starting at $2:" (target n) => % puts! (count target <) ( (n gapful?) ( count succ @count n print! " " print! ) when n succ @n ) while newline
) :show-gapfuls
100 30 show-gapfuls newline 1000000 15 show-gapfuls newline 1000000000 10 show-gapfuls</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
newLISP
<lang newLISP>; Part 1: Useful functions
- Create an integer out of the first and last digits of a given integer
(define (first-and-last-digits number)
(local (digits first-digit last-digit) (set 'digits (format "%d" number)) (set 'first-digit (first digits)) (set 'last-digit (last digits)) (int (append first-digit last-digit))))
- Divisbility test
(define (divisible-by? num1 num2)
(zero? (% num1 num2)))
- Gapfulness test
(define (gapful? number)
(divisible-by? number (first-and-last-digits number)))
- Increment until a gapful number is found
(define (next-gapful-after number)
(do-until (gapful? number) (++ number)))
- Return a list of gapful numbers beyond some (excluded) lower limit.
(define (gapful-numbers quantity lower-limit)
(let ((gapfuls '()) (number lower-limit)) (dotimes (counter quantity) (set 'number (next-gapful-after number)) (push number gapfuls)) (reverse gapfuls)))
- Format a list of numbers together into decimal notation.
(define (format-many numbers)
(map (curry format "%d") numbers))
- Format a list of integers on one line with commas
(define (format-one-line numbers)
(join (format-many numbers) ", "))
- Display a quantity of gapful numbers beyond some (excluded) lower limit.
(define (show-gapfuls quantity lower-limit)
(println "The first " quantity " gapful numbers beyond " lower-limit " are:") (println (format-one-line (gapful-numbers quantity lower-limit))))
- Part 2
- Complete the task
(show-gapfuls 30 99) (show-gapfuls 15 999999) (show-gapfuls 10 999999999) (exit)</lang>
- Output:
The first 30 gapful numbers beyond 99 are: 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 The first 15 gapful numbers beyond 999999 are: 1000000, 1000005, 1000008, 1000010, 1000016, 1000020, 1000021, 1000030, 1000032, 1000034, 1000035, 1000040, 1000050, 1000060, 1000065 The first 10 gapful numbers beyond 999999999 are: 1000000000, 1000000001, 1000000005, 1000000008, 1000000010, 1000000016, 1000000020, 1000000027, 1000000030, 1000000032
Nim
<lang Nim>import strutils
func gapfulDivisor(n: Positive): Positive =
## Return the gapful divisor of "n". let last = n mod 10 var first = n div 10 while first > 9: first = first div 10 result = 10 * first + last
iterator gapful(start: Positive): Positive =
## Yield the gapful numbers starting from "start". var n = start while true: let d = n.gapfulDivisor() if n mod d == 0: yield n inc n
proc displayGapfulNumbers(start, num: Positive) =
## Display the first "num" gapful numbers greater or equal to "start". echo "\nFirst $1 gapful numbers ⩾ $2:".format(num, start) var count = 0 var line: string for n in gapful(start): line.addSep(" ") line.add($n) inc count if count == num: break echo line
when isMainModule:
displayGapfulNumbers(100, 30) displayGapfulNumbers(1_000_000, 15) displayGapfulNumbers(1_000_000_000, 10)</lang>
- Output:
First 30 gapful numbers ⩾ 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 ⩾ 1000000: 1000000 1000005 1000008 1000010 1000016 1000020 1000021 1000030 1000032 1000034 1000035 1000040 1000050 1000060 1000065 First 10 gapful numbers ⩾ 1000000000: 1000000000 1000000001 1000000005 1000000008 1000000010 1000000016 1000000020 1000000027 1000000030 1000000032
Pascal
Now using using en passant updated MOD-values. Only recognizable for huge amounts of tests 100|74623687 ( up to 1 billion )-> takes 1.845s instead of 11.25s <lang pascal>program gapful;
{$IFDEF FPC}
{$MODE DELPHI}{$OPTIMIZATION ON,ALL}
{$ELSE}
{$APPTYPE CONSOLE}
{$ENDIF}
uses
sysutils // IntToStr
{$IFDEF FPC}
,strUtils // Numb2USA aka commatize
{$ENDIF};
const
cIdx = 5; starts: array[0..cIdx - 1] of Uint64 = (100, 1000 * 1000, 10 * 1000 * 1000, 1000 * 1000 * 1000, 7123); counts: array[0..cIdx - 1] of Uint64 = (30, 15, 15, 10, 25); //100| 74623687 => 1000*1000*1000 //100| 746236131 => 10*1000*1000*1000 //100|7462360431 =>100*1000*1000*1000 Base = 10;
var
ModsHL: array[0..99] of NativeUint; Pow10: Uint64; //global, seldom used countLmt: NativeUint; //Uint64; only for extreme counting
{$IFNDEF FPC}
function Numb2USA(const S: string): string; var
i, NA: Integer;
begin
i := Length(S); Result := S; NA := 0; while (i > 0) do begin if ((Length(Result) - i + 1 - NA) mod 3 = 0) and (i <> 1) then begin insert(',', Result, i); inc(NA); end; Dec(i); end;
end; {$ENDIF}
procedure OutHeader(i: NativeInt); begin
writeln('First ', counts[i], ', gapful numbers starting at ', Numb2USA(IntToStr (starts[i])));
end;
procedure OutNum(n: Uint64); begin
write(' ', n);
end;
procedure InitMods(n: Uint64; H_dgt: NativeUint); //calculate first mod of n, when it reaches n var
i, j: NativeInt;
begin
j := H_dgt; //= H_dgt+i for i := 0 to Base - 1 do begin ModsHL[j] := n mod j; inc(n); inc(j); end;
end;
procedure InitMods2(n: Uint64; H_dgt, L_Dgt: NativeUint); //calculate first mod of n, when it reaches n //beware, that the lower n are reached in the next base round var
i, j: NativeInt;
begin
j := H_dgt; n := n - L_Dgt; for i := 0 to L_Dgt - 1 do begin ModsHL[j] := (n + base) mod j; inc(n); inc(j); end; for i := L_Dgt to Base - 1 do begin ModsHL[j] := n mod j; inc(n); inc(j); end;
end;
procedure Main(TestNum: Uint64; Cnt: NativeUint); var
LmtNextNewHiDgt: Uint64; tmp, LowDgt, GapNum: NativeUint;
begin
countLmt := Cnt; Pow10 := Base * Base; LmtNextNewHiDgt := Base * Pow10; while LmtNextNewHiDgt <= TestNum do begin Pow10 := LmtNextNewHiDgt; LmtNextNewHiDgt := LmtNextNewHiDgt * Base; end; LowDgt := TestNum mod Base; GapNum := TestNum div Pow10; LmtNextNewHiDgt := (GapNum + 1) * Pow10; GapNum := Base * GapNum; if LowDgt <> 0 then InitMods2(TestNum, GapNum, LowDgt) else InitMODS(TestNum, GapNum);
GapNum := GapNum + LowDgt; repeat
// if TestNum MOD (GapNum) = 0 then
if ModsHL[GapNum] = 0 then begin tmp := countLmt - 1; if tmp < 32 then OutNum(TestNum); countLmt := tmp; // Test and BREAK only if something has changed if tmp = 0 then BREAK; end; tmp := Base + ModsHL[GapNum]; //translate into "if-less" version 3.35s -> 1.85s //bad branch prediction :-( //if tmp >= GapNum then tmp -= GapNum; tmp := tmp - (-ORD(tmp >= GapNum) and GapNum); ModsHL[GapNum] := tmp;
TestNum := TestNum + 1; tmp := LowDgt + 1;
inc(GapNum); if tmp >= Base then begin tmp := 0; GapNum := GapNum - Base; end; LowDgt := tmp; //next Hi Digit if TestNum >= LmtNextNewHiDgt then begin LowDgt := 0; GapNum := GapNum + Base; LmtNextNewHiDgt := LmtNextNewHiDgt + Pow10; //next power of 10 if GapNum >= Base * Base then begin Pow10 := Pow10 * Base; LmtNextNewHiDgt := 2 * Pow10; GapNum := Base; end; initMods(TestNum, GapNum); end; until false;
end;
var
i: integer;
begin
for i := 0 to High(starts) do begin OutHeader(i); Main(starts[i], counts[i]); writeln(#13#10); end; {$IFNDEF LINUX} readln; {$ENDIF}
end.</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 15, gapful numbers starting at 10,000,000 10000000 10000001 10000003 10000004 10000005 10000008 10000010 10000016 10000020 10000030 10000032 10000035 10000040 10000050 10000060 First 10, gapful numbers starting at 1,000,000,000 1000000000 1000000001 1000000005 1000000008 1000000010 1000000016 1000000020 1000000027 1000000030 1000000032 First 25, gapful numbers starting at 7,123 7125 7140 7171 7189 7210 7272 7275 7280 7296 7350 7373 7420 7425 7474 7488 7490 7560 7575 7630 7632 7676 7700 7725 7770 7777 _____ First 74623687, gapful numbers starting at 100 999998976 999999000 999999090 999999091 999999099 999999152 999999165 999999180 999999270 999999287 999999333 999999354 999999355 999999360 999999448 999999450 999999456 999999540 999999545 999999612 999999630 999999720 999999735 999999810 999999824 999999900 999999925 999999936 999999938 999999990 1000000000 real 0m1,845s start | count //100| 74623687 => 1000*1000*1000 //100| 746236131 => 10*1000*1000*1000 //100|7462360431 =>100*1000*1000*1000
only counting
<lang pascal>program gapful; {$IFDEF FPC}
{$MODE DELPHI}{$OPTIMIZATION ON,ALL}
{$ELSE}
{$APPTYPE CONSOLE}
{$ENDIF} uses
sysutils,// IntToStr strUtils;// Numb2USA aka commatize
var
LCMsHL : array of NativeInt;
function GCD(a, b: Int64): Int64; var
temp: Int64;
begin
while b <> 0 do begin temp := b; b := a mod b; a := temp end; result := a
end;
function LCM(a, b: Int64): Int64; begin
LCM := (a DIV GCD(a,b)) * b;
end;
procedure InitLCM(Base:NativeInt); var
i : integer;
Begin
For i := Base to (Base*Base-1) do LCMsHL[i] := LCM(i,Base);
end;
function CountGapFul(H_Digit,Base:NativeInt;PotBase:Uint64):Uint64; //Counts gapfulnumbers [n*PotBase..(n+1)*PotBase -1] ala [100..199] var
EndDgt,Dgt : NativeInt; P,k,lmt,sum,dSum: UInt64;
begin
P := PotBase*H_Digit; lmt := P+PotBase-1; Dgt := H_Digit*Base; sum := (PotBase-1) DIV dgt +1; For EndDgt := 1 to Base-1 do Begin inc(Dgt); //search start //first value divisible by dgt k := p-(p MOD dgt)+ dgt; //value divisible by dgt ending in the right digit while (k mod Base) <> EndDgt do inc(k,dgt); IF k> lmt then continue; //one found +1 //count the occurences in (lmt-k) dSum := (lmt-k) DIV LCMsHL[dgt] +1; inc(sum,dSum); //writeln(dgt:5,k:21,dSum:21,Sum:21); end; //writeln(p:21,Sum:21); CountGapFul := sum;
end;
procedure Main(Base:NativeUInt); var
i : NativeUInt; pot,total,lmt: Uint64;//High(Uint64) = 2^64-1
Begin
lmt := High(pot) DIV Base; pot := sqr(Base);//"100" in Base setlength(LCMsHL,pot); InitLCM(Base); total := 0; repeat IF pot > lmt then break; For i := 1 to Base-1 do //ala 100..199 ,200..299,300..399,..,900..999 inc(total,CountGapFul(i,base,pot)); pot *= Base; writeln('Total [',sqr(Base),'..',Numb2USA(IntToStr(pot)),'] : ',Numb2USA(IntToStr(total+1))); until false; setlength(LCMsHL,0);
end;
BEGIN
Main(10); Main(100);
END.</lang>
- Output:
Base :10 Total [100..1,000] : 77 Total [100..10,000] : 765 Total [100..100,000] : 7,491 Total [100..1,000,000] : 74,665 Total [100..10,000,000] : 746,286 Total [100..100,000,000] : 7,462,438 Total [100..1,000,000,000] : 74,623,687 Total [100..10,000,000,000] : 746,236,131 Total [100..100,000,000,000] : 7,462,360,431 Total [100..1,000,000,000,000] : 74,623,603,381 Total [100..10,000,000,000,000] : 746,236,032,734 Total [100..100,000,000,000,000] : 7,462,360,326,234 Total [100..1,000,000,000,000,000] : 74,623,603,260,964 Total [100..10,000,000,000,000,000] : 746,236,032,608,141 Total [100..100,000,000,000,000,000] : 7,462,360,326,079,810 Total [100..1,000,000,000,000,000,000] : 74,623,603,260,796,424 Total [100..10,000,000,000,000,000,000] : 746,236,032,607,962,357 Base :100 Total [10000..1,000,000] : 6,039 Total [10000..100,000,000] : 251,482 Total [10000..10,000,000,000] : 24,738,934 Total [10000..1,000,000,000,000] : 2,473,436,586 Total [10000..100,000,000,000,000] : 247,343,160,115 Total [10000..10,000,000,000,000,000] : 24,734,315,489,649 Total [10000..1,000,000,000,000,000,000] : 2,473,431,548,401,507
Perl
<lang perl>use strict; use warnings; use feature 'say';
sub comma { reverse ((reverse shift) =~ s/(.{3})/$1,/gr) =~ s/^,//r }
sub is_gapful { my $n = shift; 0 == $n % join(, (split //, $n)[0,-1]) }
use constant Inf => 1e10; for ([1e2, 30], [1e6, 15], [1e9, 10], [7123, 25]) {
my($start, $count) = @$_; printf "\nFirst $count gapful numbers starting at %s:\n", comma $start; my $n = 0; my $g = ; $g .= do { $n < $count ? (is_gapful($_) and ++$n and "$_ ") : last } for $start .. Inf; say $g;
}</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 First 25 gapful numbers starting at 7,123: 7125 7140 7171 7189 7210 7272 7275 7280 7296 7350 7373 7420 7425 7474 7488 7490 7560 7575 7630 7632 7676 7700 7725 7770 7777
Phix
constant starts = {1e2, 1e6, 1e7, 1e9, 7123}, counts = {30, 15, 15, 10, 25} for i=1 to length(starts) do integer count = counts[i], j = starts[i], pow = 100 while j>=pow*10 do pow *= 10 end while printf(1,"First %d gapful numbers starting at %,d: ", {count, j}) while count do integer fl = floor(j/pow)*10 + remainder(j,10) if remainder(j,fl)==0 then printf(1,"%d ", j) count -= 1 end if j += 1 if j>=10*pow then pow *= 10 end if end while printf(1,"\n") end for
- 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 15 gapful numbers starting at 10,000,000: 10000000 10000001 10000003 10000004 10000005 10000008 10000010 10000016 10000020 10000030 10000032 10000035 10000040 10000050 10000060 First 10 gapful numbers starting at 1,000,000,000: 1000000000 1000000001 1000000005 1000000008 1000000010 1000000016 1000000020 1000000027 1000000030 1000000032 First 25 gapful numbers starting at 7,123: 7125 7140 7171 7189 7210 7272 7275 7280 7296 7350 7373 7420 7425 7474 7488 7490 7560 7575 7630 7632 7676 7700 7725 7770 7777
Plain English
<lang plainenglish>To run: Start up. Show the gapful numbers at various spots. Wait for the escape key. Shut down.
A digit is a number.
To get a digit of a number (last): Privatize the number. Divide the number by 10 giving a quotient and a remainder. Put the remainder into the digit.
To get a digit of a number (first): Privatize the number. Loop. Divide the number by 10 giving a quotient and a remainder. Put the quotient into the number. If the number is 0, put the remainder into the digit; exit. Repeat.
To make a number from the first and last digits of another number: Get a digit of the other number (first). Get another digit of the other number (last). Put the digit times 10 plus the other digit into the number.
To decide if a number is gapful: Make another number from the first and last digits of the number. If the number is evenly divisible by the other number, say yes. Say no.
To show a number of the gapful numbers starting from another number: Privatize the other number. Put 0 into a gapful counter. Loop. If the other number is gapful, write "" then the other number then " " on the console without advancing; bump the gapful counter. If the gapful counter is the number, break. Bump the other number. Repeat. Write "" then the return byte on the console.
To show the gapful numbers at various spots: Write "30 gapful numbers starting at 100:" on the console. Show 30 of the gapful numbers starting from 100. Write "15 gapful numbers starting at 1000000:" on the console. Show 15 of the gapful numbers starting from 1000000. Write "10 gapful numbers starting at 1000000000:" on the console. Show 10 of the gapful numbers starting from 1000000000.</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
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]
Raku
(formerly Perl 6)
Also test starting on a number that doesn't start with 1. Required to have titles, may as well make 'em noble. :-)
<lang perl6>use Lingua::EN::Numbers;
for (1e2, 30, 1e6, 15, 1e9, 10, 7123, 25)».Int -> $start, $count {
put "\nFirst $count gapful numbers starting at {comma $start}:\n" ~ <Sir Lord Duke King>.pick ~ ": ", ~ ($start..*).grep( { $_ %% .comb[0, *-1].join } )[^$count];
}</lang>
- Output:
First 30 gapful numbers starting at 100: Sir: 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: Duke: 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: King: 1000000000 1000000001 1000000005 1000000008 1000000010 1000000016 1000000020 1000000027 1000000030 1000000032 First 25 gapful numbers starting at 7,123: King: 7125 7140 7171 7189 7210 7272 7275 7280 7296 7350 7373 7420 7425 7474 7488 7490 7560 7575 7630 7632 7676 7700 7725 7770 7777
REXX
<lang rexx>/*REXX program computes and displays a series of gapful numbers starting at some number.*/ numeric digits 20 /*ensure enough decimal digits gapfuls.*/ parse arg gapfuls /*obtain optional arguments from the CL*/ if gapfuls= then gapfuls= 30 25@7123 15@1000000 10@1000000000 /*assume defaults.*/
do until gapfuls=; parse var gapfuls stuff gapfuls; call gapful stuff end /*until*/
exit 0 /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ gapful: procedure; parse arg n "@" sp; if sp== then sp= 100 /*get args; use default.*/
say center(' 'n " gapful numbers starting at: " sp' ', 125, "═") $=; #= 0 /*initialize the $ list.*/ do j=sp until #==n /*SP: starting point. */ parse var j a 2 -1 b /*get 1st and last digit*/ 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 ═══════════════════════════════════════════ 25 gapful numbers starting at: 7123 ═══════════════════════════════════════════ 7125 7140 7171 7189 7210 7272 7275 7280 7296 7350 7373 7420 7425 7474 7488 7490 7560 7575 7630 7632 7676 7700 7725 7770 7777 ═════════════════════════════════════════ 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
Ring
<lang Ring> nr = 0 gapful1 = 99 gapful2 = 999999 gapful3 = 999999999 limit1 = 30 limit2 = 15 limit3 = 10
see "First 30 gapful numbers >= 100:" + nl while nr < limit1
gapful1 = gapful1 + 1 gap1 = left((string(gapful1)),1) gap2 = right((string(gapful1)),1) gap = number(gap1 +gap2) if gapful1 % gap = 0 nr = nr + 1 see "" + nr + ". " + gapful1 + nl ok
end see nl
see "First 15 gapful numbers >= 1000000:" + nl nr = 0 while nr < limit2
gapful2 = gapful2 + 1 gap1 = left((string(gapful2)),1) gap2 = right((string(gapful2)),1) gap = number(gap1 +gap2) if (nr < limit2) and gapful2 % gap = 0 nr = nr + 1 see "" + nr + ". " + gapful2 + nl ok
end see nl
see "First 10 gapful numbers >= 1000000000:" + nl nr = 0 while nr < limit3
gapful3 = gapful3 + 1 gap1 = left((string(gapful3)),1) gap2 = right((string(gapful3)),1) gap = number(gap1 +gap2) if (nr < limit2) and gapful3 % gap = 0 nr = nr + 1 see "" + nr + ". " + gapful3 + nl ok
end
</lang>
- Output:
First 30 gapful numbers >= 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 >= 1000000: 1000000, 1000005, 1000008, 1000010, 1000016, 1000020, 1000021, 1000030, 1000032, 1000034, 1000035, 1000040, 1000050, 1000060, 1000065 First 10 gapful numbers >= 1000000000: 1000000000, 1000000001, 1000000005, 1000000008, 1000000010, 1000000016, 1000000020, 1000000027, 1000000030, 1000000032
Ruby
<lang ruby>class Integer
def gapful? a = digits self % (a.last*10 + a.first) == 0 end
end
specs = {100 => 30, 1_000_000 => 15, 1_000_000_000 => 10, 7123 => 25}
specs.each do |start, num|
puts "first #{num} gapful numbers >= #{start}:" p (start..).lazy.select(&:gapful?).take(num).to_a
end </lang>
- Output:
first 30 gapful numbers >= 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 >= 1000000: [1000000, 1000005, 1000008, 1000010, 1000016, 1000020, 1000021, 1000030, 1000032, 1000034, 1000035, 1000040, 1000050, 1000060, 1000065] first 10 gapful numbers >= 1000000000: [1000000000, 1000000001, 1000000005, 1000000008, 1000000010, 1000000016, 1000000020, 1000000027, 1000000030, 1000000032] first 25 gapful numbers >= 7123: [7125, 7140, 7171, 7189, 7210, 7272, 7275, 7280, 7296, 7350, 7373, 7420, 7425, 7474, 7488, 7490, 7560, 7575, 7630, 7632, 7676, 7700, 7725, 7770, 7777]
Sidef
Concept extended to other bases: <lang ruby>func is_gapful(n, base=10) {
n.is_div(base*floor(n / base**n.ilog(base)) + n%base)
}
var task = [
"(Required) The first %s gapful numbers (>= %s)", 30, 1e2, 10, "(Required) The first %s gapful numbers (>= %s)", 15, 1e6, 10, "(Required) The first %s gapful numbers (>= %s)", 10, 1e9, 10, "(Extra) The first %s gapful numbers (>= %s)", 10, 987654321, 10, "(Extra) The first %s gapful numbers (>= %s)", 10, 987654321, 12,
]
task.each_slice(4, {|title, n, from, b|
say sprintf("\n#{title} for base #{b}:", n, from.commify) say (from..Inf -> lazy.grep{ is_gapful(_,b) }.first(n).join(' '))
})</lang>
- Output:
(Required) The first 30 gapful numbers (>= 100) for base 10: 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 (Required) The first 15 gapful numbers (>= 1,000,000) for base 10: 1000000 1000005 1000008 1000010 1000016 1000020 1000021 1000030 1000032 1000034 1000035 1000040 1000050 1000060 1000065 (Required) The first 10 gapful numbers (>= 1,000,000,000) for base 10: 1000000000 1000000001 1000000005 1000000008 1000000010 1000000016 1000000020 1000000027 1000000030 1000000032 (Extra) The first 10 gapful numbers (>= 987,654,321) for base 10: 987654330 987654334 987654336 987654388 987654420 987654485 987654510 987654513 987654592 987654600 (Extra) The first 10 gapful numbers (>= 987,654,321) for base 12: 987654325 987654330 987654336 987654360 987654368 987654384 987654388 987654390 987654393 987654395
Swift
<lang swift>func isGapful(n: Int) -> Bool {
guard n > 100 else { return true }
let asString = String(n) let div = Int("\(asString.first!)\(asString.last!)")!
return n % div == 0
}
let first30 = (100...).lazy.filter(isGapful).prefix(30) let mil = (1_000_000...).lazy.filter(isGapful).prefix(15) let bil = (1_000_000_000...).lazy.filter(isGapful).prefix(15)
print("First 30 gapful numbers: \(Array(first30))") print("First 15 >= 1,000,000: \(Array(mil))") print("First 15 >= 1,000,000,000: \(Array(bil))")</lang>
- Output:
First 30 gapful numbers: [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 >= 1,000,000: [1000000, 1000005, 1000008, 1000010, 1000016, 1000020, 1000021, 1000030, 1000032, 1000034, 1000035, 1000040, 1000050, 1000060, 1000065] First 15 >= 1,000,000,000: [1000000000, 1000000001, 1000000005, 1000000008, 1000000010, 1000000016, 1000000020, 1000000027, 1000000030, 1000000032, 1000000035, 1000000039, 1000000040, 1000000050, 1000000053]
Tcl
<lang Tcl>proc ungap n {
if {[string length $n] < 3} { return $n } return [string index $n 0][string index $n end]
}
proc gapful n {
return [expr {0 == ($n % [ungap $n])}]
}
- --> list of gapful numbers >= n
proc GFlist {count n} {
set r {} while {[llength $r] < $count} { if {[gapful $n]} { lappend r $n } incr n } return $r
}
proc show {count n} {
puts "The first $count gapful >= $n: [GFlist $count $n]"
}
show 30 100 show 15 1000000 show 10 1000000000 </lang>
- Output:
The first 30 gapful >= 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 The first 15 gapful >= 1000000: 1000000 1000005 1000008 1000010 1000016 1000020 1000021 1000030 1000032 1000034 1000035 1000040 1000050 1000060 1000065 The first 10 gapful >= 1000000000: 1000000000 1000000001 1000000005 1000000008 1000000010 1000000016 1000000020 1000000027 1000000030 1000000032
Visual Basic .NET
<lang vbnet>Module Module1
Function FirstNum(n As Integer) As Integer REM Divide by ten until the leading digit remains. While n >= 10 n /= 10 End While Return n End Function
Function LastNum(n As Integer) As Integer REM Modulo gives you the last digit. Return n Mod 10 End Function
Sub FindGap(n As Integer, gaps As Integer) Dim count = 0 While count < gaps Dim i = FirstNum(n) * 10 + LastNum(n)
REM Modulo with our new integer and output the result. If n Mod i = 0 Then Console.Write("{0} ", n) count += 1 End If
n += 1 End While Console.WriteLine() Console.WriteLine() End Sub
Sub Main() Console.WriteLine("The first 30 gapful numbers are: ") FindGap(100, 30)
Console.WriteLine("The first 15 gapful numbers > 1,000,000 are: ") FindGap(1000000, 15)
Console.WriteLine("The first 10 gapful numbers > 1,000,000,000 are: ") FindGap(1000000000, 10) End Sub
End Module</lang>
- Output:
The first 30 gapful numbers are: 100 105 108 110 120 121 130 132 135 140 143 156 160 168 175 180 200 220 225 231 240 242 253 270 300 315 330 341 360 400 The first 15 gapful numbers > 1,000,000 are: 1000000 1000005 1000008 1000010 1000016 1000020 1000021 1000030 1000032 1000034 1000035 1000040 1000050 1000060 1000065 The first 10 gapful numbers > 1,000,000,000 are: 1000000000 1000000001 1000000005 1000000008 1000000010 1000000016 1000000020 1000000027 1000000030 1000000032
Wren
<lang ecmascript>import "/fmt" for Fmt
var starts = [1e2, 1e6, 1e7, 1e9, 7123] var counts = [30, 15, 15, 10, 25] for (i in 0...starts.count) {
var count = 0 var j = starts[i] var pow = 100 while (true) { if (j < pow * 10) break pow = pow * 10 } System.print("First %(counts[i]) gapful numbers starting at %(Fmt.dc(0, starts[i]))") while (count < counts[i]) { var fl = (j/pow).floor*10 + (j % 10) if (j%fl == 0) { System.write("%(j) ") count = count + 1 } j = j + 1 if (j >= 10*pow) pow = pow * 10 } System.print("\n")
}</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 15 gapful numbers starting at 10,000,000 10000000 10000001 10000003 10000004 10000005 10000008 10000010 10000016 10000020 10000030 10000032 10000035 10000040 10000050 10000060 First 10 gapful numbers starting at 1,000,000,000 1000000000 1000000001 1000000005 1000000008 1000000010 1000000016 1000000020 1000000027 1000000030 1000000032 First 25 gapful numbers starting at 7,123 7125 7140 7171 7189 7210 7272 7275 7280 7296 7350 7373 7420 7425 7474 7488 7490 7560 7575 7630 7632 7676 7700 7725 7770 7777
Yabasic
<lang Yabasic>sub is_gapful(n)
m = n
l = mod(n, 10)
while (m >= 10) m = int(m / 10) wend return (m * 10) + l
end sub
sub muestra_gapful(n, gaps)
inc = 0 print "Primeros ", gaps, " numeros gapful >= ", n while inc < gaps if mod(n, is_gapful(n)) = 0 then print " " , n , inc = inc + 1 end if n = n + 1 wend print chr$(10)
end sub
muestra_gapful(100, 30) muestra_gapful(1000000, 15) muestra_gapful(1000000000, 10) muestra_gapful(7123,25) end</lang>
- Output:
Primeros 30 numeros gapful >= 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 Primeros 15 numeros gapful >= 1000000 1000000 1000005 1000008 1000010 1000016 1000020 1000021 1000030 1000032 1000034 1000035 1000040 1000050 1000060 1000065 Primeros 10 numeros gapful >= 1000000000 1000000000 1000000001 1000000005 1000000008 1000000010 1000000016 1000000020 1000000027 1000000030 1000000032 Primeros 25 numeros gapful >= 7123 7125 7140 7171 7189 7210 7272 7275 7280 7296 7350 7373 7420 7425 7474 7488 7490 7560 7575 7630 7632 7676 7700 7725 7770 7777 ---Program done, press RETURN---
zkl
<lang zkl>fcn gapfulW(start){ //--> iterator
[start..].tweak( fcn(n){ if(n % (10*n.toString()[0] + n%10)) Void.Skip else n })
}</lang> <lang zkl>foreach n,z in
( T( T(100, 30), T(1_000_000, 15), T(1_000_000_000, 10), T(7_123,25) )){ println("First %d gapful numbers starting at %,d:".fmt(z,n)); gapfulW(n).walk(z).concat(", ").println("\n");
}</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 First 25 gapful numbers starting at 7,123: 7125, 7140, 7171, 7189, 7210, 7272, 7275, 7280, 7296, 7350, 7373, 7420, 7425, 7474, 7488, 7490, 7560, 7575, 7630, 7632, 7676, 7700, 7725, 7770, 7777