Project Euler – Math Is Fun

I recently came across Project Euler – A cool site with interesting math problems to solve. It has hundreds of problems to solve and it keeps track of what problems you have solved. The site also lists the number of people that have solved each problem so you get to feel like you’re part of a sweet exclusive club.

They get progressively harder.

The first question is: Find the sum of all the multiples of 3 or 5 below 1000.  Here’s my solution:

int sum = 0;

for (int i = 0; i < 1000; i++)
{
    if ((i % 3 == 0) || (i % 5 == 0))
    {
        sum += i;
    }
}

The fourth question is: Find the largest palindrome made from the product of two 3-digit numbers. Here’s my solution:

int largest = 0;

for (int i = 100; i < 1000; i++)
{
    for (int j = 100; j < 1000; j++)
    {
        int product = i*j;
        string productAsString = product.ToString();
                    
        char[] productReversed = productAsString.ToCharArray();
        Array.Reverse(productReversed);

        if (productAsString.Equals(new string(productReversed)))
        {
            if (product > largest)
                largest = product;
        }
    }
}

I’m not sure I’ll post my solutions to all the problems, but currently my source code can be found here: https://github.com/marksl/project-euler

This is a good problemset to keep in mind for practicing other programming languages too!!

Advertisements
This entry was posted in C# and tagged , , , . Bookmark the permalink.

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