-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprob4.rb
More file actions
29 lines (19 loc) · 671 Bytes
/
prob4.rb
File metadata and controls
29 lines (19 loc) · 671 Bytes
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
# A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
# Find the largest palindrome made from the product of two 3-digit numbers.
def is_palindrome? number
num_string = number.to_s
counter = num_string.length / 2
palindromic = true
counter.times {|n| palindromic &&= ( num_string[n] == num_string[-n-1])}
palindromic
end
range = 100..999
largest = 0
range.each { |n|
range.each { |m|
num = m * n
largest = num if is_palindrome?(num) && num > largest
#puts "n: #{n}, m: #{m}" if num == 906609
}
}
puts "Largest palindrome composite is #{largest}"