Palindromic gapful numbers: Difference between revisions
m
→{{header|Wren}}: Minor tidy
(→{{header|AppleScript}}: 9 out of 10 palindromes now derived directly from incrementation of middle digit(s). 3.4 times as fast as before.) |
m (→{{header|Wren}}: Minor tidy) |
||
(32 intermediate revisions by 10 users not shown) | |||
Line 57:
=={{header|AppleScript}}==
<
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()</
{{output}}
Line 176:
8085995808 8086006808 8086116808 8086226808 8086336808 8086446808 8086556808 8086666808 8086776808 8086886808
9675005769 9675995769 9676886769 9677777769 9678668769 9679559769 9680440869 9681331869 9682222869 9683113869"</pre>
===Translation of Phix "Ludicrously fast …"===
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.
<syntaxhighlight 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()
script theWorks
property outerX11 : missing value
property countLimit : missing value
property to_skip : missing value
property palcount : missing value
property skipd : {{}, {}, {}, {}, {}, {}, {}, {}, {}}
property skipdOuter : missing value
property pals : missing value
-- Work out the remainder from the division of the positive decimal integer value which is
-- one or two instances of digit 'digit' separated by 'gap' zeros and followed by 'shift' zeros
-- (which may not be realisable as an AppleScript number) by 'outerX11', a multiple of 11.
on rmdr(digit, gap, shift)
-- Remainders from the division of left-shifted decimals by multiples of 11 reliably repeat
-- every six places shifted > 2, so use a dividend with the equivalent digit shifts < 9.
set coefficient to 10 ^ ((shift - 3) mod 6 + 3)
if (gap > -1) then set coefficient to coefficient + 10 ^ ((gap + shift - 2) mod 6 + 3)
return (digit * coefficient mod outerX11) as integer
end rmdr
-- Recursively infer from remainder arithmetic any palindromic gapful numbers with
-- ((count lhs) * 2 + gap) digits whose outer digit is the first value in lhs.
-- Append text versions of any falling in the current keep range to the 'pals' list.
on palindromicGapfuls(lhs, gap, remainder)
-- lhs: eg {9, 4, 5} of a potential 945…549 result.
-- gap: length of inner to be filled in
-- remainder: remainder of outer, eg 9400049 mod 11, but derived from rmdr() results.
set shift to (count lhs) -- left shift of inner (same as its right shift).
-- This translation's 'skipd' is a four-deep AppleScript list structure indexed with the elements
-- of the original dictionary's keys: (skipd) -> outermost digit -> shift -> gap -> remainder (+ 1).
-- The outermost digit element doesn't change during a search based on it, so the script property
-- 'skipdOuter' has been preset to skipd's outer-th sublist in the set-up for the current search.
-- Populate it just enough here to ensure that the slot about to be checked for a 'skip' value exists.
repeat (shift - (count my skipdOuter)) times
set end of my skipdOuter to {}
end repeat
repeat (gap - (count item shift of my skipdOuter)) times
set end of item shift of my skipdOuter to {}
end repeat
repeat (remainder + 1 - (count item gap of item shift of my skipdOuter)) times
set end of item gap of item shift of my skipdOuter to missing value
end repeat
set skip to item (remainder + 1) of item gap of item shift of my skipdOuter
if ((skip is missing value) or (palcount + skip > to_skip)) then
set skip to 0
set nextGap to gap - 2
repeat with d from 0 to 9
set nextRem to (remainder + rmdr(d, nextGap, shift)) mod outerX11
if (gap > 2) then
set skip to skip + palindromicGapfuls(lhs & d, nextGap, nextRem)
else if (nextRem is 0) then
-- A palindrome of lhs's contents around gap ds would be … gapful.
set palcount to palcount + 1
if (palcount > to_skip) then
-- This one would be in the current keep range, so realise it as text and store it.
if (gap is 2) then set d to {d, d} -- Not d * 11 as d could be 0.
set end of my pals to (lhs & d & reverse of lhs) as text
else
set skip to skip + 1
end if
end if
if (palcount = countLimit) then exit repeat
end repeat
if (palcount < to_skip) then set item (remainder + 1) of item gap of item shift of my skipdOuter to skip
else
set palcount to palcount + skip
end if
return skip
end palindromicGapfuls
-- Set up a search for the last 'keep' of the first 'countLimit' PGNs > 100 whose outer digit is 'outer',
-- call the recursive process for each palindrome width, and eventually return the stored numeric texts.
on collect(outer, countLimit, keep)
-- Initialise script object properties for the current search.
set outerX11 to outer * 11
set my countLimit to countLimit
set to_skip to countLimit - keep
set palcount to 0
set skipdOuter to item outer of my skipd
set pals to {}
-- Also locals and TIDs.
set lhs to {outer}
set gap to 1 -- Number of digits between outer pair.
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "" -- For list-to-text coercions.
repeat until (palcount = countLimit)
set remainder to rmdr(outer, gap, 0)
palindromicGapfuls(lhs, gap, remainder)
set gap to gap + 1
end repeat
set AppleScript's text item delimiters to astid
return pals
end collect
end script
return theWorks
end getWorks
(* Test code *)
-- Return an integer as text with the appropriate English ordinal suffix
on ordinalise(n)
-- Adapted from Victor Yee (adapted from NG (adapted from Jason Bourque) & Paul Skinner)
set units to n mod 10
if ((units > 3) or ((n - units) mod 100 is 10) or (units < 1) or (units mod 1 > 0)) then return (n as text) & "th"
return (n as text) & item units of {"st", "nd", "rd"}
end ordinalise
on doTask()
set tests to {{20, 20, 1, 9}, {100, 15, 1, 9}, {1000, 10, 1, 9}, {10000, 5, 1, 9}, ¬
{100000, 1, 1, 9}, {1000000, 1, 1, 9}, {10000000, 1, 1, 9}, ¬
{100000000, 1, 9, 9}, {1.0E+9, 1, 9, 9}, {1.0E+10, 1, 9, 9}, ¬
{1.0E+11, 1, 9, 9}, {1.0E+12, 1, 9, 9}, ¬
{1.0E+13, 1, 9, 9}, {1.0E+14, 1, 9, 9}, ¬
{1.0E+15, 1, 2, 4}}
-- set tests to {{20, 20, 1, 9}, {100, 15, 1, 9}, {1000, 10, 1, 9}} -- The RC task.
set output to {}
set theWorks to getWorks()
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to " "
repeat with i from 1 to (count tests)
set {countLimit, keep, firstOuter, lastOuter} to item i of tests
if (countLimit = keep) then
set h to "First " & countLimit
else if (keep > 1) then
set h to "Last " & keep & (" of first " & countLimit)
else
set h to ordinalise(countLimit)
end if
if ((keep = 1) and (firstOuter = lastOuter)) then
set h to h & " palindromic gapful number > 100 ending with " & firstOuter & ":"
else if (firstOuter = lastOuter) then
set h to h & " palindromic gapful numbers > 100 ending with " & firstOuter & ":"
else
set h to h & " palindromic gapful numbers > 100 ending with digits from " & firstOuter & (" to " & lastOuter & ":")
end if
if (i > 1) then set end of output to ""
set end of output to h
repeat with outer from firstOuter to lastOuter
set end of output to theWorks's (collect(outer, countLimit, keep)) as text
end repeat
end repeat
set AppleScript's text item delimiters to linefeed
set output to output as text
set AppleScript's text item delimiters to astid
return output
end doTask
doTask()</syntaxhighlight>
{{output}}
<pre style="font-size:80%">First 20 palindromic gapful numbers > 100 ending with digits from 1 to 9:
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
Last 15 of first 100 palindromic gapful numbers > 100 ending with digits from 1 to 9:
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
Last 10 of first 1000 palindromic gapful numbers > 100 ending with digits from 1 to 9:
17799771 17800871 17811871 17822871 17833871 17844871 17855871 17866871 17877871 17888871
27799772 27800872 27811872 27822872 27833872 27844872 27855872 27866872 27877872 27888872
3084004803 3084334803 3084664803 3084994803 3085225803 3085555803 3085885803 3086116803 3086446803 3086776803
482282284 482414284 482535284 482656284 482777284 482898284 482909284 483020384 483141384 483262384
57800875 57811875 57822875 57833875 57844875 57855875 57866875 57877875 57888875 57899875
6084004806 6084334806 6084664806 6084994806 6085225806 6085555806 6085885806 6086116806 6086446806 6086776806
7452992547 7453223547 7453993547 7454224547 7454994547 7455225547 7455995547 7456226547 7456996547 7457227547
8085995808 8086006808 8086116808 8086226808 8086336808 8086446808 8086556808 8086666808 8086776808 8086886808
9675005769 9675995769 9676886769 9677777769 9678668769 9679559769 9680440869 9681331869 9682222869 9683113869
Last 5 of first 10000 palindromic gapful numbers > 100 ending with digits from 1 to 9:
1787557871 1787667871 1787777871 1787887871 1787997871
2787557872 2787667872 2787777872 2787887872 2787997872
308760067803 308763367803 308766667803 308769967803 308772277803
48327872384 48329192384 48330303384 48331513384 48332723384
5787557875 5787667875 5787777875 5787887875 5787997875
608763367806 608766667806 608769967806 608772277806 608775577806
746958859647 746961169647 746968869647 746971179647 746978879647
808691196808 808692296808 808693396808 808694496808 808695596808
968697796869 968706607869 968715517869 968724427869 968733337869
100000th palindromic gapful numbers > 100 ending with digits from 1 to 9:
178788887871
278788887872
30878611687803
4833326233384
578789987875
60878611687806
74826144162847
80869688696808
96878077087869
1000000th palindromic gapful numbers > 100 ending with digits from 1 to 9:
17878799787871
27878799787872
3087876666787803
483333272333384
57878799787875
6087876996787806
7487226666227847
8086969559696808
9687870990787869
10000000th palindromic gapful numbers > 100 ending with digits from 1 to 9:
1787878888787871
2787878888787872
308787855558787803
48333332623333384
5787878998787875
608787855558787806
748867523325768847
808696968869696808
968787783387787869
100000000th palindromic gapful number > 100 ending with 9:
96878786855868787869
1.0E+9th palindromic gapful number > 100 ending with 9:
9687878775995778787869
1.0E+10th palindromic gapful number > 100 ending with 9:
968787878661166878787869
1.0E+11th palindromic gapful number > 100 ending with 9:
96878787877355377878787869
1.0E+12th palindromic gapful number > 100 ending with 9:
9687878787863773687878787869
1.0E+13th palindromic gapful number > 100 ending with 9:
968787878787711117787878787869
1.0E+14th palindromic gapful number > 100 ending with 9:
96878787878786133168787878787869
1.0E+15th palindromic gapful numbers > 100 ending with digits from 2 to 4:
27878787878787888878787878787872
3087878787878783113878787878787803
483333333333333262333333333333384</pre>
=={{header|C}}==
{{trans|C++}}
<
#include <stdio.h>
#include <stdint.h>
Line 271 ⟶ 536:
return 0;
}</
{{out}}
Line 311 ⟶ 576:
=={{header|C++}}==
The idea of generating palindromes first then testing for gapfulness was borrowed from other solutions.
<
#include <cstdint>
Line 400 ⟶ 665:
return 0;
}</
{{out}}
Line 440 ⟶ 705:
=={{header|Crystal}}==
===Brute force and slow===
<
r1 = (10_u64**pow + 1) * digit
r2 = 10_u64**pow * (digit + 1)
Line 467 ⟶ 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" }</
===Orders of Magnitude Faster: Direct Generation of Numbers===
Line 476 ⟶ 741:
Optimized version using number<->string conversion: 21.5 secs
<
gapfuls = [] of UInt64 # array of palindromic gapfuls
dd = 11_u64 * digit # digit gapful divisor: 11, 22,...88, 99
Line 531 ⟶ 796:
1.upto(9) { |digit| puts "#{digit} : #{palindromicgapfuls(digit, count).last(keep)}" }
puts (Time.monotonic - start).total_seconds</
Compact version: 22.0 secs
<
gapfuls = [] of UInt64 # array of palindromic gapfuls
dd = 11_u64 * digit # digit gapful divisor: 11, 22,...88, 99
Line 582 ⟶ 847:
1.upto(9) { |digit| puts "#{digit} : #{palindromicgapfuls(digit, count).last(keep)}" }
puts (Time.monotonic - start).total_seconds</
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)
<
include Enumerable(UInt64)
Line 647 ⟶ 912:
1.upto(9) { |digit| puts "#{digit} : #{PalindromicGapfuls.new(digit).first(count).last(keep)}" }
puts (Time.monotonic - start).total_seconds</
Original version optimized for minimal memory use: 24.6 secs
<
palcnt = 0 # count of gapful palindromes
to_skip = count - keep # count of unwanted values to skip
Line 706 ⟶ 971:
1.upto(9) { |digit| puts "#{digit} : #{palindromicgapfuls(digit, count, keep)}" }
puts (Time.monotonic - start).total_seconds</
Compact version optimized for minimal memory use: 24.5 secs
<
palcnt = 0 # count of gapful palindromes
to_skip = count - keep # count of unwanted values to skip
Line 759 ⟶ 1,024:
1.upto(9) { |digit| puts "#{digit} : #{palindromicgapfuls(digit, count, keep)}" }
puts (Time.monotonic - start).total_seconds</
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.
<
include Enumerable(UInt64)
Line 834 ⟶ 1,099:
1.upto(9) { |digit| puts "#{digit} : #{PalindromicGapfuls.new(digit).keep_from(count, keep)}" }
puts (Time.monotonic - start).total_seconds</
Compact minimized memory version that numerically create palindromes: 9.2 secs
<
palcnt = 0 # count of gapful palindromes
to_skip = count - keep # count of unwanted values to skip
Line 895 ⟶ 1,160:
1.upto(9) { |digit| puts "#{digit} : #{palindromicgapfuls(digit, count, keep)}" }
puts (Time.monotonic - start).total_seconds</
OOP version optimized for minimal memory use, palindromes created numerically - 9.59 secs
<
include Enumerable(UInt64)
Line 971 ⟶ 1,236:
1.upto(9) { |digit| puts "#{digit} : #{PalindromicGapfuls.new(digit).keep_from(count, keep)}" }
puts (Time.monotonic - start).total_seconds</
{{out}}
Line 1,048 ⟶ 1,313:
Run as: $ crystal run --release fsharp2crystal.cr
Best Time: 29.455717914 secs
<
@digit : UInt64
@dd : UInt64
Line 1,090 ⟶ 1,355:
puts (Time.monotonic - start).total_seconds
</syntaxhighlight>
{{out}}
<pre>
Line 1,153 ⟶ 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#}}==
<
// 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,167 ⟶ 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>
{{out}}
<pre>
Line 1,202 ⟶ 1,852:
</pre>
=={{header|Factor}}==
<
math.functions math.ranges math.text.utils prettyprint sequences ;
IN: rosetta-code.palindromic-gapful-numbers
Line 1,254 ⟶ 1,904:
15 100 ! part 2
10 1000 ! part 3 (Optional)
[ show-palindromic-gapfuls ] 2tri@</
{{out}}
<pre style="height:45ex">
Line 1,293 ⟶ 1,943:
=={{header|FreeBASIC}}==
Time-consuming brute-force solution with only a few speedups...
<
if n<100 then return false
dim as string ns = str(n)
Line 1,351 ⟶ 2,001:
print_range(yays(), 1, 20)
print_range(yays(), 86, 100)
print_range(yays(), 991, 1000)</
{{out}}
<pre>
Line 1,402 ⟶ 2,052:
=={{header|Fōrmulæ}}==
=={{header|Go}}==
Line 1,412 ⟶ 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.
<
import "fmt"
Line 1,515 ⟶ 2,161:
fmt.Println()
}
}</
{{out}}
Line 1,599 ⟶ 2,245:
=={{header|Haskell}}==
===Brute Force===
<
palindromic :: Int -> Bool
Line 1,634 ⟶ 2,280:
putStrLn "\nLast 10 of first 1000 palindromic gapful numbers ending in:"
showSets (show . drop 990 . take 1000 . result)
putStrLn "\ndone."</
===Optimized===
Here the approach is to generate a series of palindromes.
<
import Control.Monad (guard)
Line 1,672 ⟶ 2,318:
, ("Last 15 of first 100", drop 85 . take 100)
, ("Last 10 of first 1000", drop 990 . take 1000)
]</
{{out}}
<pre>
Line 1,730 ⟶ 2,376:
</pre>
Part 2:
<syntaxhighlight lang="text">
palindromify=: [: , ((,~ |.@}.) ; (,~ |.))&>
gapful=: (0 = (|~ ({.,{:)&.(10&#.inv)))&>
Line 1,736 ⟶ 2,382:
task2_palindromes=: [: 10&#.&> [: palindromify task2_cartesian_products
task2_gapfuls=: [: /:~ [: ; [: (#~ gapful)@task2_palindromes&.> >:@i.
</syntaxhighlight>
<pre>
Line 1,802 ⟶ 2,448:
=={{header|Java}}==
<
import java.util.ArrayList;
import java.util.HashMap;
Line 1,912 ⟶ 2,558:
}
</syntaxhighlight>
{{out}}
Line 1,951 ⟶ 2,597:
=={{header|Julia}}==
<
struct Palindrome x1::UInt8; x2::UInt8; outer::UInt8; end
Line 1,995 ⟶ 2,641:
testpal()
</
<pre style="height:100ex;overflow:scroll">
Last digit | Last 20 of 20 palindromic gapful numbers from 100
Line 2,088 ⟶ 2,734:
1770.411549 seconds (13.02 G allocations: 443.799 GiB, 40.12% gc time)
</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">ClearAll[GapfulQ, GetFirstPalindromicGapfulNumbers]
GapfulQ[n_Integer] := Divisible[n, FromDigits[IntegerDigits[n][[{1, -1}]]]]
GetFirstPalindromicGapfulNumbers[startend_, n_Integer] :=
Module[{out = {}, i, new, digs, id},
digs = 1;
While[Length[out] < n,
Do[
id = IntegerDigits[i, 10, Ceiling[digs/2]];
If[OddQ[digs],
new = Join[{startend}, id, Rest@Reverse[id], {startend}]
,
new = Join[{startend}, id, Reverse[id], {startend}]
];
new //= FromDigits;
If[GapfulQ[new],
AppendTo[out, new]
];
i++;
,
{i, 0, 10^Ceiling[digs/2] - 1}
];
digs += 1;
];
Take[out, n]
]
Print["First 20 palindromic gapful numbers >100 ending with each digit from 1 to 9:"]
Print[GetFirstPalindromicGapfulNumbers[#, 20]] & /@ Range[9];
Print["86th to 100th:"]
Print[GetFirstPalindromicGapfulNumbers[#, 100][[86 ;; 100]]] & /@ Range[9];
Print["991st to 1000th:"]
Print[GetFirstPalindromicGapfulNumbers[#, 1000][[991 ;; 1000]]] & /@ Range[9];</syntaxhighlight>
{{out}}
<pre>First 20 palindromic gapful numbers >100 ending with each digit from 1 to 9:
{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}
86th to 100th:
{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}
991st to 1000th:
{17799771,17800871,17811871,17822871,17833871,17844871,17855871,17866871,17877871,17888871}
{27799772,27800872,27811872,27822872,27833872,27844872,27855872,27866872,27877872,27888872}
{3084004803,3084334803,3084664803,3084994803,3085225803,3085555803,3085885803,3086116803,3086446803,3086776803}
{482282284,482414284,482535284,482656284,482777284,482898284,482909284,483020384,483141384,483262384}
{57800875,57811875,57822875,57833875,57844875,57855875,57866875,57877875,57888875,57899875}
{6084004806,6084334806,6084664806,6084994806,6085225806,6085555806,6085885806,6086116806,6086446806,6086776806}
{7452992547,7453223547,7453993547,7454224547,7454994547,7455225547,7455995547,7456226547,7456996547,7457227547}
{8085995808,8086006808,8086116808,8086226808,8086336808,8086446808,8086556808,8086666808,8086776808,8086886808}
{9675005769,9675995769,9676886769,9677777769,9678668769,9679559769,9680440869,9681331869,9682222869,9683113869}</pre>
=={{header|Nim}}==
Line 2,093 ⟶ 2,803:
Forms palindromes using number<->string conversions.
<syntaxhighlight lang
import times # for timing code execution
import unicode # for reversed
Line 2,147 ⟶ 2,857:
for digit in 1..9: echo(digit, " : ", palindromicgapfuls(digit, count, keep) )
echo (epochTime() - start)</
System: I7-6700HQ, 3.5GHz, Linux Kernel 5.9.10, GCC 10.2.0, Nim 1.4.0
Line 2,156 ⟶ 2,866:
Faster version performing number<->string conversions for palindromes.
<syntaxhighlight lang
import times # for timing code execution
import unicode # for reversed
Line 2,208 ⟶ 2,918:
for digit in 1..9: echo(digit, " : ", palindromicgapfuls(digit, count, keep) )
echo (epochTime() - start)</
System: I7-6700HQ, 3.5GHz, Linux Kernel 5.9.10, GCC 10.2.0, Nim 1.4.0
Line 2,217 ⟶ 2,927:
Fastest: make palindromes directly numerically.
<
proc make_palindrome(front_half: uint64, power: int): uint64 =
var
if (power and 1) == 0:
while front_half > 0:
front_half = front_half div 10
proc palindromicgapfuls(digit, count, keep: int): seq[uint64] =
Line 2,272 ⟶ 2,982:
for digit in 1..9: echo(digit, " : ", palindromicgapfuls(digit, count, keep) )
echo (epochTime() - start)</
System: I7-6700HQ, 3.5GHz, Linux Kernel 5.9.14, GCC 10.2.0, Nim 1.4.2
Line 2,355 ⟶ 3,065:
9 : 968787783387787869</pre>
<
{$IFDEF FPC}
{$MODE DELPHI}{$OPTIMIZATION ON,ALL}{$CODEALIGN proc=16}{$ALIGN 16}
Line 2,527 ⟶ 3,237:
OutPalinGap(10000000,10000000,dgt);
writeln;
end.</
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,596 ⟶ 3,306:
real 0m4,503s
</pre>
=={{header|Perl}}==
Minor (and inefficient) tweak on the [[Gapful numbers]] task.
<syntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
use constant Inf => 1e10;
sub is_p_gapful {
my($d,$n) = @_;
return '' unless 0 == $n % 11;
my @digits = split //, $n;
$d eq $digits[0] and (0 == $n % ($digits[0].$digits[-1])) and $n eq join '', reverse @digits;
}
for ([1, 20], [86, 15]) {
my($offset, $count) = @$_;
say "Palindromic gapful numbers starting at $offset:";
for my $d ('1'..'9') {
my $n = 0; my $out = "$d: ";
$out .= do { $n+1 < $count+$offset ? (is_p_gapful($d,$_) and ++$n and $n >= $offset and "$_ ") : last } for 100 .. Inf;
say $out
}
say ''
}</syntaxhighlight>
{{out}}
<pre style="height:20ex">Palindromic gapful numbers starting at 1:
1: 121 1001 1111 1221 1331 1441 1551 1661 1771 1881 1991 10901 11011 12221 13431 14641 15851 17171 18381 19591
2: 242 2002 2112 2222 2332 2442 2552 2662 2772 2882 2992 20702 21912 22022 23232 24442 25652 26862 28182 29392
3: 363 3003 3333 3663 3993 31713 33033 36663 300003 303303 306603 309903 312213 315513 318813 321123 324423 327723 330033 333333
4: 484 4004 4224 4444 4664 4884 40304 42724 44044 46464 48884 400004 401104 402204 403304 404404 405504 406604 407704 408804
5: 5005 5115 5225 5335 5445 5555 5665 5775 5885 5995 50105 51315 52525 53735 54945 55055 56265 57475 58685 59895
6: 6006 6336 6666 6996 61116 64746 66066 69696 600006 603306 606606 609906 612216 615516 618816 621126 624426 627726 630036 633336
7: 7007 7777 77077 700007 707707 710017 717717 720027 727727 730037 737737 740047 747747 750057 757757 760067 767767 770077 777777 780087
8: 8008 8448 8888 80608 86768 88088 800008 802208 804408 806608 808808 821128 823328 825528 827728 829928 840048 842248 844448 846648
9: 9009 9999 94149 99099 900009 909909 918819 927729 936639 945549 954459 963369 972279 981189 990099 999999 9459549 9508059 9557559 9606069
Palindromic gapful numbers starting at 86:
1: 165561 166661 167761 168861 169961 170071 171171 172271 173371 174471 175571 176671 177771 178871 179971
2: 265562 266662 267762 268862 269962 270072 271172 272272 273372 274472 275572 276672 277772 278872 279972
3: 30366303 30399303 30422403 30455403 30488403 30511503 30544503 30577503 30600603 30633603 30666603 30699603 30722703 30755703 30788703
4: 4473744 4485844 4497944 4607064 4619164 4620264 4632364 4644464 4656564 4668664 4681864 4693964 4803084 4815184 4827284
5: 565565 566665 567765 568865 569965 570075 571175 572275 573375 574475 575575 576675 577775 578875 579975
6: 60399306 60422406 60455406 60488406 60511506 60544506 60577506 60600606 60633606 60666606 60699606 60722706 60755706 60788706 60811806
7: 72299227 72322327 72399327 72422427 72499427 72522527 72599527 72622627 72699627 72722727 72799727 72822827 72899827 72922927 72999927
8: 80611608 80622608 80633608 80644608 80655608 80666608 80677608 80688608 80699608 80800808 80811808 80822808 80833808 80844808 80855808
9: 95311359 95400459 95499459 95588559 95677659 95766759 95855859 95944959 96033069 96122169 96211269 96300369 96399369 96488469 96577569</pre>
=={{header|Phix}}==
{{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.
<!--<syntaxhighlight lang="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>
<span style="color: #004080;">atom</span> <span style="color: #000000;">e</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">></span><span style="color: #000000;">0</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">e</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">e</span><span style="color: #0000FF;">*</span><span style="color: #000000;">10</span> <span style="color: #0000FF;">+</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">/</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">e</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">mx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1000</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">data</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">20</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"%7d "</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">86</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">100</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"%8d "</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">991</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1000</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"%10d "</span><span style="color: #0000FF;">}}</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">\</span><span style="color: #7060A8;">ordinal</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">digit</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">results</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</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;">fl</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">*</span><span style="color: #000000;">11</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">nd</span><span style="color: #0000FF;">=</span><span style="color: #000000;">3</span> <span style="color: #008080;">to</span> <span style="color: #000000;">15</span> <span style="color: #008080;">do</span> <span style="color: #000080;font-style:italic;">-- (number of digits, usually quits early)
-- (obvs. 64-bit phix is fine with 19 digits, but 32-bit ain't)</span>
<span style="color: #004080;">bool</span> <span style="color: #000000;">odd</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">nd</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)==</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">=</span><span style="color: #000000;">d</span><span style="color: #0000FF;">*</span><span style="color: #000000;">pow</span> <span style="color: #008080;">to</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">d</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</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: #008080;">do</span> <span style="color: #000080;font-style:italic;">-- (eg 300 to 399)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">e</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">reverse_n</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">odd</span><span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">:</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span> <span style="color: #000080;font-style:italic;">-- (1 or 10 iterations)</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">e</span> <span style="color: #0000FF;">+</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">odd</span> <span style="color: #0000FF;">?</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">*</span><span style="color: #000000;">pow</span><span style="color: #0000FF;">*</span><span style="color: #000000;">100</span><span style="color: #0000FF;">+</span><span style="color: #000000;">m</span><span style="color: #0000FF;">*</span><span style="color: #000000;">pow</span><span style="color: #0000FF;">*</span><span style="color: #000000;">10</span>
<span style="color: #0000FF;">:</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">*</span><span style="color: #000000;">pow</span><span style="color: #0000FF;">*</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fl</span><span style="color: #0000FF;">)==</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #000080;font-style:italic;">-- gapful!</span>
<span style="color: #000000;">count</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #000000;">results</span><span style="color: #0000FF;">[</span><span style="color: #000000;">count</span><span style="color: #0000FF;">][</span><span style="color: #000000;">d</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">p</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">count</span><span style="color: #0000FF;">==</span><span style="color: #000000;">mx</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #000000;">results</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;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">odd</span> <span style="color: #008080;">then</span> <span style="color: #000000;">pow</span> <span style="color: #0000FF;">*=</span> <span style="color: #000000;">10</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">count</span><span style="color: #0000FF;"><</span><span style="color: #000000;">mx</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> <span style="color: #000080;font-style:italic;">-- oh dear...</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">results</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">results</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">({},</span><span style="color: #000000;">9</span><span style="color: #0000FF;">),</span><span style="color: #000000;">mx</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">9</span> <span style="color: #008080;">do</span> <span style="color: #000080;font-style:italic;">-- (the start/end digit)</span>
<span style="color: #000000;">results</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">digit</span><span style="color: #0000FF;">(</span><span style="color: #000000;">results</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">data</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #0000FF;">{</span><span style="color: #004080;">integer</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">e</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">fmt</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">data</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</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;">"%,d%s to %,d%s palindromic gapful numbers (> 100) ending with:\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">ord</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">),</span><span style="color: #000000;">e</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">ord</span><span style="color: #0000FF;">(</span><span style="color: #000000;">e</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">9</span> <span style="color: #008080;">do</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;">"%d: "</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">s</span> <span style="color: #008080;">to</span> <span style="color: #000000;">e</span> <span style="color: #008080;">do</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: #000000;">fmt</span><span style="color: #0000FF;">,</span><span style="color: #000000;">results</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">][</span><span style="color: #000000;">d</span><span style="color: #0000FF;">])</span>
<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;">"\n"</span><span style="color: #0000FF;">)</span>
<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;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
{{out}}
<pre style="font-size: 11px">
Line 2,690 ⟶ 3,454:
</pre>
=== Ludicrously fast to
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/pgn.htm here].
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Palindromic_gapful_numbers.exw
-- ===========================================
--
-- Astonishingly this is all done with standard precision numbers, <2^53.
-- I will credit [[Self_numbers#AppleScript]] and comment by Nigel Galloway
-- on the talk page for ideas that inspired me.
--
-- A palindrome such as 9459549 can be constructed/broken down into
-- 9000009
-- 400040
-- 50500
-- 9000
--
-- Further, 9459549 rem 99 is the same as
-- (the sum of rem 99 on all of those pieces) rem 99
--
-- Finding eg 400040 rem 99 can also be simplified, it is of
-- same as (400000 rem 99 + 40 rem
-- is the same as (4 rem 99[already known])*10 rem
--
-- Also, when filling a "hole", such as the final 9, we find
--
--
--
-- 9452549
--
--
-- 9455549 rem 99
-- 9456549 rem 99 = 69,
-- 9457549 rem 99 = 79,
-- 9458549 rem 99 = 89, and
--
--
-- in this case only the '9' fits.
--
-- But
--
-- when filling the 505-sized hole - what will "fit" depends not on
-- what the "outer" actually are, but what their sum rem 99 is, and
-- likewise for larger and larger holes.
-- If we later find ourselves looking at the same size hole, with
-- the same outer rem and the same rem 99 outmost requirement, we
-- would know instantly how many things are going to fit.
-- True, keeping full lists as the holes got bigger would probably
--
-- albeit one keyed on 4 conditions, we can cope. It turns out that
-- even by the 10^15th scan, we only hit 17,579 variations anyway.
--
--
-- ten millionth item is almost as fast as the first millionth, as
-- opposed to the times 10 that you'd normally expect.
--
-- Note that if I stumble across a hole that will fit more than I'm
-- prepared to fully skip, I start going through things one-by-one,
-- but that's ok because many smaller but still quite big holes
-- 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.
-- each gets extended with 10 rem 11 .. 10 rem 99,
-- 100 rem 11 .. 100 rem 99,
--
-- (not really worth trying to take advantage of any cycles
-- that might appear in such a relatively small table, as
-- it will be at most (on 64-bit) 9 * 9
--</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">rmdrn</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),
-- gap is zeroes between (-1,0,1,2,.. for eg 1,11,101,1001),
-- pow is trailing zeros (0,1,2,.. for eg 101,1010,10100),
-- n is 1..9 for 11..99
-- eg rmdrn(4,3,2,1) yields remainder(4000400,11), but
-- that === remainder(remainder(4000000,11)+
--
-- if k = remainder(4*10^(m-1),11) [already known] then
-- remainder(4*10^m,11) ===
-- we only need to keep a small table for each [digit][n].
-- Thus we avoid maths on 10^42-ish numbers/needing gmp.
--</span>
<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: #004080;">sequence</span> <span style="color: #000000;">cdn</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: #008080;">while</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cdn</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: #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;">cdn</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;">cdn</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>
<span style="color: #004080;">integer</span> <span style="color: #000000;">skipd</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">new_dict</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">palindromicgapfuls</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">pals</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">lhs</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</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: #004080;">integer</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">r</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: #000080;font-style:italic;">--
-- pals: results (passing it up grants it automatic pass-by-reference status, which may help speedwise)
-- lhs: eg "945" of a potential 9459549 result
-- 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 rmdrn()
-- p: left shift (should in fact always equal length(lhs), I think)
-- dd: outermost 1..9 (despite the name, it's a single digit)
--</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">key</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r</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: #004080;">integer</span> <span style="color: #000000;">node</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">getd_index</span><span style="color: #0000FF;">(</span><span style="color: #000000;">key</span><span style="color: #0000FF;">,</span><span style="color: #000000;">skipd</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">skip</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">node</span><span style="color: #0000FF;">==</span><span style="color: #004600;">null</span><span style="color: #0000FF;">?</span><span style="color: #000000;">count</span><span style="color: #0000FF;">:</span><span style="color: #7060A8;">getd_by_index</span><span style="color: #0000FF;">(</span><span style="color: #000000;">node</span><span style="color: #0000FF;">,</span><span style="color: #000000;">skipd</span><span style="color: #0000FF;">)),</span> <span style="color: #000000;">skipn</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">node</span><span style="color: #0000FF;">!=</span><span style="color: #004600;">null</span> <span style="color: #008080;">and</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">palcount</span><span style="color: #0000FF;">+</span><span style="color: #000000;">skip</span><span style="color: #0000FF;">)<</span><span style="color: #000000;">to_skip</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">palcount</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">skip</span>
<span style="color: #008080;">else</span>
<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;">rmdrn</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>
<span style="color: #000000;">palcount</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">palcount</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">to_skip</span> <span style="color: #008080;">then</span>
<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>
<span style="color: #008080;">if</span> <span style="color: #000000;">palcount</span><span style="color: #0000FF;">==</span><span style="color: #000000;">count</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">palcount</span><span style="color: #0000FF;"><</span><span style="color: #000000;">to_skip</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">setd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">key</span><span style="color: #0000FF;">,</span><span style="color: #000000;">skip</span><span style="color: #0000FF;">,</span><span style="color: #000000;">skipd</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;">return</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;">skip</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">collect</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: #004080;">atom</span> <span style="color: #000000;">count</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">keep</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">atom</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;">keep</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">palcount</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">3</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">pals</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<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;">rmdrn</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>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">pals</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">tests</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">20</span><span style="color: #0000FF;">,</span><span style="color: #000000;">20</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">100</span><span style="color: #0000FF;">,</span><span style="color: #000000;">15</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">10_000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">100_000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">1_000_000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">10_000_000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">100_000_000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</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: #000000;">1000_000_000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</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: #000000;">10_000_000_000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</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: #0000FF;">{</span><span style="color: #000000;">100_000_000_000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</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: #000000;">1000_000_000_000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</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: #0000FF;">{</span><span style="color: #000000;">10_000_000_000_000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</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: #000000;">100_000_000_000_000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</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: #0000FF;">{</span><span style="color: #000000;">1_000_000_000_000_000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</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;">-- {1_000_000_000_000_000,1,2,4}, -- (matches AppleScript)</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">10_000_000_000_000_000_000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</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;">-- 64 bit only
-- (any further and you'd need mpfr just to hold counts)</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">(),</span> <span style="color: #000000;">count</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">keep</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">start</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">finish</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">)-(</span><span style="color: #7060A8;">machine_bits</span><span style="color: #0000FF;">()!=</span><span style="color: #000000;">64</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">count</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">keep</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">start</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">finish</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">count</span><span style="color: #0000FF;">==</span><span style="color: #000000;">keep</span><span style="color: #0000FF;">?</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"First %d"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">count</span><span style="color: #0000FF;">}):</span>
<span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">keep</span><span style="color: #0000FF;">></span><span style="color: #000000;">1</span><span style="color: #0000FF;">?</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Last %d of first %,d"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">keep</span><span style="color: #0000FF;">,</span><span style="color: #000000;">count</span><span style="color: #0000FF;">})</span>
<span style="color: #0000FF;">:</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%,dth"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">count</span><span style="color: #0000FF;">}))),</span>
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">keep</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">?</span><span style="color: #008000;">""</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"s"</span><span style="color: #0000FF;">)</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;">"%s palindromic gapful number%s ending with:\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">})</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">tags</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">finish</span><span style="color: #0000FF;">,</span><span style="color: #000000;">start</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #000000;">collect</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">tags</span><span style="color: #0000FF;">,</span><span style="color: #000000;">count</span><span style="color: #0000FF;">,</span><span style="color: #000000;">keep</span><span style="color: #0000FF;">})</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">fmt</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%%%ds"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,{}),</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">)))</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</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;">"%d: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">tags</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">],</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">,{{</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">},</span><span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]}),</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">puts</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>
<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>
<!--</syntaxhighlight>-->
{{out}}
<pre style="font-size:
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 2,996 ⟶ 3,750:
Completed in 0.7s
</pre>
On 64bit you'll also get (in 0.8s)
<pre style="font-size: 11px">
10,000,000,000,000,000,000th palindromic gapful number ending with:
9: 968787878787878787639936787878787878787869
</pre>
I might agree that the last entry does not feel very convincing. Depending on how much
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. :-)
<
while count do
count -= 1
?collect(9,count,1)
end while</
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<
Next is Digit * 10 - 1.
Line 3,113 ⟶ 3,872:
print_numbers(1, 20, Numbers),
print_numbers(86, 100, Numbers),
print_numbers(991, 1000, Numbers).</
{{out}}
Line 3,163 ⟶ 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.
<
from pprint import pformat
import re
Line 3,195 ⟶ 3,954:
b = {k:v[-last:] for k, v in bin.items()}
txt = pformat(b, width=220)
print('', re.sub(r"[{},\[\]]", '', txt))</
{{out}}
Line 3,233 ⟶ 3,992:
===Functional===
<
from itertools import chain, count, islice, tee
Line 3,434 ⟶ 4,193:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>First 20 samples of gapful palindromes (> 100) by last digit:
Line 3,473 ⟶ 4,232:
{{works with|Rakudo|2019.07.1}}
<syntaxhighlight lang="raku"
# Infinite lazy iterator to generate palindromic "gap" numbers
Line 3,485 ⟶ 4,244:
my @gappal = (1..9).map: -> \digit {
my \divisor = digit + 10 * digit;
@npal
}
Line 3,499 ⟶ 4,258:
put "$_: " ~ @gappal[$_-1][|$range].fmt("%{$fmt}s") for 1..9;
say round( now - $now, .001 ), " seconds";
}</
{{out}}
<pre style="font-size:80%;">(Required) First 20 gapful palindromes:
Line 3,562 ⟶ 4,321:
=={{header|REXX}}==
<
/*─────────────────────── palindromic gapful numbers listed by their last decimal digit.*/
numeric digits 20 /*ensure enough decimal digits gapfuls.*/
Line 3,592 ⟶ 4,351:
do k=1 for 9; say k':' strip( subword($.k, 1 + n - z) )
end /*k*/; say
return</
{{out|output|text= when using the internal default inputs:}}
Line 3,633 ⟶ 4,392:
=={{header|Ring}}==
<
load "stdlib.ring"
Line 3,673 ⟶ 4,432:
see "done..." + nl
</syntaxhighlight>
Output:
<pre>
Line 3,691 ⟶ 4,450:
{{trans|Crystal}}
===Brute force and slow===
<
r1 = digit * (10**pow + 1)
r2 = 10**pow * (digit + 1)
Line 3,718 ⟶ 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" }</
===Orders of Magnitude Faster: Direct Generation of Numbers===
Line 3,726 ⟶ 4,485:
Run as: $ ruby palindromicgapfuls.rb
Optimized version, the ultimate fastest: Ruby 2.7.1 - 112.5 secs
<
gapfuls = [] # array of palindromic gapfuls
nn = digit * 11 # digit gapful divisor: 11, 22,...88, 99
Line 3,782 ⟶ 4,541:
1.upto(9) { |digit| puts "#{digit} : #{palindromicgapfuls(digit, count).last(keep)}" }
puts (Time.now - start)</
Compact version: Ruby-2.7.1 - 113.0 secs
<
gapfuls = [] # array of palindromic gapfuls
nn = digit * 11 # digit gapful divisor: 11, 22,...88, 99
Line 3,834 ⟶ 4,593:
1.upto(9) { |digit| puts "#{digit} : #{palindromicgapfuls(digit, count).last(keep)}" }
puts (Time.now - start)</
Object Oriented implementation: Ruby 2.7.1 - 113.0 secs
<
include Enumerable
Line 3,899 ⟶ 4,658:
1.upto(9) { |digit| puts "#{digit} : #{PalindromicGapfuls.new(digit).first(count).last(keep)}" }
puts (Time.now - start)</
Versions optimized for minimal memory use: Ruby 2.7.1 - 110.0 secs
<
palcnt = 0 # count of gapful palindromes
to_skip = count - keep # count of unwanted values to skip
Line 3,960 ⟶ 4,719:
1.upto(9) { |digit| puts "#{digit} : #{palindromicgapfuls(digit, count, keep)}" }
puts (Time.now - start)</
Compact version optimized for minimal memory use: Ruby 2.7.1 - 111.5 secs
<
palcnt = 0 # count of gapful palindromes
to_skip = count - keep # count of unwanted values to skip
Line 4,014 ⟶ 4,773:
1.upto(9) { |digit| puts "#{digit} : #{palindromicgapfuls(digit, count, keep)}" }
puts (Time.now - start)</
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.
<
include Enumerable
Line 4,090 ⟶ 4,849:
1.upto(9) { |digit| puts "#{digit} : #{PalindromicGapfuls.new(digit).keep_from(count, keep)}" }
puts (Time.now - start)</
{{out}}
<pre>First 20 palindromic gapful numbers 100 ending with:
Line 4,161 ⟶ 4,920:
{{trans|F#}}
0.186s on Intel(R) Core(TM) i5-1035G1 CPU @ 1.00GHz
<
class PalNo
def initialize(set)
Line 4,179 ⟶ 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>
{{out}}
<pre>
Line 4,219 ⟶ 4,978:
Run as: $ ruby fsharp2ruby.rb
Best Time: 437.566515299 secs
<
def initialize(digit)
@digit, @l, @dd = digit, 3, 11*digit
Line 4,247 ⟶ 5,006:
(1..9).each { |digit| PalNo.new(digit).show(10_000_000, 1) }; puts "####"
puts (Time.now - start)</
{{out}}
<pre>
Line 4,318 ⟶ 5,077:
This version uses number->string then string->number conversions to create palindromes.
<
let mut palcnt = 0u64; // count of gapful palindromes
let to_skip = count - keep as u64; // count of unwanted values to skip
Line 4,372 ⟶ 5,131:
println!("{:?}", t.elapsed())
}</
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,382 ⟶ 5,141:
About 2.5x faster.
<
let mut palcnt = 0u64; // count of gapful palindromes
let to_skip = count - keep as u64; // count of unwanted values to skip
Line 4,442 ⟶ 5,201:
println!("{:?}", t.elapsed())
}</
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,516 ⟶ 5,275:
=={{header|Sidef}}==
Inspired from the C++ and Raku entries.
<
has power = base
Line 4,550 ⟶ 5,309:
say ("#{d}: ", arr.map{ "%*s" % (w, _) }.join(' '))
}
})</
{{out}}
<pre style="font-size:83%">
Line 4,602 ⟶ 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.
<
var reverse = Fn.new { |s|
Line 4,661 ⟶ 5,420:
}
System.print()
}</
{{out}}
Line 4,719 ⟶ 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
<
// 10,False --> 1001,1111,1221,1331,1441,1551,1661,1771,1881,..
fcn createPalindromeW(start,oddLength){ //--> iterator
Line 4,740 ⟶ 5,563:
if(p%10==endsWith and (p%div)==0) p else Void.Skip
})
}</
<
[1..9].apply(palindromicGapfulW).apply("walk",20) : pgpp(_);
Line 4,752 ⟶ 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)) }
}</
{{out}}
<pre style="font-size:83%">
Line 4,788 ⟶ 5,611:
9: 9675005769 9675995769 9676886769 9677777769 9678668769 9679559769 9680440869 9681331869 9682222869 9683113869
</pre>
<
* (3 min to 48sec) with 8 cores (Intel 4/4).
*/
Line 4,797 ⟶ 5,620:
.apply("noop") // wait for threads to finish
: pgpp(_);
}</
{{out}}
<pre style="font-size:83%">
|