First perfect square in base n with n unique digits: Difference between revisions
Content added Content deleted
m (uses gforth built-in dec.r word) |
|||
Line 1,773: | Line 1,773: | ||
26 5 K99MDB35N8K25 -> ABDJNHCPF97GKMEI6OL8543201 402922566 05:15.307 06:49.008 |
26 5 K99MDB35N8K25 -> ABDJNHCPF97GKMEI6OL8543201 402922566 05:15.307 06:49.008 |
||
27 26 JJBO73E11F1101 -> A6N9QC7PKGFJIBHDMOLE8543201 457555291 06:01.338 12:50.347</pre> |
27 26 JJBO73E11F1101 -> A6N9QC7PKGFJIBHDMOLE8543201 457555291 06:01.338 12:50.347</pre> |
||
=={{header|Nim}}== |
|||
{{trans|D}} |
|||
<lang Nim>import algorithm, math, strformat |
|||
const Alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
|||
func toBaseN(num, base: Natural): string = |
|||
doAssert(base in 2..Alphabet.len, &"base must be in 2..{Alphabet.len}") |
|||
var num = num |
|||
while true: |
|||
result.add(Alphabet[num mod base]) |
|||
num = num div base |
|||
if num == 0: break |
|||
result.reverse() |
|||
func countUnique(str: string): int = |
|||
var charset: set['0'..'Z'] |
|||
for ch in str: charset.incl(ch) |
|||
result = charset.card |
|||
proc find(base: Natural) = |
|||
var n = pow(base.toFloat, (base - 1) / 2).int |
|||
while true: |
|||
let sq = n * n |
|||
let sqstr = sq.toBaseN(base) |
|||
if sqstr.len >= base and countUnique(sqstr) == base: |
|||
let nstr = n.toBaseN(base) |
|||
echo &"Base {base:2d}: {nstr:>8s}² = {sqstr:<16s}" |
|||
break |
|||
inc n |
|||
when isMainModule: |
|||
for base in 2..16: |
|||
base.find()</lang> |
|||
{{out}} |
|||
<pre>Base 2: 10² = 100 |
|||
Base 3: 22² = 2101 |
|||
Base 4: 33² = 3201 |
|||
Base 5: 243² = 132304 |
|||
Base 6: 523² = 452013 |
|||
Base 7: 1431² = 2450361 |
|||
Base 8: 3344² = 13675420 |
|||
Base 9: 11642² = 136802574 |
|||
Base 10: 32043² = 1026753849 |
|||
Base 11: 111453² = 1240A536789 |
|||
Base 12: 3966B9² = 124A7B538609 |
|||
Base 13: 3828943² = 10254773CA86B9 |
|||
Base 14: 3A9DB7C² = 10269B8C57D3A4 |
|||
Base 15: 1012B857² = 102597BACE836D4 |
|||
Base 16: 404A9D9B² = 1025648CFEA37BD9</pre> |
|||
=={{header|Pascal}}== |
=={{header|Pascal}}== |