How to resolve the algorithm First perfect square in base n with n unique digits step by step in the jq programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm First perfect square in base n with n unique digits step by step in the jq programming language
Table of Contents
Problem Statement
Find the first perfect square in a given base N that has at least N digits and exactly N significant unique digits when expressed in base N. E.G. In base 10, the first perfect square with at least 10 unique digits is 1026753849 (32043²). You may use analytical methods to reduce the search space, but the code must do a search. Do not use magic numbers or just feed the code the answer to verify it is correct.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm First perfect square in base n with n unique digits step by step in the jq programming language
Source code in the jq programming language
# 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;16) as $b
| squaresearch($b)
| "\($b|lpad(3)) \(tobase($b)|lpad(10) ) \( .*. | tobase($b))" );
task
You may also check:How to resolve the algorithm Nonoblock step by step in the Elixir programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Knapsack problem/0-1 step by step in the Sidef programming language
You may also check:How to resolve the algorithm Optional parameters step by step in the Groovy programming language
You may also check:How to resolve the algorithm Count occurrences of a substring step by step in the TUSCRIPT programming language