mirror of
https://github.com/TheAlgorithms/Ruby
synced 2025-01-29 20:34:27 +01:00
Add solution for the Project Euler Problem 4
This commit is contained in:
parent
42edafb785
commit
55e16ab995
1 changed files with 23 additions and 0 deletions
23
Project Euler/Problem 4/problem4_sol1.rb
Normal file
23
Project Euler/Problem 4/problem4_sol1.rb
Normal file
|
@ -0,0 +1,23 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
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
|
Loading…
Add table
Reference in a new issue