First perfect square in base n with n unique digits: Difference between revisions
Content added Content deleted
Line 1,528: | Line 1,528: | ||
11 -> 111453 -> 1240a536789 |
11 -> 111453 -> 1240a536789 |
||
12 -> 3966b9 -> 124a7b538609</pre> |
12 -> 3966b9 -> 124a7b538609</pre> |
||
=={{header|jq}}== |
|||
'''Adapted from [[#Julia|Julia]]''' |
|||
'''Works with jq and gojq, the C and Go implementations of jq''' |
|||
Some useful filters, but nothing fancy here. |
|||
<syntaxhighlight lang=jq> |
|||
# Input: an integral decimal number |
|||
# Output: the representation of the input in base $b as |
|||
# an array of one-character digits, with the least significant digit first. |
|||
def tobaseDigits($b): |
|||
def digit: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[.:.+1]; |
|||
def mod: . % $b; |
|||
def div: ((. - mod) / $b); |
|||
def digits: recurse( select(. > 0) | div) | mod ; |
|||
if . == 0 then "0" |
|||
else [digits | digit][:-1] |
|||
end; |
|||
def tobase($b): |
|||
tobaseDigits($b) | reverse | add; |
|||
# Input: an alphanumeric string to be interpreted as a number in base $b |
|||
# Output: the corresponding decimal value |
|||
def frombase($b): |
|||
def decimalValue: |
|||
if 48 <= . and . <= 57 then . - 48 |
|||
elif 65 <= . and . <= 90 then . - 55 # (10+.-65) |
|||
elif 97 <= . and . <= 122 then . - 87 # (10+.-97) |
|||
else "decimalValue" | error |
|||
end; |
|||
reduce (explode|reverse[]|decimalValue) as $x ({p:1}; |
|||
.value += (.p * $x) |
|||
| .p *= $b) |
|||
| .value ; |
|||
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .; |
|||
# $n and $base should be decimal integers |
|||
def hasallin($n; $base): |
|||
$base == ($n | tobaseDigits($base) | unique | length); |
|||
def squaresearch($base): |
|||
def num: "0123456789abcdef"; |
|||
(("10" + num[2:$base]) | frombase($base)) as $highest |
|||
| first( range( $highest|sqrt|floor; infinite) # $highest + 1 |
|||
| select(hasallin(.*.; $base)) ); |
|||
def task: |
|||
"Base Root N", |
|||
(range(2;15) as $b |
|||
| squaresearch($b) |
|||
| "\($b|lpad(3)) \(tobase($b)|lpad(10) ) \( .*. | tobase($b))" ); |
|||
task |
|||
</syntaxhighlight> |
|||
{{output}} |
|||
<pre> |
|||
Base Root N |
|||
2 10 100 |
|||
3 22 2101 |
|||
4 33 3201 |
|||
5 243 132304 |
|||
6 523 452013 |
|||
7 1431 2450361 |
|||
8 3344 13675420 |
|||
9 11642 136802574 |
|||
10 32043 1026753849 |
|||
11 111453 1240A536789 |
|||
12 3966B9 124A7B538609 |
|||
13 3828943 10254773CA86B9 |
|||
14 3A9DB7C 10269B8C57D3A4 |
|||
</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |