HiveBrain v1.2.0
Get Started
← Back to all entries
patternrubyMinor

Ruby solution to Project Euler Problem #4: Largest palindrome product

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
problemproductlargestpalindromeprojecteulerrubysolution

Problem

I've solved the Project Euler Problem #4, but I'd like some tips as to how to make this more efficient. I am a beginner to Ruby, so please be nice about the stupid stuffs (but still tell me about it).

Project Euler 4: Largest palindrome product

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.
1

class Euler4
  def isPalindrome(n)
    n == n.to_s.reverse.to_i
  end
end

  puts "Setting Variables"
  x = 999
  y = 999

  highest = 1

  found = false

  while (x > 100 && !found)
    y = 999
    while (y > 100 && !found)
      puts "Working with"
      puts x
      puts " and "
      puts y

      if Euler4.new.isPalindrome(x*y)
        puts "Found Palindrome"
        puts x
        puts y
        puts x*y
        if highest < (x*y)
          highest = x * y
        end
      end
      y = y - 1
    end

    x = x - 1

  end

puts "Highest Found"
puts highest

Solution

Let's start with something small:

def isPalindrome(n)
  n == n.to_s.reverse.to_i
end


How do I know if a number is a palindrome?

  • Convert it to a string.



  • Produce the reverse of that string.



  • Convert the reversed string to a number.



  • Compare the numbers for equality.



As yourself: why did we need to do the last two steps? We could have said:

  • Convert it to a string.



  • Produce the reverse of that string.



  • Compare the strings for equality.



and skipped the "convert to integer" step entirely.

Next small thing. I've removed every line of your program that does not use variable found:

found = false
while (x > 100 && !found)
  while (y > 100 && !found)


See any problems with that code? Because I don't see anywhere that it is set to true and therefore it will always be false. I'm not sure why a variable that is always false is of use to you.

Next thing: You check all pairs: (999, 999), (999, 998), ... (999, 101) and then start over again with (998, 999). But you already checked (998, 999) when you checked (999, 998)! You don't need to start y at 999. It suffices to start y at x. Then you only check each pair once instead of checking the vast majority of them twice.

Next thing: If you find an (x, y) pair that is a palindrome, you don't need to check (x, y - 1); even if it is a palindrome, it will be smaller. Probably this is what you were trying to do with your "found" variable, but you never wrote the logic correctly.

Next thing: Remove all that print-debugging trace. If you need to debug your program, use a debugger.

Code Snippets

def isPalindrome(n)
  n == n.to_s.reverse.to_i
end
found = false
while (x > 100 && !found)
  while (y > 100 && !found)

Context

StackExchange Code Review Q#136073, answer score: 4

Revisions (0)

No revisions yet.