mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-24 23:04:42 +01:00
41 lines
625 B
Go
41 lines
625 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
"strconv"
|
|
)
|
|
|
|
func main() {
|
|
sum := 0
|
|
for i := 0; i < 10000; i++ {
|
|
if hasPandigitalProduct(i) {
|
|
sum += i
|
|
}
|
|
}
|
|
fmt.Println("sum:", sum)
|
|
}
|
|
|
|
func hasPandigitalProduct(n int) bool {
|
|
for i := 1; i <= n; i++ {
|
|
for n%i == 0 && isPandigital(strconv.Itoa(n)+strconv.Itoa(i)+strconv.Itoa(n/i)) {
|
|
fmt.Printf("Pandigital:%d = %d * %d\n", n, i, n/i)
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func isPandigital(s string) bool {
|
|
var ss []string
|
|
for _, c := range s {
|
|
ss = append(ss, string(c))
|
|
}
|
|
sort.Strings(ss)
|
|
s = ""
|
|
for _, c := range ss {
|
|
s += c
|
|
}
|
|
return s == "123456789"
|
|
}
|