Palindromic gapful numbers: Difference between revisions

m
m (→‎{{header|AppleScript}}: Further minor optimisation merging the two innermost repeats.)
m (→‎{{header|Wren}}: Minor tidy)
 
(48 intermediate revisions by 11 users not shown)
Line 57:
=={{header|AppleScript}}==
 
<langsyntaxhighlight lang="applescript">on doTask()
script o
property collector : missing value
end script
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 68 ⟶ 64:
set AppleScript's text item delimiters to " "
repeat with endDigit from 1 to 9
set o's{collector1, collectorcollector2, collector3} to {{}, {}, {}}
set outerNumber to endDigit * 11 -- Number formed from the palindromes' first and last digits.
set oddDigitCount to true -- WithStarting with palindromes starting in the hundreds, the first ones will have an odd number of digits ….
set baseHi to endDigit * 10 -- Number formed from just the "high end" digits, initially endDigit and a middle 0.
set oddDigitCount to true
-- … and their "high ends" will consist of the specified end digit (leading) and a "middle" digit.
set baseHi to endDigit * 10
set hi to baseHi
set carryCheck to hi + 10 -- Number reached when incrementing the "high end" number changes its first digit.
repeat until ((count o's collector) is 1000)
set inc to 10 -- MakeIncrementor a palindrome offor the current high end value, not repeating the "middle" digit(s) whenof inthe oddDigitCountpalindromes modethemselves.
set counter to 0
-- At the same time, get the lead digit of (the high end value + 1) for later testing.
set nmaxNeeded to hi1000
set done to false
repeat until (done)
-- Work out every 10th palindrome (middle digit = 0) from the current "high end" number.
set pal to hi
if (oddDigitCount) then
set temp to hi div 10
set carryCheck to hi + 1
else
set temp to hi
set carryCheck to (hi + 1) * 10
end if
repeat until (temp is 0)
set npal to npal * 10 + temp mod 10
set temp to temp div 10
set carryCheck to carryCheck div 10
end repeat
-- IfCheck the palindrome result's gapful,and addthe itfollowing to9 palindromes (derived by incrementing the collection.middle digit(s))
if-- (nand modstore outerNumberas istext 0)any thenwhich setare endboth ofgapful o'sand collectorthe toones n div 1required.
repeat 10 times
-- Increment the high end value if this won't change its lead digit. Otherwise reset for longer palindromes.
if (carryCheckpal mod outerNumber is endDigit0) then
set hicounter to hicounter + 1
else if (counter ≤ 20) then
set end of collector1 to intText(pal)
else if (counter < 86) then
else if (counter ≤ 100) then
set end of collector2 to intText(pal)
else if (counter < 991) then
else --if (counter ≤ 1000) then
set end of collector3 to intText(pal)
set done to (counter = maxNeeded)
if (done) then exit repeat
end if
end if
set pal to pal + inc
end repeat
-- Increment the high end number's penultimate digit after every 10th palindrome.
-- If a carry changes its first digit, reset for longer palindromes.
set hi to hi + 10
if (hi = carryCheck) then
set oddDigitCount to (not oddDigitCount)
if (oddDigitCount) then set baseHi to baseHi * 10
set baseHi to baseHi * 10
set carryCheck to carryCheck * 10
set inc to inc div 11 * 10
else
set inc to inc * 11
end if
set hi to baseHi
end if
end repeat
set {end of part1, end of part2, end of part3} to (items{collector1 1as thrutext, 20collector2 ofas o'stext, collector)collector3 as text}
set end of part2 to (items 86 thru 100 of o's collector) as text
set end of part3 to (items 991 thru end of o's collector) as text
end repeat
set AppleScript's text item delimiters to linefeed
Line 113 ⟶ 129:
end doTask
 
on intText(n)
return doTask()</lang>
if (n < 100000000) then return n as text
set txt to text 2 thru end of ((100000000 + (n mod 100000000 as integer)) as text)
set n to n div 100000000
repeat
set lo to n mod 100000000 as integer
set n to n div 100000000
if (n is 0) then return (lo as text) & txt
set txt to (text 2 thru end of ((100000000 + lo) as text)) & txt
end repeat
end intText
 
return doTask()</syntaxhighlight>
 
{{output}}
Line 141 ⟶ 169:
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
3.084004803E+9 3.084334803E+9 3.084664803E+9 3.084994803E+9 3.085225803E+9 3.085555803E+9 3.085885803E+9 3.086116803E+9 3.086446803E+9 3.086776803E+9
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
6.084004806E+9 6.084334806E+9 6.084664806E+9 6.084994806E+9 6.085225806E+9 6.085555806E+9 6.085885806E+9 6.086116806E+9 6.086446806E+9 6.086776806E+9
7452992547 7453223547 7453993547 7454224547 7454994547 7455225547 7455995547 7456226547 7456996547 7457227547
7.452992547E+9 7.453223547E+9 7.453993547E+9 7.454224547E+9 7.454994547E+9 7.455225547E+9 7.455995547E+9 7.456226547E+9 7.456996547E+9 7.457227547E+9
8085995808 8086006808 8086116808 8086226808 8086336808 8086446808 8086556808 8086666808 8086776808 8086886808
8.085995808E+9 8.086006808E+9 8.086116808E+9 8.086226808E+9 8.086336808E+9 8.086446808E+9 8.086556808E+9 8.086666808E+9 8.086776808E+9 8.086886808E+9
9675005769 9675995769 9676886769 9677777769 9678668769 9679559769 9680440869 9681331869 9682222869 9683113869"</pre>
9.675005769E+9 9.675995769E+9 9.676886769E+9 9.677777769E+9 9.678668769E+9 9.679559769E+9 9.680440869E+9 9.681331869E+9 9.682222869E+9 9.683113869E+9"</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++}}
<langsyntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
Line 243 ⟶ 536:
 
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 283 ⟶ 576:
=={{header|C++}}==
The idea of generating palindromes first then testing for gapfulness was borrowed from other solutions.
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <cstdint>
 
Line 372 ⟶ 665:
 
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 412 ⟶ 705:
=={{header|Crystal}}==
===Brute force and slow===
<langsyntaxhighlight lang="ruby">def palindromesgapful(digit, pow)
r1 = (10_u64**pow + 1) * digit
r2 = 10_u64**pow * (digit + 1)
Line 439 ⟶ 732:
count = 1000
puts "\nLast 10 of first 1000 palindromic gapful numbers ending in:"
(1..9).each { |digit| print "#{digit} : #{digitscount(digit, count).last(10)}\n" }</langsyntaxhighlight>
 
===Orders of Magnitude Faster: Direct Generation of Numbers===
Crystal is a statically typed and a compiled language.
<BR>The code as implemented has been tested to produce optimum performance.
System: I7-6700HQ, 3.5 GHz5GHz, Linux Kernel 5.6.17, Crystal 0.35
Run as: $ crystal run --release palindromicgapfuls.cr
 
Optimized version using number<->string conversion, the ultimate fastest: 21.5 secs
<langsyntaxhighlight lang="ruby">def palindromicgapfuls(digit, count)
gapfuls = [] of UInt64 # array of palindromic gapfuls
nndd = digit11_u64 * 11 digit # digit gapful divisor: 11, 22,...88, 99
(2..).select do |power|
base = 10_u64**(power >> 1) # value of middle digit position: 10..
Line 457 ⟶ 750:
next_lo = base * (digit + 1) # starting half for next digit: 20.. to 100..
this_lo.step(to: next_lo - 1, by: 10) do |front_half| # d_00; d_10; d_20; ...
left_half = front_half.to_s; right_half = left_half.reverse
if power.odd?
palindrome = (left_half + right_half).to_u64
10.times do
gapfuls << palindrome if palindrome.divisible_by?(nndd)
return gapfuls if gapfuls.size == count
palindrome += base11
Line 468 ⟶ 761:
palindrome = (left_half.rchop + right_half).to_u64
10.times do
gapfuls << palindrome if palindrome.divisible_by?(nndd)
return gapfuls if gapfuls.size == count
palindrome += base
Line 503 ⟶ 796:
1.upto(9) { |digit| puts "#{digit} : #{palindromicgapfuls(digit, count).last(keep)}" }
 
puts (Time.monotonic - start).total_seconds</langsyntaxhighlight>
 
Compact version: 22.0 secs
<langsyntaxhighlight lang="ruby">def palindromicgapfuls(digit, count)
gapfuls = [] of UInt64 # array of palindromic gapfuls
nndd = digit11_u64 * 11 digit # digit gapful divisor: 11, 22,...88, 99
(2..).select do |power|
base = 10_u64**(power >> 1) # value of middle digit position: 10..
Line 520 ⟶ 813:
palindrome = (left_half + right_half).to_u64
10.times do
gapfuls << palindrome if palindrome.divisible_by?(nndd)
return gapfuls if gapfuls.size == count
palindrome += basep
Line 554 ⟶ 847:
1.upto(9) { |digit| puts "#{digit} : #{palindromicgapfuls(digit, count).last(keep)}" }
 
puts (Time.monotonic - start).total_seconds</langsyntaxhighlight>
 
Object Oriented implementation: same speed - 21.8 seconds
<Br>Here using a Struct (allocated on stack) is faster than using a Class (allocated on heap)
<langsyntaxhighlight lang="ruby">struct PalindromicGapfuls
include Enumerable(UInt64)
 
@nndd : Int32
 
def initialize(@digit : Int32)
@nndd = @digit11 * 11@digit # digit gapful divisor: 11, 22,...88, 99
end
 
Line 578 ⟶ 871:
palindrome = (left_half + right_half).to_u64
10.times do
yield palindrome if palindrome.divisible_by?(@nndd)
palindrome += base11
end
Line 584 ⟶ 877:
palindrome = (left_half.rchop + right_half).to_u64
10.times do
yield palindrome if palindrome.divisible_by?(@nndd)
palindrome += base
end
Line 619 ⟶ 912:
1.upto(9) { |digit| puts "#{digit} : #{PalindromicGapfuls.new(digit).first(count).last(keep)}" }
 
puts (Time.monotonic - start).total_seconds</langsyntaxhighlight>
 
Original version optimized for minimal memory use: 24.6 secs
<langsyntaxhighlight lang="ruby">def palindromicgapfuls(digit, count, keep)
palcnt = 0 # count of gapful palindromes
to_skip = count - keep # count of unwanted values to skip
gapfuls = [] of UInt64 # array of palindromic gapfuls
nndd = digit11_u64 * 11 digit # digit gapful divisor: 11, 22,...88, 99
(2..).select do |power|
base = 10_u64**(power >> 1) # value of middle digit position: 10..
Line 633 ⟶ 926:
next_lo = base * (digit + 1) # starting half for next digit: 20.. to 100..
this_lo.step(to: next_lo - 1, by: 10) do |front_half| # d_00; d_10; d_20; ...
left_half = front_half.to_s; right_half = left_half.reverse
if power.odd?
palindrome = (left_half + right_half).to_u64
10.times do
(gapfuls << palindrome if palindrome.divisible_by?(dd) && (palcnt += 1) > to_skip) if palindrome.divisible_by?(nn)
palindrome += base11
end
Line 643 ⟶ 936:
palindrome = (left_half.rchop + right_half).to_u64
10.times do
(gapfuls << palindrome if palindrome.divisible_by?(dd) && (palcnt += 1) > to_skip) if palindrome.divisible_by?(nn)
palindrome += base
end
Line 678 ⟶ 971:
1.upto(9) { |digit| puts "#{digit} : #{palindromicgapfuls(digit, count, keep)}" }
 
puts (Time.monotonic - start).total_seconds</langsyntaxhighlight>
 
Compact version optimized for minimal memory use: 24.5 secs
<langsyntaxhighlight lang="ruby">def palindromicgapfuls(digit, count, keep)
palcnt = 0 # count of gapful palindromes
to_skip = count - keep # count of unwanted values to skip
gapfuls = [] of UInt64 # array of palindromic gapfuls
nndd, base = (digit11_u64 * 11).to_u64digit, 1_u64 # digit gapful divisor: 11, 22,...88, 99
(2..).select do |power|
base *= 10 if power.even? # value of middle digit position: 10..
Line 697 ⟶ 990:
palindrome = (left_half + right_half).to_u64
10.times do
(gapfuls << palindrome if palindrome.divisible_by?(dd) && (palcnt += 1) > to_skip) if palindrome.divisible_by?(nn)
palindrome += basep
end
return gapfuls[0...keep] unless gapfuls.sizepalcnt < keepcount
end
end
Line 731 ⟶ 1,024:
1.upto(9) { |digit| puts "#{digit} : #{palindromicgapfuls(digit, count, keep)}" }
 
puts (Time.monotonic - start).total_seconds</langsyntaxhighlight>
 
OOP version optimized for minimal memory use - 25.4 secs
<BR>It creates an output method that skips the unwanted values and only keeps/stores the desired ones.
<langsyntaxhighlight lang="ruby">struct PalindromicGapfuls
include Enumerable(UInt64)
 
@nndd : Int32
 
def initialize(@digit : Int32)
@nndd = @digit11 * 11@digit # digit gapful divisor: 11, 22,...88, 99
end
 
Line 751 ⟶ 1,044:
next_lo = base * (@digit + 1) # starting half for next digit: 20.. to 100..
this_lo.step(to: next_lo - 1, by: 10) do |front_half| # d_00; d_10; d_20; ...
left_half = front_half.to_s; right_half = left_half.reverse
if power.odd?
palindrome = (left_half + right_half).to_u64
10.times do
yield palindrome if palindrome.divisible_by?(@nndd)
palindrome += base11
end
Line 761 ⟶ 1,054:
palindrome = (left_half.rchop + right_half).to_u64
10.times do
yield palindrome if palindrome.divisible_by?(@nndd)
palindrome += base
end
Line 806 ⟶ 1,099:
1.upto(9) { |digit| puts "#{digit} : #{PalindromicGapfuls.new(digit).keep_from(count, keep)}" }
 
puts (Time.monotonic - start).total_seconds</langsyntaxhighlight>
 
Compact minimized memory version that numerically create palindromes: 9.2 secs
<langsyntaxhighlight lang="ruby">def palindromicgapfuls(digit, count, keep)
palcnt = 0 # count of gapful palindromes
to_skip = count - keep # count of unwanted values to skip
gapfuls = [] of UInt64 # array of palindromic gapfuls
nndd, base = digit11_u64 * 11digit, 1_u64 # digit gapful divisor: 11, 22,...88, 99
(2..).select do |power|
base *= 10 if power.even? # value of middle digit position: 10..
Line 823 ⟶ 1,116:
palindrome = make_palindrome(front_half, power)
10.times do
(gapfuls << palindrome if palindrome.divisible_by?(dd) && (palcnt += 1) > to_skip) if palindrome.divisible_by?(nn)
palindrome += basep
end
return gapfuls[0...keep] unless gapfuls.sizepalcnt < keepcount
end
end
Line 835 ⟶ 1,128:
result //= 10 if power.even?
while front_half > 0
result *= result * 10 + front_half.remainder(10)
result += front_half.remainder(10)
front_half //= 10
end
Line 868 ⟶ 1,160:
1.upto(9) { |digit| puts "#{digit} : #{palindromicgapfuls(digit, count, keep)}" }
 
puts (Time.monotonic - start).total_seconds</langsyntaxhighlight>
 
OOP version optimized for minimal memory use, palindromes created numerically - 9.59 secs
<langsyntaxhighlight lang="ruby">struct PalindromicGapfuls
include Enumerable(UInt64)
@nndd : Int32
def initialize(@digit : Int32)
@nndd = @digit11 * 11@digit # digit gapful divisor: 11, 22,...88, 99
end
Line 890 ⟶ 1,182:
palindrome = make_palindrome(front_half, power)
10.times do
yield palindrome if palindrome.divisible_by?(@nndd)
palindrome += basep
end
Line 897 ⟶ 1,189:
end
 
def make_palindrome(front_half, power)
result = front_half
result //= 10 if power.even?
while front_half > 0
result *= 10
result += front_half.remainder(10)
front_half //= 10
end
result
end
# Optimized output method: only keep desired values.
def keep_from(count, keep)
Line 916 ⟶ 1,197:
return kept if kept.size == keep
end
end
 
def make_palindrome(front_half, power)
result = front_half
result //= 10 if power.even?
while front_half > 0
result = result * 10 + front_half.remainder(10)
front_half //= 10
end
result
end
end
 
start = Time.monotonic
count, keep = 20, 20
puts "First 20 palindromic gapful numbers ending with:"
1.upto(9) { |digit| puts "#{digit} : #{PalindromicGapfuls.new(digit).keep_from(count, keep)}" }
count, keep = 100, 15
puts "\nLast 15 of first 100 palindromic gapful numbers ending in:"
1.upto(9) { |digit| puts "#{digit} : #{PalindromicGapfuls.new(digit).keep_from(count, keep)}" }
count, keep = 1_000, 10
puts "\nLast 10 of first 1000 palindromic gapful numbers ending in:"
1.upto(9) { |digit| puts "#{digit} : #{PalindromicGapfuls.new(digit).keep_from(count, keep)}" }
count, keep = 100_000, 1
puts "\n100,000th palindromic gapful number ending with:"
1.upto(9) { |digit| puts "#{digit} : #{PalindromicGapfuls.new(digit).keep_from(count, keep)}" }
count, keep = 1_000_000, 1
puts "\n1,000,000th palindromic gapful number ending with:"
1.upto(9) { |digit| puts "#{digit} : #{PalindromicGapfuls.new(digit).keep_from(count, keep)}" }
count, keep = 10_000_000, 1
puts "\n10,000,000th palindromic gapful number ending with:"
1.upto(9) { |digit| puts "#{digit} : #{PalindromicGapfuls.new(digit).keep_from(count, keep)}" }
puts (Time.monotonic - start).total_seconds</langsyntaxhighlight>
 
{{out}}
Line 1,022 ⟶ 1,313:
Run as: $ crystal run --release fsharp2crystal.cr
Best Time: 29.455717914 secs
<langsyntaxhighlight lang="ruby">class PalNo
@digit : UInt64
@dd : UInt64
Line 1,064 ⟶ 1,355:
 
puts (Time.monotonic - start).total_seconds
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,127 ⟶ 1,418:
[968787783387787869]
####
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
This is among the faster versions of the problem. It solves the standard and optional tasks in 0.7 seconds.
 
<syntaxhighlight lang="Delphi">
 
{-------------Library Routines ----------------------------------------------------------------}
procedure GetDigits(N: integer; var IA: TIntegerDynArray);
{Get an array of the integers in a number}
{Numbers returned from least to most significant}
var T,I,DC: integer;
begin
DC:=Trunc(Log10(N))+1;
SetLength(IA,DC);
for I:=0 to DC-1 do
begin
T:=N mod 10;
N:=N div 10;
IA[I]:=T;
end;
end;
 
function GetNKPalindrome(N,K: int64): int64;
{Get Nth Palindrome with K-number of digits}
{N = Left half, Right = Reversed(left) a }
var Temp,H1,H2,I: int64;
begin
{Get left digit count - depends on K being odd/even}
if (K and 1)<>0 then Temp:=K div 2 else Temp:=K div 2 - 1;
{Get power of 10 digits}
H1:=trunc(Power(10, Temp));
{Add in N}
H1:=H1 + N - 1;
H2:=H1;
{ If K is odd, truncate the last digit}
if (k and 1)<>0 then H2:=H2 div 10;
{Reverse H2 and add to H1}
while H2>0 do
begin
H1:=H1 * 10 + (H2 mod 10);
H2:=H2 div 10;
end;
Result:=H1;
end;
 
 
 
function GetPalDigits(N: int64; var Offset: int64): integer;
{Get number of digits and offset for Nth Palindrome}
{Used to feed GetNKPalindrome to find Nth Palindrome}
var R1,R2,Step: int64;
begin
R1:=0;
{Step through number of digits}
for Result:=1 to 36 do
begin
{Calculate new Range step: 9,9,90,90,90,900,900...}
if (Result and 1)<>0 then Step:=9 * Trunc(Power(10,Result div 2));
{Calculate R2}
R2:=(R1 + Step)-1;
{See if N falls between R1 and R2}
if (N>=R1) and (N<=R2) then
begin
{Calculate offset and exit}
Offset:=(N - R1)+1;
exit;
end;
R1:=R2+1;
end;
end;
 
 
function GetNthPalindrome(N: integer): int64;
{Get the Nth Palindrome number}
var D,Off: int64;
begin
D:=GetPalDigits(N,Off);
Result:=GetNKPalindrome(Off,D);
end;
 
 
 
procedure GetPalindromeList(Count: integer; var Pals: TInt64DynArray);
{Get a list of the first "Count"-number of palinedromes (Fast)}
var D,I,Inx,Max: integer;
begin
{Set array length}
SetLength(Pals,Count);
Inx:=0;
{Handle palindromes up to 18 digits}
for D:=1 to 18 do
begin
{Get maximum count for palindrom of D digits}
if (D and 1)=1 then Max:=Trunc(Power(10,(D + 1) div 2))
else Max:=Trunc(Power(10,D div 2));
{Step through all the numbers half the size of the number of digits}
for I:=1 to Max-Max div 10 do
begin
{Store palindrome}
Pals[Inx]:=GetNKPalindrome(I,D);
Inc(Inx);
{Exit when array is full}
if Inx>=Count then break;
end;
end;
end;
 
 
{------------------------------------------------------------------------------------------------}
 
 
function IsGapful(N: int64): boolean;
{Return true if number is "GapFul"}
{GapFul = combined first/last}
{ digits divide evenly into N}
var Digits: TIntegerDynArray;
var I: int64;
begin
Result:=False;
{Must be 3 digit number}
if N<100 then exit;
{Put digits in array}
GetDigits(N,Digits);
{Form number from first and last digit}
I:=Digits[0] + 10 * Digits[High(Digits)];
{Does it divide evenly into N}
Result:=(N mod I)=0;
end;
 
 
function HasEndDigit(N: int64; Digit: integer): boolean;
{Return true if last digit match specified "Digit"}
var LD: integer;
begin
LD:=N mod 10;
Result:=LD=Digit;
end;
 
 
function GetGapfulPalinEndSet(Max, EndDigit: integer): string;
{Get first Max number of Gapful Palindromes with specified EndDigit}
var I,Cnt,P: integer;
begin
Result:='Ending in: '+IntToStr(EndDigit)+CRLF;
Cnt:=0;
{Get palindromes and test them}
for I:=0 to high(Integer) do
begin
{Get next palinedrome}
P:=GetNthPalindrome(I);
{Is Gapful and has specified EndDigit}
if IsGapFul(P) and HasEndDigit(P,EndDigit) then
begin
Inc(Cnt);
{Display it}
Result:=Result+Format('%8d',[P]);
if (Cnt mod 5)=0 then Result:=Result+CRLF;
{Break when finished}
if Cnt>=Max then break;
end;
end;
end;
 
 
 
function LastGapfulPalinEndSet(Count,Last,EndDigit: integer): string;
{Get Gapful Palindromes with specified EndDigit}
{Get "Last" number of items out of a total "Count" }
var I,Inx: integer;
var P: int64;
var IA: TInt64DynArray;
begin
SetLength(IA,Count);
Result:='Ending in: '+IntToStr(EndDigit)+CRLF;
{Get count number of items}
Inx:=0;
for I:=0 to Count-1 do
begin
{Keep getting palindromes until}
{they Gapful and have specified last digit}
repeat
begin
P:=GetNthPalindrome(Inx);
Inc(Inx);
end
until IsGapFul(P) and HasEndDigit(P,EndDigit);
{Save item}
IA[I]:=P;
end;
{Get last items}
for I:=Count-Last to Count-1 do
begin
Result:=Result+Format('%12d',[IA[I]]);
if (I mod 5)=4 then Result:=Result+CRLF;
end;
end;
 
 
 
procedure ShowPalindromicGapfuls(Memo: TMemo);
var S: string;
begin
Memo.Lines.Add('First 20 palindromic gapful numbers');
 
Memo.Lines.Add(GetGapFulPalinEndSet(20,1));
Memo.Lines.Add(GetGapFulPalinEndSet(20,2));
Memo.Lines.Add(GetGapFulPalinEndSet(20,3));
Memo.Lines.Add(GetGapFulPalinEndSet(20,4));
Memo.Lines.Add(GetGapFulPalinEndSet(20,5));
Memo.Lines.Add(GetGapFulPalinEndSet(20,6));
Memo.Lines.Add(GetGapFulPalinEndSet(20,7));
Memo.Lines.Add(GetGapFulPalinEndSet(20,8));
Memo.Lines.Add(GetGapFulPalinEndSet(20,9));
 
Memo.Lines.Add('86th to 100th');
Memo.Lines.Add(LastGapFulPalinEndSet(100,15,1));
Memo.Lines.Add(LastGapFulPalinEndSet(100,15,2));
Memo.Lines.Add(LastGapFulPalinEndSet(100,15,3));
Memo.Lines.Add(LastGapFulPalinEndSet(100,15,4));
Memo.Lines.Add(LastGapFulPalinEndSet(100,15,5));
Memo.Lines.Add(LastGapFulPalinEndSet(100,15,6));
Memo.Lines.Add(LastGapFulPalinEndSet(100,15,7));
Memo.Lines.Add(LastGapFulPalinEndSet(100,15,8));
Memo.Lines.Add(LastGapFulPalinEndSet(100,15,9));
 
Memo.Lines.Add('991st to 1000th:');
Memo.Lines.Add(LastGapFulPalinEndSet(1000,10,1));
Memo.Lines.Add(LastGapFulPalinEndSet(1000,10,2));
Memo.Lines.Add(LastGapFulPalinEndSet(1000,10,3));
Memo.Lines.Add(LastGapFulPalinEndSet(1000,10,4));
Memo.Lines.Add(LastGapFulPalinEndSet(1000,10,5));
Memo.Lines.Add(LastGapFulPalinEndSet(1000,10,6));
Memo.Lines.Add(LastGapFulPalinEndSet(1000,10,7));
Memo.Lines.Add(LastGapFulPalinEndSet(1000,10,8));
Memo.Lines.Add(LastGapFulPalinEndSet(1000,10,9));
end;
 
 
 
</syntaxhighlight>
{{out}}
<pre>
First 20 palindromic gapful numbers
Ending in: 1
121 1001 1111 1221 1331
1441 1551 1661 1771 1881
1991 10901 11011 12221 13431
14641 15851 17171 18381 19591
 
Ending in: 2
242 2002 2112 2222 2332
2442 2552 2662 2772 2882
2992 20702 21912 22022 23232
24442 25652 26862 28182 29392
 
Ending in: 3
363 3003 3333 3663 3993
31713 33033 36663 300003 303303
306603 309903 312213 315513 318813
321123 324423 327723 330033 333333
 
Ending in: 4
484 4004 4224 4444 4664
4884 40304 42724 44044 46464
48884 400004 401104 402204 403304
404404 405504 406604 407704 408804
 
Ending in: 5
5005 5115 5225 5335 5445
5555 5665 5775 5885 5995
50105 51315 52525 53735 54945
55055 56265 57475 58685 59895
 
Ending in: 6
6006 6336 6666 6996 61116
64746 66066 69696 600006 603306
606606 609906 612216 615516 618816
621126 624426 627726 630036 633336
 
Ending in: 7
7007 7777 77077 700007 707707
710017 717717 720027 727727 730037
737737 740047 747747 750057 757757
760067 767767 770077 777777 780087
 
Ending in: 8
8008 8448 8888 80608 86768
88088 800008 802208 804408 806608
808808 821128 823328 825528 827728
829928 840048 842248 844448 846648
 
Ending in: 9
9009 9999 94149 99099 900009
909909 918819 927729 936639 945549
954459 963369 972279 981189 990099
999999 9459549 9508059 9557559 9606069
 
86th to 100th
Ending in: 1
165561 166661 167761 168861 169961
170071 171171 172271 173371 174471
175571 176671 177771 178871 179971
 
Ending in: 2
265562 266662 267762 268862 269962
270072 271172 272272 273372 274472
275572 276672 277772 278872 279972
 
Ending in: 3
30366303 30399303 30422403 30455403 30488403
30511503 30544503 30577503 30600603 30633603
30666603 30699603 30722703 30755703 30788703
 
Ending in: 4
4473744 4485844 4497944 4607064 4619164
4620264 4632364 4644464 4656564 4668664
4681864 4693964 4803084 4815184 4827284
 
Ending in: 5
565565 566665 567765 568865 569965
570075 571175 572275 573375 574475
575575 576675 577775 578875 579975
 
Ending in: 6
60399306 60422406 60455406 60488406 60511506
60544506 60577506 60600606 60633606 60666606
60699606 60722706 60755706 60788706 60811806
 
Ending in: 7
72299227 72322327 72399327 72422427 72499427
72522527 72599527 72622627 72699627 72722727
72799727 72822827 72899827 72922927 72999927
 
Ending in: 8
80611608 80622608 80633608 80644608 80655608
80666608 80677608 80688608 80699608 80800808
80811808 80822808 80833808 80844808 80855808
 
Ending in: 9
95311359 95400459 95499459 95588559 95677659
95766759 95855859 95944959 96033069 96122169
96211269 96300369 96399369 96488469 96577569
 
991st to 1000th:
Ending in: 1
17799771 17800871 17811871 17822871 17833871
17844871 17855871 17866871 17877871 17888871
 
Ending in: 2
27799772 27800872 27811872 27822872 27833872
27844872 27855872 27866872 27877872 27888872
 
Ending in: 3
3084004803 3084334803 3084664803 3084994803 3085225803
3085555803 3085885803 3086116803 3086446803 3086776803
 
Ending in: 4
482282284 482414284 482535284 482656284 482777284
482898284 482909284 483020384 483141384 483262384
 
Ending in: 5
57800875 57811875 57822875 57833875 57844875
57855875 57866875 57877875 57888875 57899875
 
Ending in: 6
6084004806 6084334806 6084664806 6084994806 6085225806
6085555806 6085885806 6086116806 6086446806 6086776806
 
Ending in: 7
7452992547 7453223547 7453993547 7454224547 7454994547
7455225547 7455995547 7456226547 7456996547 7457227547
 
Ending in: 8
8085995808 8086006808 8086116808 8086226808 8086336808
8086446808 8086556808 8086666808 8086776808 8086886808
 
Ending in: 9
9675005769 9675995769 9676886769 9677777769 9678668769
9679559769 9680440869 9681331869 9682222869 9683113869
 
Elapsed Time: 763.788 ms.
 
</pre>
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// Palindromic Gapful Numbers . Nigel Galloway: December 3rd., 2020
let rec fN g l=seq{match l with 3->yield! seq{for n in 0L..9L->g*100L+g+n*10L}
Line 1,141 ⟶ 1,817:
[1L..9L]|>Seq.iter(fun n->rcGf n|>Seq.skip 85|>Seq.take 15|>Seq.iter(printf "%d ");printfn "");printfn "#####"
[1L..9L]|>Seq.iter(fun n->rcGf n|>Seq.skip 990|>Seq.take 10|>Seq.iter(printf "%d ");printfn "");printfn "#####"
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,176 ⟶ 1,852:
</pre>
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: formatting fry io kernel lists lists.lazy locals math
math.functions math.ranges math.text.utils prettyprint sequences ;
IN: rosetta-code.palindromic-gapful-numbers
Line 1,228 ⟶ 1,904:
15 100 ! part 2
10 1000 ! part 3 (Optional)
[ show-palindromic-gapfuls ] 2tri@</langsyntaxhighlight>
{{out}}
<pre style="height:45ex">
Line 1,267 ⟶ 1,943:
=={{header|FreeBASIC}}==
Time-consuming brute-force solution with only a few speedups...
<langsyntaxhighlight lang="freebasic">function is_gapful( n as uinteger ) as boolean
if n<100 then return false
dim as string ns = str(n)
Line 1,325 ⟶ 2,001:
print_range(yays(), 1, 20)
print_range(yays(), 86, 100)
print_range(yays(), 991, 1000)</langsyntaxhighlight>
{{out}}
<pre>
Line 1,376 ⟶ 2,052:
=={{header|Fōrmulæ}}==
 
In [{{FormulaeEntry|page=https://wiki.formulae.org/?script=examples/Palindromic_gapful_numbers this] page you can see the solution of this task.}}
 
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text ([http://wiki.formulae.org/Editing_F%C5%8Drmul%C3%A6_expressions more info]). Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for transportation effects more than visualization and edition.
 
The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.
 
=={{header|Go}}==
Line 1,386 ⟶ 2,058:
 
To keep the Pascal entry company, I've extended the search to the first 10 million such numbers for each of the nine sets.
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,489 ⟶ 2,161:
fmt.Println()
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,573 ⟶ 2,245:
=={{header|Haskell}}==
===Brute Force===
<langsyntaxhighlight lang="haskell">import Control.Monad (guard)
 
palindromic :: Int -> Bool
Line 1,608 ⟶ 2,280:
putStrLn "\nLast 10 of first 1000 palindromic gapful numbers ending in:"
showSets (show . drop 990 . take 1000 . result)
putStrLn "\ndone."</langsyntaxhighlight>
 
===Optimized===
Here the approach is to generate a series of palindromes.
<langsyntaxhighlight lang="haskell">import Data.List (sort, unfoldr)
import Control.Monad (guard)
 
Line 1,646 ⟶ 2,318:
, ("Last 15 of first 100", drop 85 . take 100)
, ("Last 10 of first 1000", drop 990 . take 1000)
]</langsyntaxhighlight>
{{out}}
<pre>
Line 1,704 ⟶ 2,376:
</pre>
Part 2:
<syntaxhighlight lang="text">
palindromify=: [: , ((,~ |.@}.) ; (,~ |.))&>
gapful=: (0 = (|~ ({.,{:)&.(10&#.inv)))&>
Line 1,710 ⟶ 2,382:
task2_palindromes=: [: 10&#.&> [: palindromify task2_cartesian_products
task2_gapfuls=: [: /:~ [: ; [: (#~ gapful)@task2_palindromes&.> >:@i.
</syntaxhighlight>
</lang>
 
<pre>
Line 1,776 ⟶ 2,448:
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">
import java.util.ArrayList;
import java.util.HashMap;
Line 1,886 ⟶ 2,558:
 
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,925 ⟶ 2,597:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">import Base.iterate, Base.IteratorSize, Base.IteratorEltype
 
struct Palindrome x1::UInt8; x2::UInt8; outer::UInt8; end
Line 1,969 ⟶ 2,641:
 
testpal()
</langsyntaxhighlight>{{out}}
<pre style="height:100ex;overflow:scroll">
Last digit | Last 20 of 20 palindromic gapful numbers from 100
Line 2,062 ⟶ 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,067 ⟶ 2,803:
Forms palindromes using number<->string conversions.
 
<syntaxhighlight lang ="ruby">import strutils, typetraits # for number input
import times # for timing code execution
import unicode # for reversed
Line 2,121 ⟶ 2,857:
for digit in 1..9: echo(digit, " : ", palindromicgapfuls(digit, count, keep) )
 
echo (epochTime() - start)</langsyntaxhighlight>
 
System: I7-6700HQ, 3.5 GHz5GHz, Linux Kernel 5.9.10, GCC 10.2.0, Nim 1.4.0
Compil: $ nim c --cc:gcc --d:danger palindromicgapfuls.nim
Run as: $ ./palindromicgapfuls
Line 2,130 ⟶ 2,866:
Faster version performing number<->string conversions for palindromes.
 
<syntaxhighlight lang ="ruby">import strutils, typetraits # for number input
import times # for timing code execution
import unicode # for reversed
Line 2,182 ⟶ 2,918:
for digit in 1..9: echo(digit, " : ", palindromicgapfuls(digit, count, keep) )
 
echo (epochTime() - start)</langsyntaxhighlight>
 
System: I7-6700HQ, 3.5 GHz5GHz, Linux Kernel 5.9.10, GCC 10.2.0, Nim 1.4.0
Compil: $ nim c -d:danger -d:lto --passC:-march=native palindromicgapfuls.nim
Run as: $ ./palindromicgapfuls
Line 2,191 ⟶ 2,927:
Fastest: make palindromes directly numerically.
 
<syntaxhighlight lang ="ruby">import strutils, typetraits # for number inputtimes
 
import times # for timing code execution
import unicode # for reversed
proc make_palindrome(front_half: uint64, power: int): uint64 =
var (resultres, front_half) = (front_half, front_half)
if (power and 1) == 0: resultres = resultres div 10
while front_half > 0:
resultres = resultres * 10 + front_half mod 10
result += front_half mod 10
front_half = front_half div 10
resultres
 
proc palindromicgapfuls(digit, count, keep: int): seq[uint64] =
var (palcnt, digit) = (0, digit.uint64) # count of gapful palindromes
let to_skip = count - keep # count of unwanted values to skip
var gapfuls = newSeq[uint64]() # array of palindromic gapfuls
let nndd = uint64(digit * 11) # digit gapful divisor: 11, 22,...88, 99
var (power, base) = (1, 11u64)
while true:
if (power.inc; power and 1) == 0: base = base * 10
Line 2,215 ⟶ 2,948:
var this_lo = base * digit # starting half for this digit: 10.. to 90..
var next_lo = base * (digit + 1) # starting half for next digit: 20.. to 100..
whilefor this_lofront_half <in countup(this_lo, next_lo - 12, 10):
let basep = if (power and 1) == 1: base11 else: base
var palindrome = make_palindrome(this_lo.uint64front_half, power)
for _ in 0..9:
if palindrome mod nndd == 0: (palcnt.inc; if palcnt > to_skip: gapfuls.add(palindrome))
palindrome += basep.uint64
if gapfuls.len >= keep: return gapfuls[0..keep-1]
this_lo += 10
 
let start = epochTime()
Line 2,250 ⟶ 2,982:
for digit in 1..9: echo(digit, " : ", palindromicgapfuls(digit, count, keep) )
 
echo (epochTime() - start)</langsyntaxhighlight>
 
System: I7-6700HQ, 3.5 GHz5GHz, Linux Kernel 5.9.1014, GCC 10.2.0, Nim 1.4.02
Compil: $ nim c --cc:gcc --d:danger palindromicgapfuls.nim
Run as: $ ./palindromicgapfuls
Time: 8.308537244796753304139614105225 secs
{{out}}
<pre>First 20 palindromic gapful numbers ending with:
Line 2,333 ⟶ 3,065:
9 : 968787783387787869</pre>
 
<langsyntaxhighlight lang="pascal">program PalinGap;
{$IFDEF FPC}
{$MODE DELPHI}{$OPTIMIZATION ON,ALL}{$CODEALIGN proc=16}{$ALIGN 16}
Line 2,505 ⟶ 3,237:
OutPalinGap(10000000,10000000,dgt);
writeln;
end.</langsyntaxhighlight>{{out}}<pre style="height:35ex;overflow:scroll;font-size:87%">
palindromic gapful numbers from 1 to 20
1 : 121 1001 1111 1221 1331 1441 1551 1661 1771 1881 1991 10901 11011 12221 13431 14641 15851 17171 18381 19591
Line 2,574 ⟶ 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)-->
<lang Phix>function reverse_n(atom s)
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
atom e = 0
<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>
while s>0 do
<span style="color: #004080;">atom</span> <span style="color: #000000;">e</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
e = e*10 + remainder(s,10)
<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>
s = floor(s/10)
<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>
end while
<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>
return e
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
end function
<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>
constant mx = 1000,
data = {{1, 20, "%7d "}, {86, 100, "%8d "}, {991, 1000, "%10d "}}
 
include builtins\ordinal.e
 
procedure main()
sequence results = repeat(repeat({},9),mx)
for d=1 to 9 do -- (the start/end digit)
integer count = 0, pow = 1, fl = d*11
for nd=3 to 15 do -- (number of digits, usually quits early)
-- (obvs. 64-bit phix is fine with 19 digits, but 32-bit ain't)
bool odd = (remainder(nd,2)==1)
for s=d*pow to (d+1)*pow-1 do -- (eg 300 to 399)
integer e = reverse_n(s)
for m=0 to iff(odd?9:0) do -- (1 or 10 iterations)
atom p = e + iff(odd ? s*pow*100+m*pow*10
: s*pow*10)
if remainder(p,fl)==0 then -- gapful!
count += 1
results[count][d] = p
-- (see? goto /is/ sometimes useful :-))
if count==mx then #ilASM{jmp :outer} end if
end if
end for
end for
if odd then pow *= 10 end if
end for
if count<mx then ?9/0 end if -- oh dear...
#ilASM{::outer}
end for
<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>
for i=1 to length(data) do
<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>
{integer s, integer e, string fmt} = data[i]
<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)
printf(1,"%,d%s to %,d%s palindromic gapful numbers (> 100) ending with:\n", {s,ord(s),e,ord(e)})
-- (obvs. 64-bit phix is fine with 19 digits, but 32-bit ain't)</span>
for d=1 to 9 do
<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>
printf(1,"%d: ",d)
<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>
for j=s to e do
<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>
printf(1,fmt,results[j][d])
<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>
end for
<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>
printf(1,"\n")
<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>
end for
<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>
printf(1,"\n")
<span style="color: #000000;">count</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
end for
<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>
end procedure
<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>
main()</lang>
<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 (&gt; 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,668 ⟶ 3,454:
</pre>
 
=== Ludicrously fast to 110,000,000,000,000,000,000th ===
{{libheader|Phix/online}}
Astonishingly this is all done with standard precision numbers, &lt; 2<sup><small>53</small></sup>. You realise this is like ten times the ''square'' of the previous limits, and still far faster.<br>
You can run this online [http://phix.x10.mx/p2js/pgn.htm here].
I will credit [[Self_numbers#AppleScript]] and the comment by Nigel Galloway on the talk page for ideas that inspired me.
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>-- demo/rosetta/Palindromic_gapful_numbers.exw
<span style="color: #000080;font-style:italic;">--
--
-- demo\rosetta\Palindromic_gapful_numbers.exw
-- A palindrome such as 9459549 can be constructed/broken down into
-- ===========================================
-- 9000009
--
-- 400040
-- Astonishingly this is all done with standard precision numbers, &lt;2^53.
-- 50500
-- I will credit [[Self_numbers#AppleScript]] and comment by Nigel Galloway
-- 9000
-- on the talk page for ideas that inspired me.
--
--
-- Further, 9459549 rem 99 is the same as
-- A palindrome such as 9459549 can be constructed/broken down into
-- (the sum of rem 99 on all of those pieces) rem 99
-- 9000009
--
-- 400040
-- Some very simple initial experiments (for examples see talk page)
-- 50500
-- quickly showed there were patterns in 4000400 rem 99 and suchlike
-- 9000
-- that we could exploit, initially I was thinking of speed but in
--
-- the end accuracy on silly sized numbers turned out to be far more
-- Further, 9459549 rem 99 is the same as
-- important (dare I say useful?)
-- (the sum of rem 99 on all of those pieces) rem 99
--
--
-- Also, when filling a "hole", such as the final 9, we find
-- Finding eg 400040 rem 99 can also be simplified, it is of v course the
-- same as (400000 rem 99 + 40 rem 9459549%99) =rem 099, and further 40 rem 99
-- is the same as (4 rem 99[already known])*10 rem 9458549%99 =[smaller 89,nos].
--
-- 9457549%99 = 79,
-- Also, when filling a "hole", such as the final 9, we find
-- 9456549%99 = 69,
-- 9455549%99 = 59, v
-- 9454549%9450549 rem 99 = 49,9
-- 9453549%9451549 rem 99 = 3919,
-- 9452549% rem 99 = 29,
-- 9451549%9453549 rem 99 = 1939, and
-- 9450549%9454549 rem 99 = 949,
-- 9455549 rem 99 ^= 59,
-- 9456549 rem 99 = 69,
-- in this case only the '9' fits.
-- 9457549 rem 99 = 79,
--
-- 9458549 rem 99 = 89, and
-- But actually we can predict what will fit from the partial sum of
-- prior pieces rem 99, ie 9000009..50500, and the same9459549 canrem be99 said= 0,
-- when filling the 505-sized hole - what will "fit" depends not on ^
-- in this case only the '9' fits.
-- what the "outer" actually are, but what their sum rem 99 is, and
--
-- likewise for larger and larger holes.
-- But Ifactually we latercan findpredict ourselveswhat lookingwill atfit thefrom samethe sizepartial hole,sum withof
-- the sameprior outerpieces rem 99, ie 9000009..50500, and the same remcan 99 outmost requirement,be wesaid
-- when filling the 505-sized hole - what will "fit" depends not on
-- would know instantly how many things are going to fit.
-- what the "outer" actually are, but what their sum rem 99 is, and
-- True, keeping full lists as the holes got bigger would probably
-- likewise for larger and larger holes.
-- consume memory almost as fast as an SR-71, but a single count,
-- If we later find ourselves looking at the same size hole, with
-- albeit one keyed on 4 conditions, we can cope. It turns out that
-- the same outer rem and the same rem 99 outmost requirement, we
-- even by the 10^15th scan, we only hit 17,579 variations anyway.
-- would know instantly how many things are going to fit.
-- As we stumble across larger and larger holes, what we learn can
-- True, keeping full lists as the holes got bigger would probably
-- be used to skip more and more similar, such that finding the
-- tenconsume millionth item ismemory almost as fast as thean first millionthSR-71, asbut a single count,
-- albeit one keyed on 4 conditions, we can cope. It turns out that
-- opposed to the times 10 that you'd normally expect.
-- even by the 10^15th scan, we only hit 17,579 variations anyway.
--
-- Note thatAs if Iwe stumble across alarger holeand thatlarger willholes, fitwhat morewe thanlearn I'mcan
-- preparedbe used to fully skip, Imore startand goingmore throughsimilar, thingssuch one-by-one,that finding the
-- ten millionth item is almost as fast as the first millionth, as
-- but that's ok because many smaller but still quite big holes
-- opposed to the times 10 that you'd normally expect.
-- will probably be skipped.
--
-- 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,
sequence cache = repeat(0,9)
-- but that's ok because many smaller but still quite big holes
 
-- will probably be skipped.
function rmdr(integer digit, gap, pow, n)
--</span>
--
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
-- 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),
<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>
-- pow is trailing zeros (0,1,2,.. for eg 101,1010,10100),
<span style="color: #000080;font-style:italic;">-- ie <nowiki>{{</nowiki>{1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1<nowiki>}}</nowiki>,
-- n is 1..9 for 11..99
-- <nowiki>{{</nowiki>2}, {2}, {2}, {2}, {2}, {2}, {2}, {2}, {2<nowiki>}}</nowiki>,
-- eg rmdr(4,3,2,1) yields remainder(4000400,11),
-- <nowiki>{{</nowiki>3}, {3}, {3}, {3}, {3}, {3}, {3}, {3}, {3<nowiki>}}</nowiki>,
-- except without using division - which becomes
-- <nowiki>{{</nowiki>4}, {4}, {4}, {4}, {4}, {4}, {4}, {4}, {4<nowiki>}}</nowiki>,
-- critical for accuracy with very large numbers.
-- <nowiki>{{</nowiki>5}, {5}, {5}, {5}, {5}, {5}, {5}, {5}, {5<nowiki>}}</nowiki>,
-- (more precisely it does not use division on
-- <nowiki>{{</nowiki>6}, {6}, {6}, {6}, {6}, {6}, {6}, {6}, {6<nowiki>}}</nowiki>,
-- anything over 10^15, and usually far less,
-- <nowiki>{{</nowiki>7}, {7}, {7}, {7}, {7}, {7}, {7}, {7}, {7<nowiki>}}</nowiki>,
-- as opposed to 10^34 final answer suggests.)
-- <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>}
if digit=0 then return 0 end if
-- aka 1 rem 11 .. 1 rem 99 are all 1,
if cache[n] = 0 then cache[n] = repeat({},9) end if
-- .. 9 rem 11 .. 9 rem 99 are all 9.
sequence cnd = cache[n][digit]
-- each gets extended with 10 rem 11 .. 10 rem 99,
if cnd={} then
-- 100 rem 11 .. 100 rem 99,
integer d = digit, k,
-- nn = n*11 ... .. 900 rem 99, etc.
-- (not really worth trying to take advantage of any cycles
for g=1 to 10 do -- (8 is actually enough)
-- that might appear in such a relatively small table, as
sequence gp = {}
-- it will be at most (on 64-bit) 9 * 9 atom* dp = d42.)
--</span>
for p=1 to 10 do -- (7 actually enough)
<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>
integer gpr = remainder(dp,nn)
<span style="color: #000080;font-style:italic;">--
if length(gp)=1 and gpr=gp[1] then exit end if
-- digit is the outer 0..9 (obvs 0 always yields 0),
k = find(gpr,gp)
-- gap is zeroes between (-1,0,1,2,.. for eg 1,11,101,1001),
if k then gp &= k exit end if
-- pow is trailing zeros (0,1,2,.. for eg 101,1010,10100),
gp &= gpr
-- n is 1..9 for 11..99
dp *= 10 -- eg 1, 10, 100, 1000, or
-- eg rmdrn(4,3,2,1) yields remainder(4000400,11), but
-- 11, 110, 1100, and/or
-- that === remainder(remainder(4000000,11)+
-- 101, 1010, 10100, etc
-- end for remainder( 400,11),11), and
-- if k = remainder(4*10^(m-1),11) [already known] then
k = find(gp,cnd)
-- remainder(4*10^m,11) === if remainder(k*10,11), thenso
-- we only need to keep a small table for each [digit][n].
if k>1 then cnd &= k end if
-- Thus we avoid maths on 10^42-ish numbers/needing gmp.
exit
--</span>
end if
<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>
cnd &= {gp}
<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>
if g>1 then d-=digit end if
<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>
d = d*10+digit -- eg 1, 11, 101, 1001, etc
<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>
end for
<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>
cache[n][digit] = cnd
<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>
end if
<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>
integer l = length(cnd)
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
if l=1 or gap<0 then
<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>
gap = 1
<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>
else
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
gap += 2
while gap>=l do gap -= l-cnd[$] end while
<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>
end if
cnd = cnd[gap]
<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>
l = length(cnd)
<span style="color: #000080;font-style:italic;">--
if l=1 then
-- pals: results (passing it up grants it automatic pass-by-reference status, which may help speedwise)
pow = 1
-- lhs: eg "945" of a potential 9459549 result
else
-- palcount, to_skip, count: self explanatory (aka got/ignore/target)
pow += 1
-- l: length of inner to be filled in
while pow>=l do pow -= l-cnd[$] end while
-- r: remainder of outer, eg remainder(9400049,11), built from rmdrn()
end if
-- p: left shift (should in fact always equal length(lhs), I think)
return cnd[pow]
-- dd: outermost 1..9 (despite the name, it's a single digit)
end function
--</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>
integer skipd = new_dict()
<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>
function palindromicgapfuls(sequence pals, string lhs, atom palcount, to_skip, count, integer l, r, p, dd)
<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>
-- pals: results (passing it up grants it automatic pass-by-reference status, which may help speedwise)
<span style="color: #008080;">else</span>
-- lhs: eg "945" of a potential 9459549 result
<span style="color: #000000;">skip</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
-- palcount, to_skip, count: self explanatory (aka got/ignore/target)
<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>
-- l: length of inner to be filled in
<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>
-- r: remainder of outer, eg remainder(9400049,11), built from rmdr()
<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>
-- p: left shift (should in fact always equal length(lhs), I think)
<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>
-- dd: outermost 1..9 (despite the name, it's a single digit)
<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>
sequence key = {l,r,p,dd}
<span style="color: #000000;">skip</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
integer node = getd_index(key,skipd)
<span style="color: #008080;">else</span>
atom skip = iff(node==null?count:getd_by_index(node,skipd))
<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>
if node!=null and (palcount+skip)<to_skip then
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
palcount += skip
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
else
<span style="color: #008080;">else</span>
skip = 0
<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>
for d=0 to 9 do
<span style="color: #000000;">skip</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">skipn</span>
integer r2 = remainder(r+rmdr(d,l-2,p,dd),dd*11)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if l<=2 then
<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>
if r2=0 then
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
palcount += 1
<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>
if palcount<=to_skip then
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
skip += 1
<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>
else
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
pals = append(pals,lhs&repeat(d+'0',l)&reverse(lhs))
end if
<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>
end if
<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>
else
<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>
{pals,palcount,atom skipn} = palindromicgapfuls(pals, lhs&(d+'0'), palcount, to_skip, count, l-2, r2, p+1, dd)
<span style="color: #004080;">sequence</span> <span style="color: #000000;">pals</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
skip += skipn
<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>
end if
<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>
if palcount==count then exit end if
<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>
end for
<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>
if palcount<to_skip then setd(key,skip,skipd) end if
<span style="color: #000000;">l</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
return {pals,palcount,skip}
<span style="color: #008080;">return</span> <span style="color: #000000;">pals</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
 
function collect(integer digit, atom count, keep)
<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>
atom to_skip = count - keep,
<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>
palcount = 0, l = 3
<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>
sequence pals = {}
<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>
string lhs = ""&(digit+'0') -- ie "1" or "2" .. or "9"
<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>
while palcount < count do
<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>
integer r = rmdr(digit,l-2,0,digit)
<span style="color: #000080;font-style:italic;">-- {1_000_000_000_000_000,1,2,4}, -- (matches AppleScript)</span>
{pals,palcount} = palindromicgapfuls(pals,lhs,palcount,to_skip,count,l-2,r,1,digit)
<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
l += 1
-- (any further and you'd need mpfr just to hold counts)</span>
end while
return pals
<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>
end function
<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>
constant tests = {{20,20,1},{100,15,1},{1000,10,1},{10_000,5,1},
<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>
{100_000,1,1},{1_000_000,1,1},{10_000_000,1,1},
<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>
{100_000_000,1,9},{1000_000_000,1,9},{10_000_000_000,1,9},
<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>
{100_000_000_000,1,9},{1000_000_000_000,1,9},
<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>
{10_000_000_000_000,1,9},{100_000_000_000_000,1,9},
<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>
{1000_000_000_000_000,1,9}}
<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>
-- (any further and you'd need mpfr just to hold counts)
<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>
atom t0 = time(), count, keep, start
<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>
for i=1 to length(tests) do
<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>
{count, keep, start} = tests[i]
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
atom from = count-keep+1
<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>
string r = iff(count==keep?sprintf("First %d",{count}):
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
iff(keep>1?sprintf("Last %d of first %,d",{keep,count})
<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>
:sprintf("%,dth",{count}))),
<!--</syntaxhighlight>-->
s = iff(keep=1?"":"s")
printf(1,"%s palindromic gapful number%s ending with:\n", {r,s})
sequence tags = tagset(9,start),
res = apply(true,collect,{tags,count,keep})
string fmt = sprintf("%%%ds",max(apply(join(res,{}),length)))
for j=1 to length(res) do
printf(1,"%d: %s\n",{tags[j],join(apply(true,sprintf,{{fmt},res[j]})," ")})
end for
puts(1,"\n")
end for
printf(1,"Completed in %s\n",elapsed(time()-t0))</lang>
{{out}}
<pre style="font-size: 11px10px">
First 20 palindromic gapful numbers ending with:
1: 121 1001 1111 1221 1331 1441 1551 1661 1771 1881 1991 10901 11011 12221 13431 14641 15851 17171 18381 19591
Line 2,974 ⟶ 3,750:
Completed in 0.7s
</pre>
On 64bit you'll also get (in 0.8s)
I would agree that the last entry does not feel very convincing. Depending on how much
<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. :-)
<langsyntaxhighlight Phixlang="phix">--count = 100000
while count do
count -= 1
?collect(9,count,1)
end while</langsyntaxhighlight>
 
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<langsyntaxhighlight lang="prolog">init_palindrome(Digit, p(10, Next, 0)):-
Next is Digit * 10 - 1.
 
Line 3,091 ⟶ 3,872:
print_numbers(1, 20, Numbers),
print_numbers(86, 100, Numbers),
print_numbers(991, 1000, Numbers).</langsyntaxhighlight>
 
{{out}}
Line 3,141 ⟶ 3,922:
Note: Although this uses the idea of generating palindromes from the Geeks4geeks reference mentioned in the Factor entry, none of their code was used.
 
<langsyntaxhighlight lang="python">from itertools import count
from pprint import pformat
import re
Line 3,173 ⟶ 3,954:
b = {k:v[-last:] for k, v in bin.items()}
txt = pformat(b, width=220)
print('', re.sub(r"[{},\[\]]", '', txt))</langsyntaxhighlight>
 
{{out}}
Line 3,211 ⟶ 3,992:
 
===Functional===
<langsyntaxhighlight lang="python">'''Palindromic gapful numbers'''
 
from itertools import chain, count, islice, tee
Line 3,412 ⟶ 4,193:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>First 20 samples of gapful palindromes (> 100) by last digit:
Line 3,451 ⟶ 4,232:
{{works with|Rakudo|2019.07.1}}
 
<syntaxhighlight lang="raku" perl6line>constant @digits = '0','1','2','3','4','5','6','7','8','9';
 
# Infinite lazy iterator to generate palindromic "gap" numbers
Line 3,463 ⟶ 4,244:
my @gappal = (1..9).map: -> \digit {
my \divisor = digit + 10 * digit;
@npal.hyper.map: -> \this { next unless (my \test = digit ~ this ~ digit) %% divisor; test }
}
 
Line 3,477 ⟶ 4,258:
put "$_: " ~ @gappal[$_-1][|$range].fmt("%{$fmt}s") for 1..9;
say round( now - $now, .001 ), " seconds";
}</langsyntaxhighlight>
{{out}}
<pre style="font-size:80%;">(Required) First 20 gapful palindromes:
Line 3,540 ⟶ 4,321:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program computes and displays palindromic gapful numbers, it also can show those */
/*─────────────────────── palindromic gapful numbers listed by their last decimal digit.*/
numeric digits 20 /*ensure enough decimal digits gapfuls.*/
Line 3,570 ⟶ 4,351:
do k=1 for 9; say k':' strip( subword($.k, 1 + n - z) )
end /*k*/; say
return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the internal default inputs:}}
 
Line 3,611 ⟶ 4,392:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 3,651 ⟶ 4,432:
 
see "done..." + nl
</syntaxhighlight>
</lang>
Output:
<pre>
Line 3,669 ⟶ 4,450:
{{trans|Crystal}}
===Brute force and slow===
<langsyntaxhighlight lang="ruby">def palindromesgapful(digit, pow)
r1 = digit * (10**pow + 1)
r2 = 10**pow * (digit + 1)
Line 3,696 ⟶ 4,477:
count = 1000
puts "\nLast 10 of first 1000 palindromic gapful numbers ending in:"
(1..9).each { |digit| print "#{digit} : #{digitscount(digit, count).last(10)}\n" }</langsyntaxhighlight>
 
===Orders of Magnitude Faster: Direct Generation of Numbers===
Line 3,704 ⟶ 4,485:
Run as: $ ruby palindromicgapfuls.rb
Optimized version, the ultimate fastest: Ruby 2.7.1 - 112.5 secs
<langsyntaxhighlight lang="ruby">def palindromicgapfuls(digit, count)
gapfuls = [] # array of palindromic gapfuls
nn = digit * 11 # digit gapful divisor: 11, 22,...88, 99
Line 3,760 ⟶ 4,541:
1.upto(9) { |digit| puts "#{digit} : #{palindromicgapfuls(digit, count).last(keep)}" }
 
puts (Time.now - start)</langsyntaxhighlight>
 
Compact version: Ruby-2.7.1 - 113.0 secs
<langsyntaxhighlight lang="ruby">def palindromicgapfuls(digit, count)
gapfuls = [] # array of palindromic gapfuls
nn = digit * 11 # digit gapful divisor: 11, 22,...88, 99
Line 3,812 ⟶ 4,593:
1.upto(9) { |digit| puts "#{digit} : #{palindromicgapfuls(digit, count).last(keep)}" }
 
puts (Time.now - start)</langsyntaxhighlight>
 
Object Oriented implementation: Ruby 2.7.1 - 113.0 secs
<langsyntaxhighlight lang="ruby">class PalindromicGapfuls
include Enumerable
 
Line 3,877 ⟶ 4,658:
1.upto(9) { |digit| puts "#{digit} : #{PalindromicGapfuls.new(digit).first(count).last(keep)}" }
 
puts (Time.now - start)</langsyntaxhighlight>
 
Versions optimized for minimal memory use: Ruby 2.7.1 - 110.0 secs
<langsyntaxhighlight lang="ruby">def palindromicgapfuls(digit, count, keep)
palcnt = 0 # count of gapful palindromes
to_skip = count - keep # count of unwanted values to skip
Line 3,938 ⟶ 4,719:
1.upto(9) { |digit| puts "#{digit} : #{palindromicgapfuls(digit, count, keep)}" }
 
puts (Time.now - start)</langsyntaxhighlight>
 
Compact version optimized for minimal memory use: Ruby 2.7.1 - 111.5 secs
<langsyntaxhighlight lang="ruby">def palindromicgapfuls(digit, count, keep)
palcnt = 0 # count of gapful palindromes
to_skip = count - keep # count of unwanted values to skip
Line 3,992 ⟶ 4,773:
1.upto(9) { |digit| puts "#{digit} : #{palindromicgapfuls(digit, count, keep)}" }
 
puts (Time.now - start)</langsyntaxhighlight>
 
OOP version optimized for minimal memory use: Ruby 2.7.1 - 116.0 secs
<BR>It creates an output method that skips the unwanted values and only keeps/stores the desired ones.
<langsyntaxhighlight lang="ruby">class PalindromicGapfuls
include Enumerable
 
Line 4,068 ⟶ 4,849:
1.upto(9) { |digit| puts "#{digit} : #{PalindromicGapfuls.new(digit).keep_from(count, keep)}" }
 
puts (Time.now - start)</langsyntaxhighlight>
{{out}}
<pre>First 20 palindromic gapful numbers 100 ending with:
Line 4,139 ⟶ 4,920:
{{trans|F#}}
0.186s on Intel(R) Core(TM) i5-1035G1 CPU @ 1.00GHz
<langsyntaxhighlight lang="ruby">
class PalNo
def initialize(set)
Line 4,157 ⟶ 4,938:
for n in 1..9 do palNo=PalNo.new(n); g=1; palNo.each{|n| print "#{n} " if g>85; g+=1; break unless g<101}; puts "" end; puts "####"
for n in 1..9 do palNo=PalNo.new(n); g=1; palNo.each{|n| print "#{n} " if g>990; g+=1; break unless g<1001}; puts "" end; puts "####"
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 4,197 ⟶ 4,978:
Run as: $ ruby fsharp2ruby.rb
Best Time: 437.566515299 secs
<langsyntaxhighlight lang="ruby">class PalNo
def initialize(digit)
@digit, @l, @dd = digit, 3, 11*digit
Line 4,225 ⟶ 5,006:
(1..9).each { |digit| PalNo.new(digit).show(10_000_000, 1) }; puts "####"
 
puts (Time.now - start)</langsyntaxhighlight>
{{out}}
<pre>
Line 4,296 ⟶ 5,077:
This version uses number->string then string->number conversions to create palindromes.
 
<langsyntaxhighlight lang="rust">fn palindromicgapfuls(digit: u64, count: u64, keep: usize) -> Vec<u64> {
let mut palcnt = 0u64; // count of gapful palindromes
let to_skip = count - keep as u64; // count of unwanted values to skip
Line 4,350 ⟶ 5,131:
println!("{:?}", t.elapsed())
}</langsyntaxhighlight>
System: I7-6700HQ, 3.5 GHz, Linux Kernel 5.9.10, Rust 1.48
Compil: $ rustc -C opt-level=3 -C target-cpu=native -C codegen-units=1 -C lto palindromicgapfuls.rs
Line 4,360 ⟶ 5,141:
About 2.5x faster.
 
<langsyntaxhighlight lang="rust">fn palindromicgapfuls(digit: u64, count: u64, keep: usize) -> Vec<u64> {
let mut palcnt = 0u64; // count of gapful palindromes
let to_skip = count - keep as u64; // count of unwanted values to skip
Line 4,420 ⟶ 5,201:
println!("{:?}", t.elapsed())
}</langsyntaxhighlight>
System: I7-6700HQ, 3.5 GHz, Linux Kernel 5.9.10, Rust 1.48
Compil: $ rustc -C opt-level=3 -C target-cpu=native -C codegen-units=1 -C lto palindromicgapfuls.rs
Line 4,494 ⟶ 5,275:
=={{header|Sidef}}==
Inspired from the C++ and Raku entries.
<langsyntaxhighlight lang="ruby">class PalindromeGenerator (digit, base=10) {
 
has power = base
Line 4,528 ⟶ 5,309:
say ("#{d}: ", arr.map{ "%*s" % (w, _) }.join(' '))
}
})</langsyntaxhighlight>
{{out}}
<pre style="font-size:83%">
Line 4,580 ⟶ 5,361:
{{libheader|Wren-fmt}}
Search limited to the first 100,000 palindromic gapful numbers as, beyond that, the numbers become too large (>= 2 ^ 53) to be accurately represented by Wren's Num type.
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Conv, Fmt
 
var reverse = Fn.new { |s|
Line 4,639 ⟶ 5,420:
}
System.print()
}</langsyntaxhighlight>
 
{{out}}
Line 4,697 ⟶ 5,478:
8: 80869644696808 80869655696808 80869666696808 80869677696808 80869688696808
9: 96877711777869 96877800877869 96877899877869 96877988977869 96878077087869
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang "XPL0">func Palindromic(N); \Return 'true' if N is palindromic
int N, I, J, S(10);
[I:= 0;
while N > 0 do
[N:= N/10;
S(I):= rem(0);
I:= I+1;
];
J:= 0; I:= I-1;
while J < I do
[if S(J) # S(I) then return false;
J:= J+1; I:= I-1;
];
return true;
];
 
int Lo, Hi, Task, Mul, N, Cnt, Prod;
[Lo:= 0; Hi:= 20;
for Task:= 1 to 2 do
[Mul:= 11;
repeat N:= 1; Cnt:= 0;
loop [Prod:= N * Mul;
if Prod >= 100 then
if rem(Prod/10) = Mul/10 then
if Palindromic(Prod) then
[Cnt:= Cnt+1;
if Cnt >= Lo then
[IntOut(0, Prod); ChOut(0, ^ )];
if Cnt >= Hi then quit;
];
N:= N+1;
];
CrLf(0);
Mul:= Mul + 11;
until Mul > 99;
CrLf(0);
Lo:= 86; Hi:= 100;
];
]</syntaxhighlight>
{{out}}
<pre>
121 1001 1111 1221 1331 1441 1551 1661 1771 1881 1991 10901 11011 12221 13431 14641 15851 17171 18381 19591
242 2002 2112 2222 2332 2442 2552 2662 2772 2882 2992 20702 21912 22022 23232 24442 25652 26862 28182 29392
363 3003 3333 3663 3993 31713 33033 36663 300003 303303 306603 309903 312213 315513 318813 321123 324423 327723 330033 333333
484 4004 4224 4444 4664 4884 40304 42724 44044 46464 48884 400004 401104 402204 403304 404404 405504 406604 407704 408804
5005 5115 5225 5335 5445 5555 5665 5775 5885 5995 50105 51315 52525 53735 54945 55055 56265 57475 58685 59895
6006 6336 6666 6996 61116 64746 66066 69696 600006 603306 606606 609906 612216 615516 618816 621126 624426 627726 630036 633336
7007 7777 77077 700007 707707 710017 717717 720027 727727 730037 737737 740047 747747 750057 757757 760067 767767 770077 777777 780087
8008 8448 8888 80608 86768 88088 800008 802208 804408 806608 808808 821128 823328 825528 827728 829928 840048 842248 844448 846648
9009 9999 94149 99099 900009 909909 918819 927729 936639 945549 954459 963369 972279 981189 990099 999999 9459549 9508059 9557559 9606069
 
165561 166661 167761 168861 169961 170071 171171 172271 173371 174471 175571 176671 177771 178871 179971
265562 266662 267762 268862 269962 270072 271172 272272 273372 274472 275572 276672 277772 278872 279972
30366303 30399303 30422403 30455403 30488403 30511503 30544503 30577503 30600603 30633603 30666603 30699603 30722703 30755703 30788703
4473744 4485844 4497944 4607064 4619164 4620264 4632364 4644464 4656564 4668664 4681864 4693964 4803084 4815184 4827284
565565 566665 567765 568865 569965 570075 571175 572275 573375 574475 575575 576675 577775 578875 579975
60399306 60422406 60455406 60488406 60511506 60544506 60577506 60600606 60633606 60666606 60699606 60722706 60755706 60788706 60811806
72299227 72322327 72399327 72422427 72499427 72522527 72599527 72622627 72699627 72722727 72799727 72822827 72899827 72922927 72999927
80611608 80622608 80633608 80644608 80655608 80666608 80677608 80688608 80699608 80800808 80811808 80822808 80833808 80844808 80855808
95311359 95400459 95499459 95588559 95677659 95766759 95855859 95944959 96033069 96122169 96211269 96300369 96399369 96488469 96577569
 
</pre>
 
=={{header|zkl}}==
Using ideas from the Factor entry
<langsyntaxhighlight lang="zkl">// 10,True --> 101,111,121,131,141,151,161,171,181,191,202, ..
// 10,False --> 1001,1111,1221,1331,1441,1551,1661,1771,1881,..
fcn createPalindromeW(start,oddLength){ //--> iterator
Line 4,718 ⟶ 5,563:
if(p%10==endsWith and (p%div)==0) p else Void.Skip
})
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">println("First 20 palindromic gapful numbers:");
[1..9].apply(palindromicGapfulW).apply("walk",20) : pgpp(_);
 
Line 4,730 ⟶ 5,575:
m,fmt := (0).max(table.apply((0).max)).numDigits, "%%%dd ".fmt(m).fmt;
foreach d,row in ([1..].zip(table)){ println(d,": ",row.pump(String,fmt)) }
}</langsyntaxhighlight>
{{out}}
<pre style="font-size:83%">
Line 4,766 ⟶ 5,611:
9: 9675005769 9675995769 9676886769 9677777769 9678668769 9679559769 9680440869 9681331869 9682222869 9683113869
</pre>
<langsyntaxhighlight lang="zkl">/* We can also thread the whole mess, which for this case, is a 3.75 speed up
* (3 min to 48sec) with 8 cores (Intel 4/4).
*/
Line 4,775 ⟶ 5,620:
.apply("noop") // wait for threads to finish
: pgpp(_);
}</langsyntaxhighlight>
{{out}}
<pre style="font-size:83%">
9,479

edits