Euler Problem 4

 Find the largest palindrome made from the product of two 3-digit numbers.

----- 

 First, I defined a palindrome recursively: a number is one if its first and last digits are the same, and if the inside is also a palindrome.

def palindrome?(digitArr)
  if digitArr.empty? 
    true
  elsif digitArr.length == 1
    true
  else
    (digitArr.first == digitArr.last && palindrome?(digitArr[1..-2])) 
  end
end

 

Note that in Ruby, methods can end with a question mark, a convention that really helps readability.  Also note that the "else if" keyword is elsif, which is weird and unhelpful to me.  I want that 20 minutes of staring at my code like "wtf?!" (before figuring that out) back.

 Then we do a search of all the multiples of two three-digit numbers until we find the biggest palindrome:

def search(i,j)
  maxPal=0
  while i > 99 do
   j=999
   while j > 99 do
      if palindrome?((i*j).to_s.split("")) then
        if(i*j)> maxPal then maxPal = i*j
        end
      end
   j-=1
   end
  i-=1
  end
  return maxPal
end

puts search(999,999)

 

There's probably a prettier way to do this. 

 

 

Published Tue, Apr 8 2008 8:35 PM by mfedner
Filed under: ,

Comments

Wednesday, May 20, 2009 12:07 PM by Mereghost

# Another take on that

class Integer

 def palindrome?

   self.to_s.reverse == self.to_s

 end

end

def search(start_number, end_number)

 retval = 0

 end_number.downto(start_number) do |i|

   end_number.downto(start_number) do |j|

     z = i * j

     retval = z if z > retval && z.palindrome?

   end

 end

 puts retval

end

search(100,999)

Not profiled or benchmarked it against yours, though.

Wednesday, December 23, 2009 9:25 AM by ahtis_ausstellung

# re: Euler Problem 4

<a href="www.hellzyea.com/car-rental"">www.hellzyea.com/car-rental">car">www.hellzyea.com/car-rental">car rental</a> [url="www.hellzyea.com/car-rental"">www.hellzyea.com/car-rental"]car rental[/url] www.hellzyea.com/car-rental  xvn

Wednesday, January 13, 2010 9:59 PM by expressrcparts

# re: Euler Problem 4

[url="www.newblueboy.com/tramadol.html"">www.newblueboy.com/tramadol.html"]tramadol[/url] <a href="www.newblueboy.com/tramadol.html"">www.newblueboy.com/tramadol.html">tramadol</a> www.newblueboy.com/tramadol.html  80358 [url="www.newblueboy.com/nexium.html"">www.newblueboy.com/nexium.html"]nexium[/url] <a href="www.newblueboy.com/nexium.html"">www.newblueboy.com/nexium.html">nexium</a> www.newblueboy.com/nexium.html  >:(( [url="www.bowlinginlubbock.com/Ambien_Prices.htm"">www.bowlinginlubbock.com/Ambien_Prices.htm"]ambien[/url] <a href="www.bowlinginlubbock.com/Ambien_Prices.htm"">www.bowlinginlubbock.com/Ambien_Prices.htm">ambien</a> www.bowlinginlubbock.com/Ambien_Prices.htm  cpktb [url="www.bowlinginlubbock.com/Accutane_Prices.htm"">www.bowlinginlubbock.com/Accutane_Prices.htm"]accutane[/url] <a href="www.bowlinginlubbock.com/Accutane_Prices.htm"">www.bowlinginlubbock.com/Accutane_Prices.htm">accutane</a> www.bowlinginlubbock.com/Accutane_Prices.htm  mxlqky [url="www.newblueboy.com/accutane.html"">www.newblueboy.com/accutane.html"]accutane[/url] <a href="www.newblueboy.com/accutane.html"">www.newblueboy.com/accutane.html">accutane</a> www.newblueboy.com/accutane.html  980

Saturday, March 13, 2010 5:03 AM by czggcmibqy

# re: Euler Problem 4

XCeyxW  <a href="hcbxbyikqhud.com/.../a>, [url=http://qmnihacyrdgv.com/]qmnihacyrdgv[/url], [link=http://zyqrehhejcui.com/]zyqrehhejcui[/link], http://ymzdsvefweti.com/

Leave a Comment

(required) 
(required) 
(optional)
(required)