Penholodigital squares: Difference between revisions

(julia example)
Line 243:
fec81b69573da24=3fd8f786²
NB. this is getting to be obnoxiously long in terms of time...</syntaxhighlight>
 
=={{header|jq}}==
'''Adapted from [[#Python|Python]]'''
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq, and with fq'''
 
Note that the definition of `_nwise/1` may be omitted if using jq.
 
'''General Utilities'''
<syntaxhighlight lang=jq>
# This def may be omitted if using jq
def _nwise($n):
def nw: if length <= $n then . else .[0:$n] , (.[$n:] | nw) end;
nw;
 
# Evaluate SIGMA $x^k * $p[k] for k=0...
def evalpoly($x; $p):
reduce range(0;p|length) as $i ({power: 1, sum:0};
.sum += .power * $p[$i]
| .power *= $x)
| .sum;
 
# Convert the input integer to a string in the specified base (2 to 36 inclusive)
def convert(base):
def stream:
recurse(if . > 0 then ./base|floor else empty end) | . % base ;
if . == 0 then "0"
else [stream] | reverse | .[1:]
| if base < 10 then map(tostring) | join("")
elif base <= 36 then map(if . < 10 then 48 + . else . + 87 end) | implode
else error("base too large")
end
end;
 
# If $j is 0, then an error condition is raised;
# otherwise, assuming infinite-precision integer arithmetic,
# if the input and $j are integers, then the result will be an integer.
def idivide($j):
. as $i
| ($i % $j) as $mod
| ($i - $mod) / $j ;
 
# input should be a non-negative integer for accuracy
# but may be any non-negative finite number
def isqrt:
def irt:
. as $x
| 1 | until(. > $x; . * 4) as $q
| {$q, $x, r: 0}
| until( .q <= 1;
.q |= idivide(4)
| .t = .x - .r - .q
| .r |= idivide(2)
| if .t >= 0
then .x = .t
| .r += .q
else .
end)
| .r ;
if type == "number" and (isinfinite|not) and (isnan|not) and . >= 0
then irt
else "isqrt requires a non-negative integer for accuracy" | error
end ;
 
# Input: an integer base 10
# Output: an array of the digits (characters) if . were printed in base $base
def digits($base):
convert($base) | tostring | [explode[] | [.] | implode];
</syntaxhighlight>
'''The Task'''
<syntaxhighlight lang=jq>
# emit an array of [$n,$sq] values where $n is a penholodigital square in the given base
# and $n and $sq are integers expressed in that base
def penholodigital($base):
{ hi: (evalpoly($base; [range(1;$base)])|isqrt),
lo: (evalpoly($base; [range(base-1; 0; -1)]) | isqrt) # evalpoly(base, base-1:-1:1)
}
| reduce range(.lo; .hi+1) as $n (null;
($n * $n) as $sq
| ($sq | digits($base)) as $digits
| if "0" | IN($digits[]) then .
elif ($digits | unique | length) == $base-1 and ($digits | sort | length) == $base - 1
then . + [[($n | convert(base)), ($sq | convert(base))]]
else .
end );
 
def task(a;b):
range(a;b) as $base
| penholodigital($base)
| "\n\nThere are \(length) penholodigital squares in base \($base):",
(_nwise(3)
| map("\(.[0])² = \(.[1])" )
| join(" "));
task(9;13)
</syntaxhighlight>
{{output}}
<pre>
There are 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 are 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 are 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 are 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
</pre>
 
=={{header|Julia}}==
Line 320 ⟶ 458:
11156eb6² = 123da7f85bce964 3fd8f786² = fec81b69573da24
</pre>
 
 
=={{header|Pascal}}==
2,442

edits