Merge pull request #56 from vzvu3k6k/project-euler-4

Add solution for the Project Euler Problem 4
This commit is contained in:
Vitor Oliveira 2020-12-30 08:44:36 -08:00 committed by GitHub
commit efcd531171
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 0 deletions

View file

@ -43,6 +43,7 @@
* [Sol2](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_3/sol2.rb)
* Problem 4
* [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_4/sol1.rb)
* [Sol2](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_4/sol2.rb)
* Problem 5
* [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_5/sol1.rb)

View file

@ -0,0 +1,26 @@
# frozen_string_literal: true
# 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.
class Integer
def parindrome?
self == reverse
end
# 123.reverse == 321
# 100.reverse == 1
def reverse
result = 0
n = self
loop do
result = result * 10 + n % 10
break if (n /= 10).zero?
end
result
end
end
factors = (100..999).to_a
products = factors.product(factors).map { _1 * _2 }
puts products.select(&:parindrome?).max