Skip to content

Instantly share code, notes, and snippets.

@viktorasm
Created February 25, 2025 10:50
Show Gist options
  • Save viktorasm/07fa4fd2fbc1d554e5aff306039b1cb9 to your computer and use it in GitHub Desktop.
Save viktorasm/07fa4fd2fbc1d554e5aff306039b1cb9 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math"
)
func magnitude(v int64) int64 {
if v == 0 {
return 0
}
result := int64(10)
for v > result {
result *= 10
}
return result
}
func reconcatenate(a, b int64) int64 {
return a*magnitude(b) + b
}
func sqrtInt(val int64) int64 {
return int64(math.Sqrt(float64(val)))
}
func sqr(a int64) int64 {
return a * a
}
func checkNumber(val int64) bool {
if sqr(sqrtInt(val)) != val {
return false
}
split := int64(10)
for val > split {
a := val / split
b := val % split
if reconcatenate(a, b) == val && val == (a+b)*(a+b) {
fmt.Printf("(%d + %d)^2 = %d\n", a, b, val)
return true
}
split *= 10
}
return false
}
func countAccepted(numDigits int64) int64 {
result := int64(0)
max := maxNumber(numDigits)
maxSqrt := sqrtInt(max)
for i := range maxSqrt {
val := i * i
if checkNumber(val) {
println("accepted:", val)
result += val
}
if i%100000000 == 0 {
fmt.Printf("progress: %.6f%% \n", float64(i)/float64(maxSqrt)*100)
}
}
return result
}
func maxNumber(numDigits int64) int64 {
result := int64(1)
for range numDigits {
result *= 10
}
return result
}
func main() {
println("total:", countAccepted(16))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment