Saturday, August 11, 2012

Programming Challenge no. 1: Sum of Multiples of 3 and 5

So I stumbled upon this site called Project Euler (It was named after Leonhard Euler who had a huge contribution in discovering infinitesimal calculus and graph theory.. Not sure if I'm thankful about his discovery.. But I guess I should since it helped improve a lot of things..)

What am I doing in Project Euler? Well, you see.. I promised to answer some programming challenges for you and as I googled the words "programming challenges", I saw this site. It's not bad actually.. I kinda like it :3


/* Warning: Must have at least basic programming experience */


So let's start with their very first challenge:
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.
You can go ahead and get your calculator. I'm not stopping you. However, I must warn you that you're gonna waste at least 15 minutes of your life pressing the multiples of 3 and 5.. and with a huge possibility of committing a mistake..

If I were you, I'd find an algorithm that will give me the final result. One way is solving it the programming style :P


For this challenge, I'll use C as my programming language.
I'll use Turbo C as my compiler. (This doesn't matter actually)
And my left brain to solve the problem! ('Cause for some reason, my right brain hates numbers..)


Analyzing the problem


We are asked to compute the sum of the multiples of 3 and 5. 

In their example, the multiples of 3 and 5 below 10 are given: 3, 5, 6 and 9. Adding these multiples will give us 23. We need to do the same thing. Only this time, we need to find the multiples below 1000.

It's tempting to list down all the multiples then add them together.. but that would be an inefficient way to solve this challenge..

What we need to do is analyze and take note what we need to solve the problem:

  1. Only multiples of 3 and 5 are allowed.
  2. Numbers like 15, 30.. etc. are multiples of 3 and at the same time, of 5.
  3. Multiples should be below 1000.
So basically we need to add numbers below 1000 that are either multiples of 3 or 5 or both..


Creating the Pseudo code


Pseudo codes are useful in creating programs. Since they use simple statements instead of a specific programming language's syntax, they're very easy to read.

So here's the pseudo code for our solution:

sum as unsigned long    // We create a variable sum that holds unsigned long integers 
num as integer             // We create a counter num that will go thru 1 to 999
While num is less than 1000     // Set a condition that will prevent the loop from going beyond 1000
          if num is divisible by 3 or 5     // Check if number is a multiple of 3 or 5
             add num to sum                  // If it is, add it to sum
          add 1 to num                          // Iterate num
print sum                                          // Show output on screen

What will happen is, the computer will go through all the numbers from 0 to 999. Each will be checked if it's divisible by 3 or 5. If the number is indeed a multiple of 3 or 5, it'll be added to the sum.

Now that wasn't so hard right? Now, let's go to the exciting part: coding..

Coding


I'll just show you directly what the code is and explain each part.

void main(){
     unsigned long sum;
     int num;
This part contains the declaration statements. Here, we declared two variables: sum and num
sum shall contain the total value of the multiples. It's unsigned long since we expect a large number. num will be iterated from 1 to 999 so it's basically like a counter.

for(sum = 0, num = 0; num < 1000; i++){
      if((num % 3 == 0) || (num % 5 == 0)){
             sum = sum + num;
     }
}
This loop searches thru the numbers below 1000 and finds the multiples of 3. The condition uses || known as the symbol for OR because we need all the multiples of 3 and 5. Using && (known as AND) will give us only the multiples of both 3 and 5.
printf("%lu", sum);

Note:  The sum should be long since we're dealing with a huge number.


Conclusion


So? Did you understand how to solve this programming problem? If you're still having trouble comprehending the method, don't hesitate to comment.

By the way, if you want a shorter code, here's another version of it..

void main(){
     unsigned long sum;
     int num;
     for(sum = 0, num = 0; num < 1000; sum = sum + ((num % 3 == 0 || num % 5 == 0) ?           num : 0), num++);
     printf("%lu", sum);
}
The italic part of the code is using ternary operation. I'll talk about that next time. I don't want to overwhelm you.

You might be asking: So what's the answer? That would be known only if you program the code yourself :P
I don't wanna spoon-feed you with answers to the Project Euler's challenges. My goal here is to relay what I can about programming. Not become a source of direct answers from a site that challenges you to solve programming problems.

If you want your answers checked or you're having troubles with the code, you can email me directly.

I hope you learned something :)

1 comment:

  1. postingan yang bagus tentang Programming Challenge no. 1: Sum of Multiples of 3 and 5

    ReplyDelete