Palindromic gapful numbers: Difference between revisions
Undo revision 330973 by Nig (talk)
(→{{header|AppleScript}}: →Translation of Phix "Ludicrously fast …": 6.5-fold speed increase through dumping NSMutableArray altogether. Several minor tweaks and comment changes.) |
|||
Line 178:
===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
<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
use framework "Foundation" -- For its NSMutableDictionary class and methods.
property outerX11 : missing value▼
property countLimit : missing value▼
property outerDigit_x11 : missing value
property to_skip : missing value
property palcount : missing value
property skipd : {{}, {}, {}, {}, {}, {}, {}, {}, {}}
property
property
-- Work out what the remainder
--
--
on rmdr(
--
--
set
if (gap > -1) then set
return (
end rmdr
-- Recursively
--
on palindromicGapfuls(lhs, gap, remainder)
-- lhs: eg {9, 4, 5} of a potential
-- gap: length of inner to be filled in
-- remainder: remainder
set shift to (count lhs) -- left shift
--
-- each containing 99 smaller dictionaries with two-element keys. The values of the original
-- 'outerDigit' and 'remainder' key elements are used to index this structure. Since 'outerDigit'
--
--
set skip to (item
if (skip is missing value) then -- Key not in dictionary. Set 'skip' high for the 'if' test below.
end repeat▼
else
set end of item shift of my skipdOuter to {}▼
end if▼
if (palcount + skip
end repeat▼
set skip to 0
set template to lhs & missing value & reverse of lhs -- Palindrome template list.
set mid to shift + 1 -- Index of its middle position.
set nextGap to gap - 2
repeat with d from 0 to 9
set nextRem to (remainder + rmdr(d, nextGap, shift)) mod
if (gap > 2) then
set skip to skip + palindromicGapfuls(lhs & d, nextGap, nextRem)
else if (nextRem is 0) then
set palcount to palcount + 1
if (palcount > to_skip) then
-- else if (gap is 0) then
-- set item mid of template to ""
-- else
-- set template to d
end if
set end of my output to template as text
else
set skip to skip + 1
end if
end if
if (palcount =
end repeat
if (palcount < to_skip) then
else
set palcount to palcount + skip
Line 255 ⟶ 264:
end palindromicGapfuls
--
on collect(
-- Initialise these script object properties for the current search.
set
set
set
set palcount to 0
set
set
--
set
set
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ""
repeat until (palcount =
set remainder to rmdr(
palindromicGapfuls(lhs, gap, remainder)
set gap to gap + 1
Line 277 ⟶ 287:
set AppleScript's text item delimiters to astid
return
end collect
on initialiseSkipd()
repeat with thisList in my skipd
repeat 99 times
set end of thisList to current application's class "NSMutableDictionary"'s new()
▲ end repeat
▲ end repeat
end initialiseSkipd
end script
tell theWorks to initialiseSkipd()
return theWorks
end getWorks
on doTask()
Line 301 ⟶ 310:
{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
set output to {}
Line 308 ⟶ 317:
set AppleScript's text item delimiters to " "
repeat with i from 1 to (count tests)
set {
▲ set h to "First " & countLimit
else if (keep > 1) then
set
▲ else
▲ set h to ordinalise(countLimit)
▲ end if
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
end if
if (firstEndDigit is not lastEndDigit) then set t to "digits " & firstEndDigit & " to " & t
set end of output to theWorks's (collect(outer, countLimit, keep)) as text▼
set end of output to ""
▲ set end of
repeat with digit from firstEndDigit to lastEndDigit
end repeat
end repeat
set AppleScript's text item delimiters to linefeed
set output to (rest of output) as text
set AppleScript's text item delimiters to astid
Line 339 ⟶ 346:
{{output}}
<pre style="font-size:80%">First 20 palindromic gapful numbers > 100 ending with digits
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
Line 350 ⟶ 357:
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
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
Line 361 ⟶ 368:
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
17799771 17800871 17811871 17822871 17833871 17844871 17855871 17866871 17877871 17888871
27799772 27800872 27811872 27822872 27833872 27844872 27855872 27866872 27877872 27888872
Line 372 ⟶ 379:
9675005769 9675995769 9676886769 9677777769 9678668769 9679559769 9680440869 9681331869 9682222869 9683113869
Last 5 of first 10000 palindromic gapful numbers > 100 ending with digits
1787557871 1787667871 1787777871 1787887871 1787997871
2787557872 2787667872 2787777872 2787887872 2787997872
Line 383 ⟶ 390:
968697796869 968706607869 968715517869 968724427869 968733337869
100000th palindromic gapful
178788887871
278788887872
Line 394 ⟶ 401:
96878077087869
1000000th palindromic gapful
17878799787871
27878799787872
Line 405 ⟶ 412:
9687870990787869
10000000th palindromic gapful
1787878888787871
2787878888787872
Line 437 ⟶ 444:
96878787878786133168787878787869
1.0E+15th palindromic gapful
27878787878787888878787878787872
3087878787878783113878787878787803
|