One billion isn’t that big, you can just check them all if each check is fast
Yeah, I think you can just do it (assuming a fast enough language)
I’m going to just try doing it brute force in go
I think this suffices for reverse:
func reverse(n int) int {
result := 0
for n > 0 {
result = 10*result + n%10
n /= 10
}
return result
}
(12:17:47 AM) Then reversible is fairly straightforward too...
(12:18:42 AM) Does this work?
func reversible(n int) bool {
sum := n + reverse(n)
for sum > 0 {
if (sum % 10) % 2 == 0 {
return false
}
sum /= 10
}
return true
}
This gives me 125 reversible numbers below 1000… what?
Is there some leading zeros thing going on, I think that’s what’s it. A reversible number can’t be divisible by 10!
Great, checking for that gives us 120 as expected.
(12:22:12 AM) OK, running up to 1 billion… hopefully this will finish in time
Code:
package main
import "fmt"
func reverse(n int) int {
result := 0
for n > 0 {
result = 10*result + n%10
n /= 10
}
return result
}
func reversible(n int) bool {
if n%10 == 0 {
return false
}
sum := n + reverse(n)
for sum > 0 {
if (sum%10)%2 == 0 {
return false
}
sum /= 10
}
return true
}
func main() {
count := 0
for i := 1; i < 1000000000; i++ {
if reversible(i) {
count++
}
}
fmt.Println(count)
}
Answer: 608720
(took 30 seconds to run)
(12:22:49 AM) done