Palindromic gapful numbers: Difference between revisions

m
m (→‎{{header|Raku}}: work around concurrency problem in recent Raku)
m (→‎{{header|Wren}}: Minor tidy)
 
(14 intermediate revisions by 6 users not shown)
Line 57:
=={{header|AppleScript}}==
 
<langsyntaxhighlight lang="applescript">on doTask()
set part1 to {"First 20 palindromic gapful numbers > 100 ending with each digit from 1 to 9:"}
set part2 to {"86th to 100th such:"}
Line 141:
end intText
 
return doTask()</langsyntaxhighlight>
 
{{output}}
Line 180:
See [https://www.rosettacode.org/wiki/Palindromic_gapful_numbers#Ludicrously_fast_to_10.2C000.2C000.2C000.2C000.2C000.2C000th the original's] comments for an explanation of the logic. While this translation was developed directly from the Phix, comparing the two may be difficult as I've pulled things around a bit for AppleScript efficiency and have relabelled some of the variables for consistency and to help me understand the process myself. On my machine, this version takes 0.12 seconds to carry out the set tasks — only a tad slower than the hard-wired script above. The batch of extreme tests in the Phix demo takes around 1.6 seconds.
 
<langsyntaxhighlight lang="applescript">-- Return a script object containing the main handlers.
-- It could be loaded as a library instead if there were any point in having such a library.
on getWorks()
Line 336:
end doTask
 
doTask()</langsyntaxhighlight>
 
{{output}}
Line 444:
=={{header|C}}==
{{trans|C++}}
<langsyntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
Line 536:
 
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 576:
=={{header|C++}}==
The idea of generating palindromes first then testing for gapfulness was borrowed from other solutions.
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <cstdint>
 
Line 665:
 
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 705:
=={{header|Crystal}}==
===Brute force and slow===
<langsyntaxhighlight lang="ruby">def palindromesgapful(digit, pow)
r1 = (10_u64**pow + 1) * digit
r2 = 10_u64**pow * (digit + 1)
Line 732:
count = 1000
puts "\nLast 10 of first 1000 palindromic gapful numbers ending in:"
(1..9).each { |digit| print "#{digit} : #{digitscount(digit, count).last(10)}\n" }</langsyntaxhighlight>
 
===Orders of Magnitude Faster: Direct Generation of Numbers===
Line 741:
 
Optimized version using number<->string conversion: 21.5 secs
<langsyntaxhighlight lang="ruby">def palindromicgapfuls(digit, count)
gapfuls = [] of UInt64 # array of palindromic gapfuls
dd = 11_u64 * digit # digit gapful divisor: 11, 22,...88, 99
Line 796:
1.upto(9) { |digit| puts "#{digit} : #{palindromicgapfuls(digit, count).last(keep)}" }
 
puts (Time.monotonic - start).total_seconds</langsyntaxhighlight>
 
Compact version: 22.0 secs
<langsyntaxhighlight lang="ruby">def palindromicgapfuls(digit, count)
gapfuls = [] of UInt64 # array of palindromic gapfuls
dd = 11_u64 * digit # digit gapful divisor: 11, 22,...88, 99
Line 847:
1.upto(9) { |digit| puts "#{digit} : #{palindromicgapfuls(digit, count).last(keep)}" }
 
puts (Time.monotonic - start).total_seconds</langsyntaxhighlight>
 
Object Oriented implementation: same speed - 21.8 seconds
<Br>Here using a Struct (allocated on stack) is faster than using a Class (allocated on heap)
<langsyntaxhighlight lang="ruby">struct PalindromicGapfuls
include Enumerable(UInt64)
 
Line 912:
1.upto(9) { |digit| puts "#{digit} : #{PalindromicGapfuls.new(digit).first(count).last(keep)}" }
 
puts (Time.monotonic - start).total_seconds</langsyntaxhighlight>
 
Original version optimized for minimal memory use: 24.6 secs
<langsyntaxhighlight lang="ruby">def palindromicgapfuls(digit, count, keep)
palcnt = 0 # count of gapful palindromes
to_skip = count - keep # count of unwanted values to skip
Line 971:
1.upto(9) { |digit| puts "#{digit} : #{palindromicgapfuls(digit, count, keep)}" }
 
puts (Time.monotonic - start).total_seconds</langsyntaxhighlight>
 
Compact version optimized for minimal memory use: 24.5 secs
<langsyntaxhighlight lang="ruby">def palindromicgapfuls(digit, count, keep)
palcnt = 0 # count of gapful palindromes
to_skip = count - keep # count of unwanted values to skip
Line 1,024:
1.upto(9) { |digit| puts "#{digit} : #{palindromicgapfuls(digit, count, keep)}" }
 
puts (Time.monotonic - start).total_seconds</langsyntaxhighlight>
 
OOP version optimized for minimal memory use - 25.4 secs
<BR>It creates an output method that skips the unwanted values and only keeps/stores the desired ones.
<langsyntaxhighlight lang="ruby">struct PalindromicGapfuls
include Enumerable(UInt64)
 
Line 1,099:
1.upto(9) { |digit| puts "#{digit} : #{PalindromicGapfuls.new(digit).keep_from(count, keep)}" }
 
puts (Time.monotonic - start).total_seconds</langsyntaxhighlight>
 
Compact minimized memory version that numerically create palindromes: 9.2 secs
<langsyntaxhighlight lang="ruby">def palindromicgapfuls(digit, count, keep)
palcnt = 0 # count of gapful palindromes
to_skip = count - keep # count of unwanted values to skip
Line 1,160:
1.upto(9) { |digit| puts "#{digit} : #{palindromicgapfuls(digit, count, keep)}" }
 
puts (Time.monotonic - start).total_seconds</langsyntaxhighlight>
 
OOP version optimized for minimal memory use, palindromes created numerically - 9.59 secs
<langsyntaxhighlight lang="ruby">struct PalindromicGapfuls
include Enumerable(UInt64)
Line 1,236:
1.upto(9) { |digit| puts "#{digit} : #{PalindromicGapfuls.new(digit).keep_from(count, keep)}" }
puts (Time.monotonic - start).total_seconds</langsyntaxhighlight>
 
{{out}}
Line 1,313:
Run as: $ crystal run --release fsharp2crystal.cr
Best Time: 29.455717914 secs
<langsyntaxhighlight lang="ruby">class PalNo
@digit : UInt64
@dd : UInt64
Line 1,355:
 
puts (Time.monotonic - start).total_seconds
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,418:
[968787783387787869]
####
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
This is among the faster versions of the problem. It solves the standard and optional tasks in 0.7 seconds.
 
<syntaxhighlight lang="Delphi">
 
{-------------Library Routines ----------------------------------------------------------------}
procedure GetDigits(N: integer; var IA: TIntegerDynArray);
{Get an array of the integers in a number}
{Numbers returned from least to most significant}
var T,I,DC: integer;
begin
DC:=Trunc(Log10(N))+1;
SetLength(IA,DC);
for I:=0 to DC-1 do
begin
T:=N mod 10;
N:=N div 10;
IA[I]:=T;
end;
end;
 
function GetNKPalindrome(N,K: int64): int64;
{Get Nth Palindrome with K-number of digits}
{N = Left half, Right = Reversed(left) a }
var Temp,H1,H2,I: int64;
begin
{Get left digit count - depends on K being odd/even}
if (K and 1)<>0 then Temp:=K div 2 else Temp:=K div 2 - 1;
{Get power of 10 digits}
H1:=trunc(Power(10, Temp));
{Add in N}
H1:=H1 + N - 1;
H2:=H1;
{ If K is odd, truncate the last digit}
if (k and 1)<>0 then H2:=H2 div 10;
{Reverse H2 and add to H1}
while H2>0 do
begin
H1:=H1 * 10 + (H2 mod 10);
H2:=H2 div 10;
end;
Result:=H1;
end;
 
 
 
function GetPalDigits(N: int64; var Offset: int64): integer;
{Get number of digits and offset for Nth Palindrome}
{Used to feed GetNKPalindrome to find Nth Palindrome}
var R1,R2,Step: int64;
begin
R1:=0;
{Step through number of digits}
for Result:=1 to 36 do
begin
{Calculate new Range step: 9,9,90,90,90,900,900...}
if (Result and 1)<>0 then Step:=9 * Trunc(Power(10,Result div 2));
{Calculate R2}
R2:=(R1 + Step)-1;
{See if N falls between R1 and R2}
if (N>=R1) and (N<=R2) then
begin
{Calculate offset and exit}
Offset:=(N - R1)+1;
exit;
end;
R1:=R2+1;
end;
end;
 
 
function GetNthPalindrome(N: integer): int64;
{Get the Nth Palindrome number}
var D,Off: int64;
begin
D:=GetPalDigits(N,Off);
Result:=GetNKPalindrome(Off,D);
end;
 
 
 
procedure GetPalindromeList(Count: integer; var Pals: TInt64DynArray);
{Get a list of the first "Count"-number of palinedromes (Fast)}
var D,I,Inx,Max: integer;
begin
{Set array length}
SetLength(Pals,Count);
Inx:=0;
{Handle palindromes up to 18 digits}
for D:=1 to 18 do
begin
{Get maximum count for palindrom of D digits}
if (D and 1)=1 then Max:=Trunc(Power(10,(D + 1) div 2))
else Max:=Trunc(Power(10,D div 2));
{Step through all the numbers half the size of the number of digits}
for I:=1 to Max-Max div 10 do
begin
{Store palindrome}
Pals[Inx]:=GetNKPalindrome(I,D);
Inc(Inx);
{Exit when array is full}
if Inx>=Count then break;
end;
end;
end;
 
 
{------------------------------------------------------------------------------------------------}
 
 
function IsGapful(N: int64): boolean;
{Return true if number is "GapFul"}
{GapFul = combined first/last}
{ digits divide evenly into N}
var Digits: TIntegerDynArray;
var I: int64;
begin
Result:=False;
{Must be 3 digit number}
if N<100 then exit;
{Put digits in array}
GetDigits(N,Digits);
{Form number from first and last digit}
I:=Digits[0] + 10 * Digits[High(Digits)];
{Does it divide evenly into N}
Result:=(N mod I)=0;
end;
 
 
function HasEndDigit(N: int64; Digit: integer): boolean;
{Return true if last digit match specified "Digit"}
var LD: integer;
begin
LD:=N mod 10;
Result:=LD=Digit;
end;
 
 
function GetGapfulPalinEndSet(Max, EndDigit: integer): string;
{Get first Max number of Gapful Palindromes with specified EndDigit}
var I,Cnt,P: integer;
begin
Result:='Ending in: '+IntToStr(EndDigit)+CRLF;
Cnt:=0;
{Get palindromes and test them}
for I:=0 to high(Integer) do
begin
{Get next palinedrome}
P:=GetNthPalindrome(I);
{Is Gapful and has specified EndDigit}
if IsGapFul(P) and HasEndDigit(P,EndDigit) then
begin
Inc(Cnt);
{Display it}
Result:=Result+Format('%8d',[P]);
if (Cnt mod 5)=0 then Result:=Result+CRLF;
{Break when finished}
if Cnt>=Max then break;
end;
end;
end;
 
 
 
function LastGapfulPalinEndSet(Count,Last,EndDigit: integer): string;
{Get Gapful Palindromes with specified EndDigit}
{Get "Last" number of items out of a total "Count" }
var I,Inx: integer;
var P: int64;
var IA: TInt64DynArray;
begin
SetLength(IA,Count);
Result:='Ending in: '+IntToStr(EndDigit)+CRLF;
{Get count number of items}
Inx:=0;
for I:=0 to Count-1 do
begin
{Keep getting palindromes until}
{they Gapful and have specified last digit}
repeat
begin
P:=GetNthPalindrome(Inx);
Inc(Inx);
end
until IsGapFul(P) and HasEndDigit(P,EndDigit);
{Save item}
IA[I]:=P;
end;
{Get last items}
for I:=Count-Last to Count-1 do
begin
Result:=Result+Format('%12d',[IA[I]]);
if (I mod 5)=4 then Result:=Result+CRLF;
end;
end;
 
 
 
procedure ShowPalindromicGapfuls(Memo: TMemo);
var S: string;
begin
Memo.Lines.Add('First 20 palindromic gapful numbers');
 
Memo.Lines.Add(GetGapFulPalinEndSet(20,1));
Memo.Lines.Add(GetGapFulPalinEndSet(20,2));
Memo.Lines.Add(GetGapFulPalinEndSet(20,3));
Memo.Lines.Add(GetGapFulPalinEndSet(20,4));
Memo.Lines.Add(GetGapFulPalinEndSet(20,5));
Memo.Lines.Add(GetGapFulPalinEndSet(20,6));
Memo.Lines.Add(GetGapFulPalinEndSet(20,7));
Memo.Lines.Add(GetGapFulPalinEndSet(20,8));
Memo.Lines.Add(GetGapFulPalinEndSet(20,9));
 
Memo.Lines.Add('86th to 100th');
Memo.Lines.Add(LastGapFulPalinEndSet(100,15,1));
Memo.Lines.Add(LastGapFulPalinEndSet(100,15,2));
Memo.Lines.Add(LastGapFulPalinEndSet(100,15,3));
Memo.Lines.Add(LastGapFulPalinEndSet(100,15,4));
Memo.Lines.Add(LastGapFulPalinEndSet(100,15,5));
Memo.Lines.Add(LastGapFulPalinEndSet(100,15,6));
Memo.Lines.Add(LastGapFulPalinEndSet(100,15,7));
Memo.Lines.Add(LastGapFulPalinEndSet(100,15,8));
Memo.Lines.Add(LastGapFulPalinEndSet(100,15,9));
 
Memo.Lines.Add('991st to 1000th:');
Memo.Lines.Add(LastGapFulPalinEndSet(1000,10,1));
Memo.Lines.Add(LastGapFulPalinEndSet(1000,10,2));
Memo.Lines.Add(LastGapFulPalinEndSet(1000,10,3));
Memo.Lines.Add(LastGapFulPalinEndSet(1000,10,4));
Memo.Lines.Add(LastGapFulPalinEndSet(1000,10,5));
Memo.Lines.Add(LastGapFulPalinEndSet(1000,10,6));
Memo.Lines.Add(LastGapFulPalinEndSet(1000,10,7));
Memo.Lines.Add(LastGapFulPalinEndSet(1000,10,8));
Memo.Lines.Add(LastGapFulPalinEndSet(1000,10,9));
end;
 
 
 
</syntaxhighlight>
{{out}}
<pre>
First 20 palindromic gapful numbers
Ending in: 1
121 1001 1111 1221 1331
1441 1551 1661 1771 1881
1991 10901 11011 12221 13431
14641 15851 17171 18381 19591
 
Ending in: 2
242 2002 2112 2222 2332
2442 2552 2662 2772 2882
2992 20702 21912 22022 23232
24442 25652 26862 28182 29392
 
Ending in: 3
363 3003 3333 3663 3993
31713 33033 36663 300003 303303
306603 309903 312213 315513 318813
321123 324423 327723 330033 333333
 
Ending in: 4
484 4004 4224 4444 4664
4884 40304 42724 44044 46464
48884 400004 401104 402204 403304
404404 405504 406604 407704 408804
 
Ending in: 5
5005 5115 5225 5335 5445
5555 5665 5775 5885 5995
50105 51315 52525 53735 54945
55055 56265 57475 58685 59895
 
Ending in: 6
6006 6336 6666 6996 61116
64746 66066 69696 600006 603306
606606 609906 612216 615516 618816
621126 624426 627726 630036 633336
 
Ending in: 7
7007 7777 77077 700007 707707
710017 717717 720027 727727 730037
737737 740047 747747 750057 757757
760067 767767 770077 777777 780087
 
Ending in: 8
8008 8448 8888 80608 86768
88088 800008 802208 804408 806608
808808 821128 823328 825528 827728
829928 840048 842248 844448 846648
 
Ending in: 9
9009 9999 94149 99099 900009
909909 918819 927729 936639 945549
954459 963369 972279 981189 990099
999999 9459549 9508059 9557559 9606069
 
86th to 100th
Ending in: 1
165561 166661 167761 168861 169961
170071 171171 172271 173371 174471
175571 176671 177771 178871 179971
 
Ending in: 2
265562 266662 267762 268862 269962
270072 271172 272272 273372 274472
275572 276672 277772 278872 279972
 
Ending in: 3
30366303 30399303 30422403 30455403 30488403
30511503 30544503 30577503 30600603 30633603
30666603 30699603 30722703 30755703 30788703
 
Ending in: 4
4473744 4485844 4497944 4607064 4619164
4620264 4632364 4644464 4656564 4668664
4681864 4693964 4803084 4815184 4827284
 
Ending in: 5
565565 566665 567765 568865 569965
570075 571175 572275 573375 574475
575575 576675 577775 578875 579975
 
Ending in: 6
60399306 60422406 60455406 60488406 60511506
60544506 60577506 60600606 60633606 60666606
60699606 60722706 60755706 60788706 60811806
 
Ending in: 7
72299227 72322327 72399327 72422427 72499427
72522527 72599527 72622627 72699627 72722727
72799727 72822827 72899827 72922927 72999927
 
Ending in: 8
80611608 80622608 80633608 80644608 80655608
80666608 80677608 80688608 80699608 80800808
80811808 80822808 80833808 80844808 80855808
 
Ending in: 9
95311359 95400459 95499459 95588559 95677659
95766759 95855859 95944959 96033069 96122169
96211269 96300369 96399369 96488469 96577569
 
991st to 1000th:
Ending in: 1
17799771 17800871 17811871 17822871 17833871
17844871 17855871 17866871 17877871 17888871
 
Ending in: 2
27799772 27800872 27811872 27822872 27833872
27844872 27855872 27866872 27877872 27888872
 
Ending in: 3
3084004803 3084334803 3084664803 3084994803 3085225803
3085555803 3085885803 3086116803 3086446803 3086776803
 
Ending in: 4
482282284 482414284 482535284 482656284 482777284
482898284 482909284 483020384 483141384 483262384
 
Ending in: 5
57800875 57811875 57822875 57833875 57844875
57855875 57866875 57877875 57888875 57899875
 
Ending in: 6
6084004806 6084334806 6084664806 6084994806 6085225806
6085555806 6085885806 6086116806 6086446806 6086776806
 
Ending in: 7
7452992547 7453223547 7453993547 7454224547 7454994547
7455225547 7455995547 7456226547 7456996547 7457227547
 
Ending in: 8
8085995808 8086006808 8086116808 8086226808 8086336808
8086446808 8086556808 8086666808 8086776808 8086886808
 
Ending in: 9
9675005769 9675995769 9676886769 9677777769 9678668769
9679559769 9680440869 9681331869 9682222869 9683113869
 
Elapsed Time: 763.788 ms.
 
</pre>
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// Palindromic Gapful Numbers . Nigel Galloway: December 3rd., 2020
let rec fN g l=seq{match l with 3->yield! seq{for n in 0L..9L->g*100L+g+n*10L}
Line 1,432 ⟶ 1,817:
[1L..9L]|>Seq.iter(fun n->rcGf n|>Seq.skip 85|>Seq.take 15|>Seq.iter(printf "%d ");printfn "");printfn "#####"
[1L..9L]|>Seq.iter(fun n->rcGf n|>Seq.skip 990|>Seq.take 10|>Seq.iter(printf "%d ");printfn "");printfn "#####"
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,467 ⟶ 1,852:
</pre>
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: formatting fry io kernel lists lists.lazy locals math
math.functions math.ranges math.text.utils prettyprint sequences ;
IN: rosetta-code.palindromic-gapful-numbers
Line 1,519 ⟶ 1,904:
15 100 ! part 2
10 1000 ! part 3 (Optional)
[ show-palindromic-gapfuls ] 2tri@</langsyntaxhighlight>
{{out}}
<pre style="height:45ex">
Line 1,558 ⟶ 1,943:
=={{header|FreeBASIC}}==
Time-consuming brute-force solution with only a few speedups...
<langsyntaxhighlight lang="freebasic">function is_gapful( n as uinteger ) as boolean
if n<100 then return false
dim as string ns = str(n)
Line 1,616 ⟶ 2,001:
print_range(yays(), 1, 20)
print_range(yays(), 86, 100)
print_range(yays(), 991, 1000)</langsyntaxhighlight>
{{out}}
<pre>
Line 1,667 ⟶ 2,052:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Palindromic_gapful_numbers}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
In '''[https://formulae.org/?example=Palindromic_gapful_numbers this]''' page you can see the program(s) related to this task and their results.
 
=={{header|Go}}==
Line 1,677 ⟶ 2,058:
 
To keep the Pascal entry company, I've extended the search to the first 10 million such numbers for each of the nine sets.
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,780 ⟶ 2,161:
fmt.Println()
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,864 ⟶ 2,245:
=={{header|Haskell}}==
===Brute Force===
<langsyntaxhighlight lang="haskell">import Control.Monad (guard)
 
palindromic :: Int -> Bool
Line 1,899 ⟶ 2,280:
putStrLn "\nLast 10 of first 1000 palindromic gapful numbers ending in:"
showSets (show . drop 990 . take 1000 . result)
putStrLn "\ndone."</langsyntaxhighlight>
 
===Optimized===
Here the approach is to generate a series of palindromes.
<langsyntaxhighlight lang="haskell">import Data.List (sort, unfoldr)
import Control.Monad (guard)
 
Line 1,937 ⟶ 2,318:
, ("Last 15 of first 100", drop 85 . take 100)
, ("Last 10 of first 1000", drop 990 . take 1000)
]</langsyntaxhighlight>
{{out}}
<pre>
Line 1,995 ⟶ 2,376:
</pre>
Part 2:
<syntaxhighlight lang="text">
palindromify=: [: , ((,~ |.@}.) ; (,~ |.))&>
gapful=: (0 = (|~ ({.,{:)&.(10&#.inv)))&>
Line 2,001 ⟶ 2,382:
task2_palindromes=: [: 10&#.&> [: palindromify task2_cartesian_products
task2_gapfuls=: [: /:~ [: ; [: (#~ gapful)@task2_palindromes&.> >:@i.
</syntaxhighlight>
</lang>
 
<pre>
Line 2,067 ⟶ 2,448:
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">
import java.util.ArrayList;
import java.util.HashMap;
Line 2,177 ⟶ 2,558:
 
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,216 ⟶ 2,597:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">import Base.iterate, Base.IteratorSize, Base.IteratorEltype
 
struct Palindrome x1::UInt8; x2::UInt8; outer::UInt8; end
Line 2,260 ⟶ 2,641:
 
testpal()
</langsyntaxhighlight>{{out}}
<pre style="height:100ex;overflow:scroll">
Last digit | Last 20 of 20 palindromic gapful numbers from 100
Line 2,355 ⟶ 2,736:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[GapfulQ, GetFirstPalindromicGapfulNumbers]
GapfulQ[n_Integer] := Divisible[n, FromDigits[IntegerDigits[n][[{1, -1}]]]]
GetFirstPalindromicGapfulNumbers[startend_, n_Integer] :=
Line 2,385 ⟶ 2,766:
Print[GetFirstPalindromicGapfulNumbers[#, 100][[86 ;; 100]]] & /@ Range[9];
Print["991st to 1000th:"]
Print[GetFirstPalindromicGapfulNumbers[#, 1000][[991 ;; 1000]]] & /@ Range[9];</langsyntaxhighlight>
{{out}}
<pre>First 20 palindromic gapful numbers >100 ending with each digit from 1 to 9:
Line 2,422 ⟶ 2,803:
Forms palindromes using number<->string conversions.
 
<langsyntaxhighlight lang="ruby">import strutils # for number input
import times # for timing code execution
import unicode # for reversed
Line 2,476 ⟶ 2,857:
for digit in 1..9: echo(digit, " : ", palindromicgapfuls(digit, count, keep) )
 
echo (epochTime() - start)</langsyntaxhighlight>
 
System: I7-6700HQ, 3.5GHz, Linux Kernel 5.9.10, GCC 10.2.0, Nim 1.4.0
Line 2,485 ⟶ 2,866:
Faster version performing number<->string conversions for palindromes.
 
<langsyntaxhighlight lang="ruby">import strutils # for number input
import times # for timing code execution
import unicode # for reversed
Line 2,537 ⟶ 2,918:
for digit in 1..9: echo(digit, " : ", palindromicgapfuls(digit, count, keep) )
 
echo (epochTime() - start)</langsyntaxhighlight>
 
System: I7-6700HQ, 3.5GHz, Linux Kernel 5.9.10, GCC 10.2.0, Nim 1.4.0
Line 2,546 ⟶ 2,927:
Fastest: make palindromes directly numerically.
 
<langsyntaxhighlight lang="ruby">import times
 
proc make_palindrome(front_half: uint64, power: int): uint64 =
Line 2,601 ⟶ 2,982:
for digit in 1..9: echo(digit, " : ", palindromicgapfuls(digit, count, keep) )
 
echo (epochTime() - start)</langsyntaxhighlight>
 
System: I7-6700HQ, 3.5GHz, Linux Kernel 5.9.14, GCC 10.2.0, Nim 1.4.2
Line 2,684 ⟶ 3,065:
9 : 968787783387787869</pre>
 
<langsyntaxhighlight lang="pascal">program PalinGap;
{$IFDEF FPC}
{$MODE DELPHI}{$OPTIMIZATION ON,ALL}{$CODEALIGN proc=16}{$ALIGN 16}
Line 2,856 ⟶ 3,237:
OutPalinGap(10000000,10000000,dgt);
writeln;
end.</langsyntaxhighlight>{{out}}<pre style="height:35ex;overflow:scroll;font-size:87%">
palindromic gapful numbers from 1 to 20
1 : 121 1001 1111 1221 1331 1441 1551 1661 1771 1881 1991 10901 11011 12221 13431 14641 15851 17171 18381 19591
Line 2,928 ⟶ 3,309:
=={{header|Perl}}==
Minor (and inefficient) tweak on the [[Gapful numbers]] task.
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 2,950 ⟶ 3,331:
}
say ''
}</langsyntaxhighlight>
{{out}}
<pre style="height:20ex">Palindromic gapful numbers starting at 1:
Line 2,977 ⟶ 3,358:
{{trans|Go}}
Translation of Go, but trimmed back to bare minimum: you should not expect this to fare particularly well at the 10_000_000-level against the likes of Go/Pascal, though it should fare reasonably well against lesser beings... The version below beats 'em all, though, for now.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">reverse_n</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
Line 3,036 ⟶ 3,417:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre style="font-size: 11px">
Line 3,074 ⟶ 3,455:
 
=== Ludicrously fast to 10,000,000,000,000,000,000th ===
{{libheader|Phix/online}}
Astonishingly this is all done with standard precision numbers, &lt; 2<sup><small>53</small></sup>. You realise this is like ten thousand times the ''square'' of the previous limits, and still far faster.<br>
You can run this online [http://phix.x10.mx/p2js/pgn.htm here].
I will credit [[Self_numbers#AppleScript]] and the comment by Nigel Galloway on the talk page for ideas that inspired me.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">-- demo/rosetta/Palindromic_gapful_numbers.exw
-- demo\rosetta\Palindromic_gapful_numbers.exw
-- ===========================================
--
-- Astonishingly this is all done with standard precision numbers, &lt;2^53.
-- I will credit https://rosettacode.org/wiki/[[Self_numbers#AppleScript]] and comment by Nigel Galloway
-- the comment by Nigel Galloway on the talk page for ideas that inspired me.
--
-- A palindrome such as 9459549 can be constructed/broken down into
Line 3,133 ⟶ 3,516:
-- will probably be skipped.
--</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">cache</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">columnize</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">columnize</span><span style="color: #0000FF;">({</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">9</span><span style="color: #0000FF;">)}),</span><span style="color: #000000;">9</span><span style="color: #0000FF;">))</span>
<span style="color: #000080;font-style:italic;">-- ie <nowiki>{{</nowiki>{1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1<nowiki>}}</nowiki>,
-- <nowiki>{{</nowiki>2}, {2}, {2}, {2}, {2}, {2}, {2}, {2}, {2<nowiki>}}</nowiki>,
-- <nowiki>{{</nowiki>3}, {3}, {3}, {3}, {3}, {3}, {3}, {3}, {3<nowiki>}}</nowiki>,
-- <nowiki>{{</nowiki>4}, {4}, {4}, {4}, {4}, {4}, {4}, {4}, {4<nowiki>}}</nowiki>,
-- <nowiki>{{</nowiki>5}, {5}, {5}, {5}, {5}, {5}, {5}, {5}, {5<nowiki>}}</nowiki>,
-- <nowiki>{{</nowiki>6}, {6}, {6}, {6}, {6}, {6}, {6}, {6}, {6<nowiki>}}</nowiki>,
-- <nowiki>{{</nowiki>7}, {7}, {7}, {7}, {7}, {7}, {7}, {7}, {7<nowiki>}}</nowiki>,
-- <nowiki>{{</nowiki>8}, {8}, {8}, {8}, {8}, {8}, {8}, {8}, {8<nowiki>}}</nowiki>,
-- <nowiki>{{</nowiki>9}, {9}, {9}, {9}, {9}, {9}, {9}, {9}, {9<nowiki>}}</nowiki>}
-- aka 1 rem 11 .. 1 rem 99 are all 1,
-- .. 9 rem 11 .. 9 rem 99 are all 9.
Line 3,153 ⟶ 3,537:
-- it will be at most (on 64-bit) 9 * 9 * 42.)
--</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">rmdrrmdrn</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">digit</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">gap</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">pow</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">--
-- digit is the outer 0..9 (obvs 0 always yields 0),
Line 3,159 ⟶ 3,543:
-- pow is trailing zeros (0,1,2,.. for eg 101,1010,10100),
-- n is 1..9 for 11..99
-- eg rmdrrmdrn(4,3,2,1) yields remainder(4000400,11), but
-- that === remainder(remainder(4000000,11)+
-- remainder( 400,11),11), and
-- if k = remainder(4*10^(m-1),11) [already known] then
-- remainder(4*10^m,11) === remainder(k*10,11), so
Line 3,169 ⟶ 3,553:
<span style="color: #008080;">if</span> <span style="color: #000000;">digit</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">nn</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">*</span><span style="color: #000000;">11</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">g</span>
<span style="color: #008080004080;">whilesequence</span> <span style="color: #7060A8000000;">lengthcdn</span> <span style="color: #0000FF;">(=</span> <span style="color: #000000;">cache</span><span style="color: #0000FF;">[</span><span style="color: #000000;">digit</span><span style="color: #0000FF;">][</span><span style="color: #000000;">n</span><span style="color: #0000FF;">])<</span><span style="color: #000000;">gap</span><span style="color: #0000FF;">+</span><span style="color: #000000;">pow</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span> <span style="color: #008080;">do</span>
<span style="color: #000000008080;">cache</span><span style="color: #0000FF;">[</span><span style="color: #000000;">digit</span><span style="color: #0000FF;">][</span><span style="color: #000000;">n</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">&=while</span> <span style="color: #7060A8;">remainderlength</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cachecdn</span><span style="color: #0000FF;">[)<</span><span style="color: #000000;">digitgap</span><span style="color: #0000FF;">][+</span><span style="color: #000000;">npow</span><span style="color: #0000FF;">][$]*+</span><span style="color: #000000;">102</span><span style="color: #0000FF;">,</span><span style="color: #000000008080;">nn</span><span style="color: #0000FF;">)do</span>
<span style="color: #000000;">cache</span><span style="color: #0000FF;">[</span><span style="color: #000000;">digit</span><span style="color: #0000FF;">][</span><span style="color: #000000;">n</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span> <span style="color: #000080;font-style:italic;">-- (kill refcount)</span>
<span style="color: #000000;">cdn</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cdn</span><span style="color: #0000FF;">[$]*</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">nn</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">cache</span><span style="color: #0000FF;">[</span><span style="color: #000000;">digit</span><span style="color: #0000FF;">][</span><span style="color: #000000;">n</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">cdn</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #000000;">g</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">gap</span><span style="color: #0000FF;">=-</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">?</span> <span style="color: #000000;">0</span> <span style="color: #0000FF;">:</span> <span style="color: #000000;">cachecdn</span><span style="color: #0000FF;">[</span><span style="color: #000000;">digit</span><span style="color: #0000FF;">][</span><span style="color: #000000;">n</span><span style="color: #0000FF;">][</span><span style="color: #000000;">gap</span><span style="color: #0000FF;">+</span><span style="color: #000000;">pow</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">g</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">cachecdn</span><span style="color: #0000FF;">[</span><span style="color: #000000;">digit</span><span style="color: #0000FF;">][</span><span style="color: #000000;">n</span><span style="color: #0000FF;">][</span><span style="color: #000000;">pow</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span><span style="color: #000000;">nn</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
Line 3,184 ⟶ 3,571:
-- palcount, to_skip, count: self explanatory (aka got/ignore/target)
-- l: length of inner to be filled in
-- r: remainder of outer, eg remainder(9400049,11), built from rmdrrmdrn()
-- p: left shift (should in fact always equal length(lhs), I think)
-- dd: outermost 1..9 (despite the name, it's a single digit)
Line 3,196 ⟶ 3,583:
<span style="color: #000000;">skip</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">9</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">r2</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">+</span><span style="color: #000000;">rmdrrmdrn</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dd</span><span style="color: #0000FF;">),</span><span style="color: #000000;">dd</span><span style="color: #0000FF;">*</span><span style="color: #000000;">11</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">l</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">2</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">r2</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
Line 3,203 ⟶ 3,590:
<span style="color: #000000;">skip</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">else</span>
<span style="color: #000000;">pals</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pals</span><span style="color: #0000FF;">),</span><span style="color: #000000;">lhs</span><span style="color: #0000FF;">&</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d</span><span style="color: #0000FF;">+</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">)&</span><span style="color: #7060A8;">reverse</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lhs</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">else</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">pals</span><span style="color: #0000FF;">,</span><span style="color: #000000;">palcount</span><span style="color: #0000FF;">,</span><span style="color: #000000;">skipn</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">palindromicgapfuls</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pals</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">lhs</span><span style="color: #0000FF;">&(</span><span style="color: #000000;">d</span><span style="color: #0000FF;">+</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">palcount</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">to_skip</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">count</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">r2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">dd</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">skip</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">skipn</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
Line 3,223 ⟶ 3,610:
<span style="color: #004080;">string</span> <span style="color: #000000;">lhs</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span><span style="color: #0000FF;">&(</span><span style="color: #000000;">digit</span><span style="color: #0000FF;">+</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- ie "1" or "2" .. or "9"</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">palcount</span> <span style="color: #0000FF;"><</span> <span style="color: #000000;">count</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rmdrrmdrn</span><span style="color: #0000FF;">(</span><span style="color: #000000;">digit</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">digit</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">pals</span><span style="color: #0000FF;">,</span><span style="color: #000000;">palcount</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">palindromicgapfuls</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pals</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lhs</span><span style="color: #0000FF;">,</span><span style="color: #000000;">palcount</span><span style="color: #0000FF;">,</span><span style="color: #000000;">to_skip</span><span style="color: #0000FF;">,</span><span style="color: #000000;">count</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">digit</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">l</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
Line 3,257 ⟶ 3,644:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<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;">"Completed in %s\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">))</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre style="font-size: 11px10px">
First 20 palindromic gapful numbers ending with:
1: 121 1001 1111 1221 1331 1441 1551 1661 1771 1881 1991 10901 11011 12221 13431 14641 15851 17171 18381 19591
Line 3,371 ⟶ 3,758:
spare time you have, leave this running and it'll look better, uncomment the reset of
count, to something that'll actually finish, and you'll start to believe. :-)
<langsyntaxhighlight Phixlang="phix">--count = 100000
while count do
count -= 1
?collect(9,count,1)
end while</langsyntaxhighlight>
 
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<langsyntaxhighlight lang="prolog">init_palindrome(Digit, p(10, Next, 0)):-
Next is Digit * 10 - 1.
 
Line 3,485 ⟶ 3,872:
print_numbers(1, 20, Numbers),
print_numbers(86, 100, Numbers),
print_numbers(991, 1000, Numbers).</langsyntaxhighlight>
 
{{out}}
Line 3,535 ⟶ 3,922:
Note: Although this uses the idea of generating palindromes from the Geeks4geeks reference mentioned in the Factor entry, none of their code was used.
 
<langsyntaxhighlight lang="python">from itertools import count
from pprint import pformat
import re
Line 3,567 ⟶ 3,954:
b = {k:v[-last:] for k, v in bin.items()}
txt = pformat(b, width=220)
print('', re.sub(r"[{},\[\]]", '', txt))</langsyntaxhighlight>
 
{{out}}
Line 3,605 ⟶ 3,992:
 
===Functional===
<langsyntaxhighlight lang="python">'''Palindromic gapful numbers'''
 
from itertools import chain, count, islice, tee
Line 3,806 ⟶ 4,193:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>First 20 samples of gapful palindromes (> 100) by last digit:
Line 3,845 ⟶ 4,232:
{{works with|Rakudo|2019.07.1}}
 
<syntaxhighlight lang="raku" perl6line>constant @digits = '0','1','2','3','4','5','6','7','8','9';
 
# Infinite lazy iterator to generate palindromic "gap" numbers
Line 3,857 ⟶ 4,244:
my @gappal = (1..9).map: -> \digit {
my \divisor = digit + 10 * digit;
@npal.hyper.map: -> \this { next unless (my \test = digit ~ this ~ digit) %% divisor; test }
}
 
Line 3,866 ⟶ 4,253:
,"\n(Extra stretchy) 9,995th through 10,000th:", 9994..9999, 12
,"\n(Meh) 100,000th:", 99999, 14
).hyper(:1batch).map: -> $caption, $range, $fmt {
my $now = now;
say $caption;
put "$_: " ~ @gappal[$_-1][|$range].fmt("%{$fmt}s") for 1..9;
say round( now - $now, .001 ), " seconds";
}</langsyntaxhighlight>
{{out}}
<pre style="font-size:80%;">(Required) First 20 gapful palindromes:
Line 3,934 ⟶ 4,321:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program computes and displays palindromic gapful numbers, it also can show those */
/*─────────────────────── palindromic gapful numbers listed by their last decimal digit.*/
numeric digits 20 /*ensure enough decimal digits gapfuls.*/
Line 3,964 ⟶ 4,351:
do k=1 for 9; say k':' strip( subword($.k, 1 + n - z) )
end /*k*/; say
return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the internal default inputs:}}
 
Line 4,005 ⟶ 4,392:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 4,045 ⟶ 4,432:
 
see "done..." + nl
</syntaxhighlight>
</lang>
Output:
<pre>
Line 4,063 ⟶ 4,450:
{{trans|Crystal}}
===Brute force and slow===
<langsyntaxhighlight lang="ruby">def palindromesgapful(digit, pow)
r1 = digit * (10**pow + 1)
r2 = 10**pow * (digit + 1)
Line 4,090 ⟶ 4,477:
count = 1000
puts "\nLast 10 of first 1000 palindromic gapful numbers ending in:"
(1..9).each { |digit| print "#{digit} : #{digitscount(digit, count).last(10)}\n" }</langsyntaxhighlight>
 
===Orders of Magnitude Faster: Direct Generation of Numbers===
Line 4,098 ⟶ 4,485:
Run as: $ ruby palindromicgapfuls.rb
Optimized version, the ultimate fastest: Ruby 2.7.1 - 112.5 secs
<langsyntaxhighlight lang="ruby">def palindromicgapfuls(digit, count)
gapfuls = [] # array of palindromic gapfuls
nn = digit * 11 # digit gapful divisor: 11, 22,...88, 99
Line 4,154 ⟶ 4,541:
1.upto(9) { |digit| puts "#{digit} : #{palindromicgapfuls(digit, count).last(keep)}" }
 
puts (Time.now - start)</langsyntaxhighlight>
 
Compact version: Ruby-2.7.1 - 113.0 secs
<langsyntaxhighlight lang="ruby">def palindromicgapfuls(digit, count)
gapfuls = [] # array of palindromic gapfuls
nn = digit * 11 # digit gapful divisor: 11, 22,...88, 99
Line 4,206 ⟶ 4,593:
1.upto(9) { |digit| puts "#{digit} : #{palindromicgapfuls(digit, count).last(keep)}" }
 
puts (Time.now - start)</langsyntaxhighlight>
 
Object Oriented implementation: Ruby 2.7.1 - 113.0 secs
<langsyntaxhighlight lang="ruby">class PalindromicGapfuls
include Enumerable
 
Line 4,271 ⟶ 4,658:
1.upto(9) { |digit| puts "#{digit} : #{PalindromicGapfuls.new(digit).first(count).last(keep)}" }
 
puts (Time.now - start)</langsyntaxhighlight>
 
Versions optimized for minimal memory use: Ruby 2.7.1 - 110.0 secs
<langsyntaxhighlight lang="ruby">def palindromicgapfuls(digit, count, keep)
palcnt = 0 # count of gapful palindromes
to_skip = count - keep # count of unwanted values to skip
Line 4,332 ⟶ 4,719:
1.upto(9) { |digit| puts "#{digit} : #{palindromicgapfuls(digit, count, keep)}" }
 
puts (Time.now - start)</langsyntaxhighlight>
 
Compact version optimized for minimal memory use: Ruby 2.7.1 - 111.5 secs
<langsyntaxhighlight lang="ruby">def palindromicgapfuls(digit, count, keep)
palcnt = 0 # count of gapful palindromes
to_skip = count - keep # count of unwanted values to skip
Line 4,386 ⟶ 4,773:
1.upto(9) { |digit| puts "#{digit} : #{palindromicgapfuls(digit, count, keep)}" }
 
puts (Time.now - start)</langsyntaxhighlight>
 
OOP version optimized for minimal memory use: Ruby 2.7.1 - 116.0 secs
<BR>It creates an output method that skips the unwanted values and only keeps/stores the desired ones.
<langsyntaxhighlight lang="ruby">class PalindromicGapfuls
include Enumerable
 
Line 4,462 ⟶ 4,849:
1.upto(9) { |digit| puts "#{digit} : #{PalindromicGapfuls.new(digit).keep_from(count, keep)}" }
 
puts (Time.now - start)</langsyntaxhighlight>
{{out}}
<pre>First 20 palindromic gapful numbers 100 ending with:
Line 4,533 ⟶ 4,920:
{{trans|F#}}
0.186s on Intel(R) Core(TM) i5-1035G1 CPU @ 1.00GHz
<langsyntaxhighlight lang="ruby">
class PalNo
def initialize(set)
Line 4,551 ⟶ 4,938:
for n in 1..9 do palNo=PalNo.new(n); g=1; palNo.each{|n| print "#{n} " if g>85; g+=1; break unless g<101}; puts "" end; puts "####"
for n in 1..9 do palNo=PalNo.new(n); g=1; palNo.each{|n| print "#{n} " if g>990; g+=1; break unless g<1001}; puts "" end; puts "####"
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 4,591 ⟶ 4,978:
Run as: $ ruby fsharp2ruby.rb
Best Time: 437.566515299 secs
<langsyntaxhighlight lang="ruby">class PalNo
def initialize(digit)
@digit, @l, @dd = digit, 3, 11*digit
Line 4,619 ⟶ 5,006:
(1..9).each { |digit| PalNo.new(digit).show(10_000_000, 1) }; puts "####"
 
puts (Time.now - start)</langsyntaxhighlight>
{{out}}
<pre>
Line 4,690 ⟶ 5,077:
This version uses number->string then string->number conversions to create palindromes.
 
<langsyntaxhighlight lang="rust">fn palindromicgapfuls(digit: u64, count: u64, keep: usize) -> Vec<u64> {
let mut palcnt = 0u64; // count of gapful palindromes
let to_skip = count - keep as u64; // count of unwanted values to skip
Line 4,744 ⟶ 5,131:
println!("{:?}", t.elapsed())
}</langsyntaxhighlight>
System: I7-6700HQ, 3.5 GHz, Linux Kernel 5.9.10, Rust 1.48
Compil: $ rustc -C opt-level=3 -C target-cpu=native -C codegen-units=1 -C lto palindromicgapfuls.rs
Line 4,754 ⟶ 5,141:
About 2.5x faster.
 
<langsyntaxhighlight lang="rust">fn palindromicgapfuls(digit: u64, count: u64, keep: usize) -> Vec<u64> {
let mut palcnt = 0u64; // count of gapful palindromes
let to_skip = count - keep as u64; // count of unwanted values to skip
Line 4,814 ⟶ 5,201:
println!("{:?}", t.elapsed())
}</langsyntaxhighlight>
System: I7-6700HQ, 3.5 GHz, Linux Kernel 5.9.10, Rust 1.48
Compil: $ rustc -C opt-level=3 -C target-cpu=native -C codegen-units=1 -C lto palindromicgapfuls.rs
Line 4,888 ⟶ 5,275:
=={{header|Sidef}}==
Inspired from the C++ and Raku entries.
<langsyntaxhighlight lang="ruby">class PalindromeGenerator (digit, base=10) {
 
has power = base
Line 4,922 ⟶ 5,309:
say ("#{d}: ", arr.map{ "%*s" % (w, _) }.join(' '))
}
})</langsyntaxhighlight>
{{out}}
<pre style="font-size:83%">
Line 4,974 ⟶ 5,361:
{{libheader|Wren-fmt}}
Search limited to the first 100,000 palindromic gapful numbers as, beyond that, the numbers become too large (>= 2 ^ 53) to be accurately represented by Wren's Num type.
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Conv, Fmt
 
var reverse = Fn.new { |s|
Line 5,033 ⟶ 5,420:
}
System.print()
}</langsyntaxhighlight>
 
{{out}}
Line 5,091 ⟶ 5,478:
8: 80869644696808 80869655696808 80869666696808 80869677696808 80869688696808
9: 96877711777869 96877800877869 96877899877869 96877988977869 96878077087869
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang "XPL0">func Palindromic(N); \Return 'true' if N is palindromic
int N, I, J, S(10);
[I:= 0;
while N > 0 do
[N:= N/10;
S(I):= rem(0);
I:= I+1;
];
J:= 0; I:= I-1;
while J < I do
[if S(J) # S(I) then return false;
J:= J+1; I:= I-1;
];
return true;
];
 
int Lo, Hi, Task, Mul, N, Cnt, Prod;
[Lo:= 0; Hi:= 20;
for Task:= 1 to 2 do
[Mul:= 11;
repeat N:= 1; Cnt:= 0;
loop [Prod:= N * Mul;
if Prod >= 100 then
if rem(Prod/10) = Mul/10 then
if Palindromic(Prod) then
[Cnt:= Cnt+1;
if Cnt >= Lo then
[IntOut(0, Prod); ChOut(0, ^ )];
if Cnt >= Hi then quit;
];
N:= N+1;
];
CrLf(0);
Mul:= Mul + 11;
until Mul > 99;
CrLf(0);
Lo:= 86; Hi:= 100;
];
]</syntaxhighlight>
{{out}}
<pre>
121 1001 1111 1221 1331 1441 1551 1661 1771 1881 1991 10901 11011 12221 13431 14641 15851 17171 18381 19591
242 2002 2112 2222 2332 2442 2552 2662 2772 2882 2992 20702 21912 22022 23232 24442 25652 26862 28182 29392
363 3003 3333 3663 3993 31713 33033 36663 300003 303303 306603 309903 312213 315513 318813 321123 324423 327723 330033 333333
484 4004 4224 4444 4664 4884 40304 42724 44044 46464 48884 400004 401104 402204 403304 404404 405504 406604 407704 408804
5005 5115 5225 5335 5445 5555 5665 5775 5885 5995 50105 51315 52525 53735 54945 55055 56265 57475 58685 59895
6006 6336 6666 6996 61116 64746 66066 69696 600006 603306 606606 609906 612216 615516 618816 621126 624426 627726 630036 633336
7007 7777 77077 700007 707707 710017 717717 720027 727727 730037 737737 740047 747747 750057 757757 760067 767767 770077 777777 780087
8008 8448 8888 80608 86768 88088 800008 802208 804408 806608 808808 821128 823328 825528 827728 829928 840048 842248 844448 846648
9009 9999 94149 99099 900009 909909 918819 927729 936639 945549 954459 963369 972279 981189 990099 999999 9459549 9508059 9557559 9606069
 
165561 166661 167761 168861 169961 170071 171171 172271 173371 174471 175571 176671 177771 178871 179971
265562 266662 267762 268862 269962 270072 271172 272272 273372 274472 275572 276672 277772 278872 279972
30366303 30399303 30422403 30455403 30488403 30511503 30544503 30577503 30600603 30633603 30666603 30699603 30722703 30755703 30788703
4473744 4485844 4497944 4607064 4619164 4620264 4632364 4644464 4656564 4668664 4681864 4693964 4803084 4815184 4827284
565565 566665 567765 568865 569965 570075 571175 572275 573375 574475 575575 576675 577775 578875 579975
60399306 60422406 60455406 60488406 60511506 60544506 60577506 60600606 60633606 60666606 60699606 60722706 60755706 60788706 60811806
72299227 72322327 72399327 72422427 72499427 72522527 72599527 72622627 72699627 72722727 72799727 72822827 72899827 72922927 72999927
80611608 80622608 80633608 80644608 80655608 80666608 80677608 80688608 80699608 80800808 80811808 80822808 80833808 80844808 80855808
95311359 95400459 95499459 95588559 95677659 95766759 95855859 95944959 96033069 96122169 96211269 96300369 96399369 96488469 96577569
 
</pre>
 
=={{header|zkl}}==
Using ideas from the Factor entry
<langsyntaxhighlight lang="zkl">// 10,True --> 101,111,121,131,141,151,161,171,181,191,202, ..
// 10,False --> 1001,1111,1221,1331,1441,1551,1661,1771,1881,..
fcn createPalindromeW(start,oddLength){ //--> iterator
Line 5,112 ⟶ 5,563:
if(p%10==endsWith and (p%div)==0) p else Void.Skip
})
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">println("First 20 palindromic gapful numbers:");
[1..9].apply(palindromicGapfulW).apply("walk",20) : pgpp(_);
 
Line 5,124 ⟶ 5,575:
m,fmt := (0).max(table.apply((0).max)).numDigits, "%%%dd ".fmt(m).fmt;
foreach d,row in ([1..].zip(table)){ println(d,": ",row.pump(String,fmt)) }
}</langsyntaxhighlight>
{{out}}
<pre style="font-size:83%">
Line 5,160 ⟶ 5,611:
9: 9675005769 9675995769 9676886769 9677777769 9678668769 9679559769 9680440869 9681331869 9682222869 9683113869
</pre>
<langsyntaxhighlight lang="zkl">/* We can also thread the whole mess, which for this case, is a 3.75 speed up
* (3 min to 48sec) with 8 cores (Intel 4/4).
*/
Line 5,169 ⟶ 5,620:
.apply("noop") // wait for threads to finish
: pgpp(_);
}</langsyntaxhighlight>
{{out}}
<pre style="font-size:83%">
9,476

edits