Gapful numbers: Difference between revisions
m
syntax highlighting fixup automation
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 42:
{{trans|Python}}
<
print("\nFirst "n‘ gapful numbers from ’start)
[Int] l
Line 50:
I l.len == n
L.break
print(l)</
{{out}}
Line 66:
=={{header|Ada}}==
<
procedure Gapful_Numbers is
Line 109:
Find_Gapful (From => 1_000_000_000, Count => 10);
New_Line;
end Gapful_Numbers;</
{{out}}
<pre>First 30 gapful numbers over 100:
Line 122:
=={{header|ALGOL 68}}==
<
# where f is the first digit and b is the final digit #
# unary GAPFUL operator - returns TRUE if n is gapful #
Line 160:
print gapful( 15 GAPFUL 1 000 000, 1 000 000 );
print gapful( 10 GAPFUL 1 000 000 000, 1 000 000 000 )
END</
{{out}}
<pre>
Line 173:
=={{header|AppleScript}}==
<
set units to n mod 10
set temp to n div 10
Line 203:
set output to output as text
set AppleScript's text item delimiters to astid
return output</
{{output}}
<
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
Line 213:
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"</
=={{header|Applesoft BASIC}}==
{{trans|C}}
Numbers are printed digit by digit so that we can see what they actually are because large numbers would normally be printed in scientific notation.
<
110 COUNT = 30
120 GOSUB 230"GENERATE GAPS"
Line 254:
440 NEXT P
450 PRINT MID$(M$ + " " + P$, 2 - (POS(0) + LEN(P$) + 1 > PEEK(33))) ;
460 RETURN</
=={{header|Arturo}}==
<
s: to :string n
divisor: to :integer (first s) ++ last s
Line 280:
]
print "\n"
]</
{{out}}
Line 305:
=={{header|AutoHotkey}}==
<
counter:= 0, output := ""
while (counter < Qty){
Line 314:
}
return output
}</
Examples:<
MsgBox, 262144, , % Gapful_numbers(1000000, 15)
MsgBox, 262144, , % Gapful_numbers(1000000000, 10)</
{{out}}
<pre>
Line 380:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f GAPFUL_NUMBERS.AWK
# converted from C++
Line 407:
printf("\n")
}
</syntaxhighlight>
{{out}}
<pre>
Line 419:
=={{header|BASIC256}}==
{{trans|Yabasic}}
<
m = n
l = n mod 10
Line 445:
call muestra_gapful(1000000000, 10)
call muestra_gapful(7123,25)
end</
{{out}}
<pre>
Line 453:
=={{header|C}}==
<syntaxhighlight lang="c">
#include<stdio.h>
Line 489:
return 0;
}
</syntaxhighlight>
Output :
<pre>First 30 Gapful numbers >= 100 :
Line 562:
=={{header|C++}}==
<
bool gapful(int n) {
Line 589:
show_gapful_numbers(1000000000, 10);
return 0;
}</
{{out}}
Line 602:
=={{header|C#}}==
<
using System;
Line 669:
</syntaxhighlight>
{{out}}
<pre>
Line 684:
=={{header|Clojure}}==
<
(defn last-digit [n] (mod n 10))
Line 704:
""])))
(doall (map report-range [ [1 30] [1000000 15] [1000000000 10] ]))</
{{Out}}
Line 721:
numbers have to be on separate lines.
<
PROGRAM-ID. GAPFUL.
Line 768:
ADD 1 TO N.
IF GAP-AMOUNT IS GREATER THAN 0
GO TO CHECK-GAPFUL-NUMBER.</
{{out}}
<pre style='height: 50ex;'>First 30 gapful numbers >= 100:
Line 837:
Numbers >= 1,000,000,000 are printed out in scientific notation and we can't see what they actually are, so I used 100,000,000 instead.
<
110 DEF FNLD(N) = N - 10 * INT(N/10)
120 DEF FNBE(N) = 10 * FNFD(N) + FNLD(N)
Line 855:
260 END
270 DATA 1,30, 1000000,15, 100000000,10
280 DATA -1</
{{Out}}
Line 883:
=={{header|Common Lisp}}==
{{trans|Clojure}}
<
(defun last-digit (n) (mod n 10))
Line 904:
(mapcar #'report-range '((1 30) (1000000 15) (1000000000 10)))
</syntaxhighlight>
{{Out}}
Line 924:
{{trans|Ruby}}
With lazy iterator
<
def gapful?
a = self.to_s.chars.map(&.to_i)
Line 936:
puts "first #{count} gapful numbers >= #{start}:"
puts (start..).each.select(&.gapful?).first(count).to_a, "\n"
end</
Alternative
<
def gapful?
a = self.to_s.chars.map(&.to_i)
Line 953:
(start..).each { |n| n.gapful? && (gapful << n; i += 1); break if i == count }
puts gapful, "\n"
end</
{{out}}
<pre>first 30 gapful numbers >= 100:
Line 969:
=={{header|D}}==
{{trans|Go}}
<
import std.stdio;
Line 1,010:
writeln("\n");
}
}</
{{out}}
<pre>First 30 gapful numbers starting at 100:
Line 1,035:
The gapfulness implementation:
<
-export([first_digit/1, last_digit/1, bookend_number/1, is_gapful/1]).
Line 1,045:
bookend_number(N) -> 10 * first_digit(N) + last_digit(N).
is_gapful(N) -> (N >= 100) and (0 == N rem bookend_number(N)).</
The streams implementation:
<
-export([yield/1, naturals/0, naturals/1, filter/2, take/2, to_list/1]).
Line 1,086:
{X, Xs} -> to_list(Xs, [X|Acc]);
halt -> lists:reverse(Acc)
end.</
The main program that puts them together:
<
-mode(compile).
Line 1,098:
stream:naturals(Start))))]).
main(_) -> lists:map(fun report_range/1, [[1,30],[1000000,15],[1000000000,10]]).</
{{Out}}
Line 1,112:
=={{header|Factor}}==
<
math.text.utils sequences ;
Line 1,121:
2dup lfrom [ gapful? ] lfilter ltake list>array
"%d gapful numbers starting at %d:\n%[%d, %]\n\n" printf
] 2tri@</
{{out}}
<pre>
Line 1,136:
=={{header|Forth}}==
Developed with Gforth 0.7.9
<
: Int>Str s>d <# #s #> ;
: firstDigit C@ [char] 0 - ;
Line 1,156:
1000000 15 main cr
1000000000 10 main cr
</syntaxhighlight>
{{out}}
<pre>
Line 1,220:
=={{header|FreeBASIC}}==
<
function is_gapful( n as uinteger ) as boolean
if n<100 then return false
Line 1,258:
i += 1
wend
print</
{{out}}
<pre>
Line 1,272:
=={{header|Frink}}==
<
// Create function to calculate gapful number
gapful[num,totalCounter] :=
Line 1,301:
gapful[1000000,15]
gapful[1000000000,10]
</syntaxhighlight>
{{out}}
Line 1,322:
=={{header|Go}}==
<
import "fmt"
Line 1,362:
fmt.Println("\n")
}
}</
{{out}}
Line 1,384:
=={{header|Groovy}}==
{{trans|Java}}
<
private static String commatize(long n) {
StringBuilder sb = new StringBuilder(Long.toString(n))
Line 1,422:
}
}
}</
{{out}}
<pre>First 30 gapful numbers starting at 100
Line 1,440:
=={{header|Haskell}}==
<
gapful :: Int -> Bool
Line 1,453:
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</
{{out}}
<pre>
Line 1,468:
Or, defining the predicate in applicative terms, and wrapping the output:
<
import Data.List.Split (chunksOf)
Line 1,500:
(read (ws !! 1))
[read (ws !! 5) :: Int ..]
)</
{{Out}}
<pre>First 30 gapful numbers >= 100:
Line 1,523:
=={{header|J}}==
<syntaxhighlight lang="text">
gapful =: 0 = (|~ ({.,{:)&.(10&#.inv))
Line 1,531:
'The first ' , (": y) , ' gapful numbers exceeding ' , (":<:x) , ' are ' , (":gn)
)
</syntaxhighlight>
<pre>
task 30
Line 1,544:
{{trans|D}}
<
public class GapfulNumbers {
Line 1,581:
}
}
}</
{{out}}
<pre>First 30 gapful numbers starting at 100:
Line 1,601:
===Windows command line version===
{{works with|Windows Script Host}}
<
function gapfulness_divisor (number) {
var digit_string = number.toString(10)
Line 1,656:
print_gapfuls_with_header(100, 30)
print_gapfuls_with_header(1000000, 15)
print_gapfuls_with_header(1000000000, 10)</
{{Output}}
Line 1,667:
===ES6===
<
'use strict';
Line 1,874:
// MAIN ---
return main();
})();</
{{Out}}
<pre>First 30 gapful numbers >= 100:
Line 1,900:
The following program works with both jq and gojq; for very large integers, the latter should be used.
<
# which is assumed to be an integer
def gapful($start):
Line 1,914:
([limit(15;gapful(1000000))] | join(" ")),
"First 10 gapful numbers starting from 10^9:",
([limit(10;gapful(pow(10;9)))] | join(" "))</
{{out}}
<pre>First 30 gapful numbers starting from 100:
Line 1,923:
1000000000 1000000001 1000000005 1000000008 1000000010 1000000016 1000000020 1000000027 1000000030 1000000032</pre>
=={{header|Julia}}==
<
firstlast(a) = 10 * a[end] + a[1]
Line 1,933:
take(n, gapfuls(x)))
end
</
<pre>
First 30 gapful numbers starting at 100:
Line 1,945:
=={{header|Kotlin}}==
{{trans|Java}}
<
val sb = StringBuilder(n.toString())
val le = sb.length
Line 1,984:
println('\n')
}
}</
{{out}}
<pre>First 30 gapful numbers starting at 100:
Line 2,002:
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
{def gapfuls
{lambda {:n :i :N}
Line 2,023:
-> 1000000000 1000000001 1000000005 1000000008 1000000010
1000000016 1000000020 1000000027 1000000030 1000000032
</syntaxhighlight>
=={{header|Logo}}==
{{trans|Clojure}}
<
output sum product 10 first :n last :n
end
Line 2,055:
apply "report_range ?
]
</syntaxhighlight>
{{Out}}
Line 2,069:
=={{header|LOLCODE}}==
{{trans|Clojure}}
<
HOW IZ I FurstDigit YR Numbr
Line 2,133:
I IZ Report YR 1000000000 AN YR 10 MKAY
KTHXBYE
</syntaxhighlight>
{{Out}}
Line 2,148:
=={{header|Lua}}==
{{trans|C}}
<
local counter = 0
local i = start
Line 2,172:
generateGaps(1000000000, 15)
print()</
{{out}}
<pre>First 30 Gapful numbers >= 100 :
Line 2,241:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
GapFulQ[n_Integer] := Divisible[n, FromDigits[IntegerDigits[n][[{1, -1}]]]]
i = 100;
Line 2,263:
i++
]
res</
{{out}}
<pre>{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}
Line 2,271:
=={{header|min}}==
{{works with|min|0.19.6}}
<
(((10 mod) (10 div)) cleave) :moddiv
((dup 0 ==) (pop new) 'moddiv 'cons linrec) :digits
Line 2,293:
100 30 show-gapfuls newline
1000000 15 show-gapfuls newline
1000000000 10 show-gapfuls</
{{out}}
<pre>
Line 2,307:
=={{header|newLISP}}==
<
;; Create an integer out of the first and last digits of a given integer
Line 2,355:
(show-gapfuls 15 999999)
(show-gapfuls 10 999999999)
(exit)</
{{out}}
Line 2,366:
=={{header|Nim}}==
<
Line 2,406:
displayGapfulNumbers(100, 30)
displayGapfulNumbers(1_000_000, 15)
displayGapfulNumbers(1_000_000_000, 10)</
{{out}}
Line 2,423:
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
<
Line 2,602:
end;
{$IFNDEF LINUX} readln; {$ENDIF}
end.</
{{out}}
<pre style="font-size:80%">First 30, gapful numbers starting at 100
Line 2,628:
//100|7462360431 =>100*1000*1000*1000 </pre>
===only counting===
<
{$IFDEF FPC}
{$MODE DELPHI}{$OPTIMIZATION ON,ALL}
Line 2,722:
Main(10);
Main(100);
END.</
{{out}}
<pre>Base :10
Line 2,753:
=={{header|Perl}}==
<
use warnings;
use feature 'say';
Line 2,768:
$g .= do { $n < $count ? (is_gapful($_) and ++$n and "$_ ") : last } for $start .. Inf;
say $g;
}</
{{out}}
<pre>First 30 gapful numbers starting at 100:
Line 2,784:
=={{header|Phix}}==
{{trans|Go}}
<!--<
<span style="color: #008080;">constant</span> <span style="color: #000000;">starts</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1e2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1e6</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1e7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1e9</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7123</span><span style="color: #0000FF;">},</span>
<span style="color: #000000;">counts</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">30</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">15</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">15</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">10</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">25</span><span style="color: #0000FF;">}</span>
Line 2,806:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
{{out}}
<pre style="font-size: 10px">
Line 2,817:
=={{header|Plain English}}==
<
Start up.
Show the gapful numbers at various spots.
Line 2,864:
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.</
{{out}}
<pre>
Line 2,879:
=={{header|PL/M}}==
This can be compiled with the original 8080 PL/M compiler and run under CP/M or a clone or emulator. Just shows the first 30 Gapful numbers >= 100, as the original 8080 PL/M only supports at most (unsigned) 16-bit numbers.
<
/* THE FIRST DIGIT AND L IS THE LAST DIGIT */
BDOS: PROCEDURE( FN, ARG ); /* CP/M BDOS SYSTEM CALL */
Line 2,927:
CALL PR$NL;
EOF</
{{out}}
<pre>
Line 2,936:
=={{header|PowerShell}}==
{{trans|Clojure}}
<
param ( [int] $Number )
[int]$Number.ToString().Substring(0,1)
Line 2,979:
Search-Range 1000000 15
Search-Range 1000000000 10
</syntaxhighlight>
{{Out}}
<pre>The first 30 gapful numbers >= 1:
Line 2,992:
=={{header|PureBasic}}==
<
n1.i=n%10
n2.i=Val(Left(Str(n),1))
Line 3,021:
PrintN(~"\nFirst 10 gapful numbers ≥ 1,000,000,000:")
PutGapNum(1000000000,10,5)
Input()</
{{out}}
<pre>First 30 gapful numbers ≥ 100:
Line 3,038:
=={{header|Python}}==
<
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)))</
{{out}}
Line 3,061:
Also test starting on a number that ''doesn't'' start with 1. Required to have titles, may as well make 'em noble. :-)
<syntaxhighlight lang="raku"
for (1e2, 30, 1e6, 15, 1e9, 10, 7123, 25)».Int -> $start, $count {
Line 3,067:
<Sir Lord Duke King>.pick ~ ": ", ~
($start..*).grep( { $_ %% .comb[0, *-1].join } )[^$count];
}</
{{out}}
<pre>First 30 gapful numbers starting at 100:
Line 3,082:
=={{header|REXX}}==
<
numeric digits 20 /*ensure enough decimal digits gapfuls.*/
parse arg gapfuls /*obtain optional arguments from the CL*/
Line 3,099:
#= # + 1; $= $ j /*bump #; append ──► $ */
end /*j*/
say strip($); say; return</
{{out|output|text= when using the default inputs:}}
Line 3,119:
=={{header|Ring}}==
<syntaxhighlight lang="ring">
nr = 0
gapful1 = 99
Line 3,168:
end
</syntaxhighlight>
{{out}}
<pre>
Line 3,180:
=={{header|Ruby}}==
<
def gapful?
a = digits
Line 3,193:
p (start..).lazy.select(&:gapful?).take(num).to_a
end
</syntaxhighlight>
{{out}}
<pre>first 30 gapful numbers >= 100:
Line 3,207:
=={{header|Scheme}}==
{{trans|Clojure}}
<
(define (last-digit n) (modulo n 10))
(define (bookend-number n) (+ (* 10 (first-digit n)) (last-digit n)))
Line 3,226:
(map report-range '((100 30) (1000000 15) (1000000000 10)))
</syntaxhighlight>
{{Out}}
<pre>The first 30 gapful numbers >= 100:
Line 3,239:
=={{header|Sidef}}==
Concept extended to other bases:
<
n.is_div(base*floor(n / base**n.ilog(base)) + n%base)
}
Line 3,254:
say sprintf("\n#{title} for base #{b}:", n, from.commify)
say (from..Inf -> lazy.grep{ is_gapful(_,b) }.first(n).join(' '))
})</
{{out}}
<pre>
Line 3,277:
This is not a particularly efficient solution, but it gets the job done.
<syntaxhighlight lang="sql">
/*
This code is an implementation of gapful numbers in SQL ORACLE 19c
Line 3,312:
select gapful_numbers(1000000000,10) as res from dual;
/
</syntaxhighlight>
{{out}}
Line 3,323:
=={{header|Swift}}==
<
guard n > 100 else {
return true
Line 3,340:
print("First 30 gapful numbers: \(Array(first30))")
print("First 15 >= 1,000,000: \(Array(mil))")
print("First 15 >= 1,000,000,000: \(Array(bil))")</
{{out}}
Line 3,349:
=={{header|Tcl}}==
<
if {[string length $n] < 3} {
return $n
Line 3,379:
show 15 1000000
show 10 1000000000
</syntaxhighlight>
{{out}}
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
Line 3,388:
{{works with|Bourne Again Shell}}
{{trans|Clojure}}
<
printf '%s\n' "${1:0:1}"
}
Line 3,430:
}
report-ranges 1,30 1000000,15 1000000000,10</
{{Out}}
Line 3,446:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<
Function FirstNum(n As Integer) As Integer
Line 3,489:
End Sub
End Module</
{{out}}
<pre>The first 30 gapful numbers are:
Line 3,502:
=={{header|Vlang}}==
{{trans|go}}
<
mut s := n.str()
le := s.len
Line 3,538:
println("\n")
}
}</
{{out}}
Line 3,561:
{{trans|Go}}
{{libheader|Wren-fmt}}
<
var starts = [1e2, 1e6, 1e7, 1e9, 7123]
Line 3,584:
}
System.print("\n")
}</
{{out}}
Line 3,605:
=={{header|XBS}}==
<
set s:string = tostring(n);
set d = toint(`{s::at(0)}{s::at(?s-1)}`);
Line 3,628:
findGapfulNumbers(100,30);
findGapfulNumbers(1000000,15);
findGapfulNumbers(1000000000,15);</
{{out}}
<pre>
Line 3,637:
=={{header|XPL0}}==
<
int N0, N, First, Last;
[N:= N0;
Line 3,667:
ShowGap(1_000_000, 15);
ShowGap(1_000_000_000, 10);
]</
{{out}}
Line 3,680:
=={{header|Yabasic}}==
<
m = n
l = mod(n, 10)
Line 3,706:
muestra_gapful(1000000000, 10)
muestra_gapful(7123,25)
end</
{{out}}
<pre>
Line 3,725:
=={{header|zkl}}==
<
[start..].tweak(
fcn(n){ if(n % (10*n.toString()[0] + n%10)) Void.Skip else n })
}</
<
( 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");
}</
{{out}}
<pre>
|