Project Euler 004 – IronRuby

23 02 2011

Problem 4

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.

Solution

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

 

# placeholder

answer = 0;

 

#loop through each 3-digit number

for i in 100..999 do

    for j in i..999 do

        product = i*j

        # the palindrome test is simple here

        # since we are using the same technique

        # as the F# and C# functions

        if product.to_s().reverse == product.to_s() &&

            answer < product

                answer = product

        end

    end

end

 

puts answer

 

Discussion

I have to admit, Ruby makes this code pretty simple.

If you have questions, leave a comment or please find me on Twitter (@azzlsoft) or email (rich@azzlsoft.com).

Advertisements

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: