Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,85 @@
on doTask()
set part1 to {"First 20 palindromic gapful numbers > 100 ending with each digit from 1 to 9:"}
set part2 to {"86th to 100th such:"}
set part3 to {"991st to 1000th:"}
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to " "
repeat with endDigit from 1 to 9
set {collector1, collector2, collector3} to {{}, {}, {}}
set outerNumber to endDigit * 11 -- Number formed from the palindromes' first and last digits.
set oddDigitCount to true -- Starting with palindromes in the hundreds.
set baseHi to endDigit * 10 -- Number formed from just the "high end" digits, initially endDigit and a middle 0.
set hi to baseHi
set carryCheck to hi + 10 -- Number reached when incrementing the "high end" number changes its first digit.
set inc to 10 -- Incrementor for the middle digit(s) of the palindromes themselves.
set counter to 0
set maxNeeded to 1000
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
else
set temp to hi
end if
repeat until (temp is 0)
set pal to pal * 10 + temp mod 10
set temp to temp div 10
end repeat
-- Check the result and the following 9 palindromes (derived by incrementing the middle digit(s))
-- and store as text any which are both gapful and the ones required.
repeat 10 times
if (pal mod outerNumber is 0) then
set counter to counter + 1
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 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 {collector1 as text, collector2 as text, collector3 as text}
end repeat
set AppleScript's text item delimiters to linefeed
set output to {part1, "", part2, "", part3} as text
set AppleScript's text item delimiters to astid
return output
end doTask
on intText(n)
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()

View file

@ -0,0 +1,157 @@
-- 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()