123456789101112131415161718192021222324252627282930 |
- #!/usr/bin/ruby
- # Daniel "Trizen" Șuteu
- # Date: 09 October 2017
- # https://github.com/trizen
- # 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.
- # https://projecteuler.net/problem=4
- # Runtime: 0.175s
- func largest_palindrome_product (n) {
- for k in ((10**n - 1) `downto` 10**(n-1)) {
- var t = Num("#{k}#{Str(k).flip}")
- t.divisors.each {|d|
- if ((d.len == n) && ((t/d).len == n)) {
- return [d, t/d, t]
- }
- }
- }
- }
- say largest_palindrome_product(2)
- say largest_palindrome_product(3)
|