Penholodigital squares: Difference between revisions
Content added Content deleted
(→{{header|Wren}}: Removed unnecessary 'join'.) |
(Added Go) |
||
Line 49: | Line 49: | ||
;* [[First_perfect_square_in_base_n_with_n_unique_digits]] |
;* [[First_perfect_square_in_base_n_with_n_unique_digits]] |
||
=={{header|Go}}== |
|||
{{trans|Wren}} |
|||
{{libheader|Go-rcu}} |
|||
<syntaxhighlight lang="go">package main |
|||
import ( |
|||
"fmt" |
|||
"math" |
|||
"rcu" |
|||
"strconv" |
|||
) |
|||
func reverse(s string) string { |
|||
r := make([]byte, len(s)) |
|||
for i := 0; i < len(s); i++ { |
|||
r[i] = s[len(s)-1-i] |
|||
} |
|||
return string(r) |
|||
} |
|||
func main() { |
|||
primes := []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47} |
|||
digits := "123456789ABCDEF" |
|||
for b := 9; b <= 16; b++ { |
|||
master := 1 |
|||
for d := 1; d < b; d++ { |
|||
master *= primes[d-1] |
|||
} |
|||
var phd []int |
|||
smin, _ := strconv.ParseInt(digits[0:b-1], b, 64) |
|||
min := int(math.Ceil(math.Sqrt(float64(smin)))) |
|||
smax, _ := strconv.ParseInt(reverse(digits[0:b-1]), b, 64) |
|||
max := int(math.Floor(math.Sqrt(float64(smax)))) |
|||
for i := min; i <= max; i++ { |
|||
sq := i * i |
|||
if b == 10 && (sq%3) != 0 { |
|||
continue |
|||
} |
|||
digs := rcu.Digits(sq, b) |
|||
containsZero := false |
|||
for _, dig := range digs { |
|||
if dig == 0 { |
|||
containsZero = true |
|||
break |
|||
} |
|||
} |
|||
if containsZero { |
|||
continue |
|||
} |
|||
key := 1 |
|||
for _, dig := range digs { |
|||
key *= primes[dig-1] |
|||
} |
|||
if key == master { |
|||
phd = append(phd, i) |
|||
} |
|||
} |
|||
fmt.Println("There is a total of", len(phd), "penholodigital squares in base", b, "\b:") |
|||
if b > 13 { |
|||
phd = []int{phd[0], phd[len(phd)-1]} |
|||
} |
|||
for i := 0; i < len(phd); i++ { |
|||
sq2 := phd[i] * phd[i] |
|||
fmt.Printf("%s² = %s ", strconv.FormatInt(int64(phd[i]), b), strconv.FormatInt(int64(sq2), b)) |
|||
if (i+1)%3 == 0 { |
|||
fmt.Println() |
|||
} |
|||
} |
|||
if len(phd)%3 != 0 { |
|||
fmt.Println() |
|||
} |
|||
fmt.Println() |
|||
} |
|||
}</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> |
|||
=={{header|J}}== |
=={{header|J}}== |