-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem145.rb
38 lines (31 loc) · 963 Bytes
/
Problem145.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# How many reversible numbers are there below one-billion?
# Problem 145
#
# Some positive integers n have the property that the sum [ n + reverse(n) ] consists entirely of odd (decimal) digits. For instance, 36 + 63 = 99 and 409 + 904 = 1313. We will call such numbers reversible; so 36, 63, 409, and 904 are reversible. Leading zeroes are not allowed in either n or reverse(n).
#
# There are 120 reversible numbers below one-thousand.
#
# How many reversible numbers are there below one-billion (109)?
beginning_time = Time.now
class Integer
def reversible?
return false if self.to_s[-1].to_i == 0
sum = (self + self.to_s.reverse!.to_i).to_s
0.upto(sum.length-1) do |i|
if sum[i].to_i % 2 == 0
return false
end
end
return true
end
end
count = 0
0.upto(99_999_999) do |i|
if i.reversible?
count+=1
p i
end
end
p count
end_time = Time.now
puts "Time elapsed #{(end_time - beginning_time)} seconds"