Euler Programming – Problem 1

Ok, so running blogging took over from technical blogging. Something I’m going to attempt to rectify this year (a number of small steps to improve myself over the coming year hopefully).

Scanning through my Twitter stream over the Christmas break I came across an article about Euler Programming site, which provides puzzles to be solved. I’ve decided to start doing these, attempting to do at least 1 a day to try and re-activate my mind. The 1-a-day routine isn’t really succeeding at this stage though – I’ve just solved Problem 3, and this is the 10th day of January. Ah well.

I’ll attempt to post my solution to each problem, in the hope that if I’ve gone wrong, or there is a better way someone might point me in the right direction. All solutions will be done in Java.

Euler Problem 1

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

My Answer

        int sum = 0;
        
        for (int i = 1; i < 1000; i++) {
            if (((i % 3) == 0) || ((i % 5) == 0)) {
                sum += i;
            }
        }
        
        System.out.println(“Sum: ” + sum);

And the overall answer is: 233168

Advertisements

One thought on “Euler Programming – Problem 1

  1. Pingback: Pairing up for Project Euler | code and effect

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