Monday, April 11, 2011

Problem 001

Problem Statement
Add all the natural numbers below one thousand that are multiples of 3 or 5.

Solution
This is a simple problem. Loop through all numbers starting from 1 and incrementing in steps of 1. Then check the following conditions:
1. If the number is divisible by 3, then add that number to a counter.
2. If the number is divisible by 5 and not by 15, then add that number to a counter.

The check for divisibility by 15 ensures that the counter is not incremented twice.

Code


Code Explanation
The code can be explained as follows:

1. Begin loop, starting from 1 and going up to the value entered by the user.
2. If the number is divisible by three then increment a counter with the value of that number.
3. If the number is divisible by five and not by fifteen then increment the counter with the value of that number.
4. Output the counter.

Short Code


How To Use
The call should be made like with the parameter ?n=1000. The ?n is to specify we are going to find the range up to 1000. This is general code and can be used to find multiples of 3 and 5 up to any number.

A sample call can be made like this http://www.Euler_001.php?n=1000

No comments:

Post a Comment