-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprob_004.rb
42 lines (33 loc) · 1004 Bytes
/
prob_004.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
39
40
41
42
# 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.
# Some people get fancy with 11's, but that's not an guarenteed solution
# as odd-digited palindromic numbers aren't products of 11.
# time: O(n^2)
# space: O(1)
# stack: O(1)
# 0.27s user
# 0.03s system
# 86% cpu
# 0.352 total
def largest_palindrome_for_x_digit_numbers(digits)
min = 10**(digits-1)
max = 10**(digits) - 1
brute_largest_palindrome_product(min, max)
end
def brute_largest_palindrome_product(min, max)
largest = 0
for i in (min..max)
for j in (i..max)
product = i*j
largest = product if palindrome?(product) and product > largest
end
end
largest
end
def palindrome?(number)
num = number.to_s
half = num.length/2
num[0, half] == num[num.length-half, num.length].reverse
end
puts largest_palindrome_for_x_digit_numbers(3)