Penholodigital squares: Difference between revisions

Created Nim solution.
(Added AppleScript.)
(Created Nim solution.)
Line 936:
4b802235a² = 1234978cibd6gfhea5 ii844bia2² = ihg8f71c6da59be324 20734.916185 seconds (169 allocations:
2.163 MiB)
</pre>
 
=={{header|Nim}}==
{{trans|Wren}}
Translation with several modifications to make things easier in Nim.
<syntaxhighlight lang="Nim">import std/[algorithm, math, strformat, sugar]
 
const
Primes = [1: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
Digits = "0123456789ABCDEF"
 
func digits(n, base: Positive): seq[int] =
## Return the digits of "n" in given base (least significant
## digits first).
var n = n.Natural
while n != 0:
result.add n mod base
n = n div base
 
func parseInt(d: openArray[char]; base: Positive): int =
## Convert a sequence of characters in given base to an integer.
for c in d:
result = result * base + (if c <= '9': ord(c) - ord('0') else: ord(c) - ord('A') + 10)
 
func toBase(n: Natural; base: Positive): string =
## Return the string representation of "n" in given base.
let d = n.digits(base)
for i in countdown(d.high, 0):
result.add Digits[d[i]]
 
func lastPrimeFactor(n: Positive): int =
## Return the last prime factor of "n".
var n = n
if (n and 1) == 0:
result = 2
while true:
n = n shr 1
if (n and 1) != 0: break
var d = 3
while d * d <= n:
if n mod d == 0:
result = d
while true:
n = n div d
if n mod d != 0: break
inc d, 2
if n > result: result = n
 
for b in 9..16:
let master = Primes[1..<b]
var phd: seq[int]
let digits = Digits[1..<b]
let min = sqrt(digits.parseInt(b).toFloat).ceil.toInt
let max = sqrt(digits.reversed.parseInt(b).toFloat).ceil.toInt
let divider = lastPrimeFactor(b - 1)
 
# Build the list of square roots of pendholodigital squares.
for i in min..max:
if i mod divider != 0: continue
let sq = i * i
let digs = sq.digits(b)
if 0 in digs: continue
var key = collect(for dig in digs: Primes[dig])
if sorted(key) == master: phd.add i
 
echo &"There is a total of {phd.len} penholodigital squares in base {b}:"
if b > 13: phd = @[phd[0], phd[^1]]
for i in 0..phd.high:
stdout.write &" {phd[i].toBase(b)}² = {(phd[i]^2).toBase(b)}"
if i mod 3 == 2: echo()
if phd.len mod 3 != 0: echo()
echo()
</syntaxhighlight>
 
{{out}}
<pre>There is a total of 10 penholodigital squares in base 9:
3825² = 16328547 3847² = 16523874 4617² = 23875614
4761² = 25487631 6561² = 47865231 6574² = 48162537
6844² = 53184267 7285² = 58624317 7821² = 68573241
8554² = 82314657
 
There is a total of 30 penholodigital squares in base 10:
11826² = 139854276 12363² = 152843769 12543² = 157326849
14676² = 215384976 15681² = 245893761 15963² = 254817369
18072² = 326597184 19023² = 361874529 19377² = 375468129
19569² = 382945761 19629² = 385297641 20316² = 412739856
22887² = 523814769 23019² = 529874361 23178² = 537219684
23439² = 549386721 24237² = 587432169 24276² = 589324176
24441² = 597362481 24807² = 615387249 25059² = 627953481
25572² = 653927184 25941² = 672935481 26409² = 697435281
26733² = 714653289 27129² = 735982641 27273² = 743816529
29034² = 842973156 29106² = 847159236 30384² = 923187456
 
There is a total of 20 penholodigital squares in base 11:
42045² = 165742A893 43152² = 173A652894 44926² = 18792A6453
47149² = 1A67395824 47257² = 1A76392485 52071² = 249A758631
54457² = 2719634A85 55979² = 286A795314 59597² = 314672A895
632A4² = 3671A89245 64069² = 376198A254 68335² = 41697528A3
71485² = 46928A7153 81196² = 5A79286413 83608² = 632A741859
86074² = 6713498A25 89468² = 7148563A29 91429² = 76315982A4
93319² = 795186A234 A3A39² = 983251A764
 
There is a total of 23 penholodigital squares in base 12:
117789² = 135B7482A69 16357B² = 23A5B976481 16762B² = 24AB5379861
16906B² = 25386749BA1 173434² = 26B859A3714 178278² = 2835BA17694
1A1993² = 34A8125B769 1A3595² = 354A279B681 1B0451² = 3824B7569A1
1B7545² = 3A5B2487961 2084A9² = 42A1583B769 235273² = 5287BA13469
2528B5² = 5B23A879641 25B564² = 62937B5A814 262174² = 63A8527B194
285A44² = 73B615A8294 29A977² = 7B9284A5361 2A7617² = 83AB5479261
2B0144² = 8617B35A294 307381² = 93825A67B41 310828² = 96528AB7314
319488² = 9AB65823714 319A37² = 9B2573468A1
 
There is a total of 0 penholodigital squares in base 13:
 
There is a total of 160 penholodigital squares in base 14:
1129535² = 126A84D79C53B 3A03226² = DB3962A7541C8
 
There is a total of 419 penholodigital squares in base 15:
4240C58² = 12378DA5B6EC94 EE25E4A² = ED4C93285671BA
 
There is a total of 740 penholodigital squares in base 16:
11156EB6² = 123DA7F85BCE964 3FD8F786² = FEC81B69573DA24
</pre>
 
256

edits