# finding multiple of a number?

Page 1 of 1

## 2 Replies - 19836 Views - Last Post: 01 August 2013 - 01:01 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=326084&amp;s=a59b9e352c4fa2a7ce1f4059e4b00f56&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 AndreeU17

Reputation: 6
• Posts: 163
• Joined: 29-June 13

# finding multiple of a number?

Posted 31 July 2013 - 11:08 PM

Hi i wanted to solve this question:
```/*
* Find the multiple of 2 number that are under 1000
*
* The Question is:
* 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.
*
*/

```

So i went ahead and realized that for this i would need a for loop, i also figured i needed an if statement but never implemented it because i wasn't sure how to use it in this scenario.
So as for me, i created this which by the way i got all the multiples of 3 the way i wanted but i feel like i cheated myself. Instead of using computing logic and use statements, methods, or loops to find the multiples of 3, i just simply kept adding 3 resulting in the correct answer. Could anyone show me how it was suppose to be programmed or was this the actual way i should of done it ? Thanks
```public class multiples {

public static void main(String[] args) {
multipleOf3();

}

public static void multipleOf3(){
int multipleOf3 = 3;
for(int i = 0; i < 1000; i+=3){
System.out.println(i);
}
}

}

```

REMEMBER IM ONLY SOLVING FOR THE MULTIPLE OF 3 NOT 5

Thank You
-Jonathan

Is This A Good Question/Topic? 0

## Replies To: finding multiple of a number?

### #2 jon.kiparsky

• Beginner

Reputation: 11020
• Posts: 18,804
• Joined: 19-March 11

## Re: finding multiple of a number?

Posted 31 July 2013 - 11:51 PM

That's actually not a bad approach. You definitely understand the mathematical meaning of "multiple of 3" here. And if you want to assemble a list of multiples of 3, this is not really a terrible way to do it.

Depending how you want to go after this, you might consider whether it's useful to know that 1000/3 * 3 is a good way to get the greatest multiple of 3 which is less than 1000. (why is this? Because integer division discards the remainder, so 1000/3 returns the greatest number n such that n*3 <=1000.) 1000/3 might also be a useful number here. For example, if you had the sum of integers from 1..1000/3, you could multiply that by 3 to get the sum of multiples of 3 which are less than 1000.

However, you might want to observe that this problem asks for the sum of multiples of 3 OR 5 - this implies that you want to add 15 only once!

You can correct for this in two ways - either by being careful about what you add, or by removing those items you shouldn't have added in the first place.

(I'm being careful to say little about how to actually solve the problem, since I recognize this as an Euler problem, and I respect their wish to preserve the integrity of their problem set. I will be deleting or editing posts which violate this by providing more solution than I think is warranted...)

This post has been edited by jon.kiparsky: 31 July 2013 - 11:52 PM

### #3 cfoley

• Cabbage

Reputation: 2388
• Posts: 5,013
• Joined: 11-December 07

## Re: finding multiple of a number?

Posted 01 August 2013 - 01:01 AM

Once you have solved it with code, this is a good one to go back and solve with pen and paper. Use the same heuristic. If it takes a minute for the actual pen and paper calculation then there is a better way. Obviously, looping up to 1000 won't cut it here!

So, you asked about an if statement. I assume you want something like this, and that the bit in brackets is what you're not getting.

```for (int i == 0; i < 1000; i++) {
if (/* is a multiple of something */) {
counter += i;
}
}
```

If something is a multiple, then the remainder after division is zero. The % operator gives you that remainder.