Public Group

# Trying to solve mathematical problem using programing skills

This topic is 2059 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello.
The question i was posed 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.

int main()
{
int sum = 0;

for(int i = 0; i < 1000; i++)
{
if((i % 3) == 0)
{
sum += i;
}
if((i%5) == 0)
{
sum += i;
}
}
std::cout<<sum<<std::endl;
}

Output is "266333"
what em i doing wrong?

##### Share on other sites
You're accumulating multiples of 15 twice since those numbers are divisible by both 3 and 5. I'm not going to provide a solution though, but let you think about it.

##### Share on other sites

You're accumulating multiples of 15 twice since those numbers are divisible by both 3 and 5. I'm not going to provide a solution though, but let you think about it.

You don't have to, and thanks for the answer.
I feel oddly stupid.

The output now is now correct
Combined the if statement to one

if((i % 3) == 0 || (i%5) == 0)
{
sum += i;
OStream << i << "\n";
}
Edited by BaneTrapper

##### Share on other sites
Read opened thread for this problem to find out better solution. It's really interesting.

##### Share on other sites
I feel dumb for only now realizing that Project Euler used a FizzBuzz-like situation on their first problem. Edited by Nypyren

##### Share on other sites
I think you can solve it in constant time on paper (or with one expression in your programming language), no need for loops

##### Share on other sites

I think you can solve it in constant time on paper (or with one expression in your programming language), no need for loops

For 3 and 5, yeah, you can for any bound [eqn]n[/eqn] (inclusive, use [eqn]n - 1[/eqn] if you don't want to include it), it's just:

[eqn]\displaystyle \left ( \sum_{k = 1}^{\lfloor n / 3 \rfloor} 3k \right ) + \left ( \sum_{k = 1}^{\lfloor n / 5 \rfloor} 5k \right ) - \left ( \sum_{k = 1}^{\lfloor n / 15 \rfloor} 15k \right )[/eqn]

Which simplifies down to (the floor symbols indicate integer division):

[eqn]\displaystyle \frac{3}{2} \left \lfloor \frac{n}{3} \right \rfloor \left (\left \lfloor \frac{n}{3} \right \rfloor + 1 \right ) + \frac{5}{2} \left \lfloor \frac{n}{5} \right \rfloor \left (\left \lfloor \frac{n}{5} \right \rfloor + 1 \right ) - \frac{15}{2} \left \lfloor \frac{n}{15} \right \rfloor \left (\left \lfloor \frac{n}{15} \right \rfloor + 1 \right )[/eqn]

For [eqn]n = 1000[/eqn], this gives [eqn]234168[/eqn]. For [eqn]n = 999[/eqn], we get [eqn]233168[/eqn] as expected.

You can see the general pattern - sum up the multiples of each number required (here 3 and 5), and then subtract any numbers which are multiples of any two of them at the same time. You can generalize this to more than two numbers, but it gets pretty messy as you need [eqn]2^p - 1[/eqn] individual sums to get the correct result for [eqn]p[/eqn] different numbers, so it gets impractical if you need to consider many different divisors.

##### Share on other sites

Read opened thread for this problem to find out better solution. It's really interesting.

I was asking what is wrong with my code, tho i made mistake.
I tried looking for other codes to see how they where made, i keep crossing on PHP code, so i open thread .

I think you can solve it in constant time on paper (or with one expression in your programming language), no need for loops

I have so low mathematical knowledge. Don't know these formulas at all.

For 3 and 5, yeah, you can for any bound [eqn]n[/eqn] (inclusive, use [eqn]n - 1[/eqn] if you don't want to include it), it's just:
[eqn]\displaystyle \left ( \sum_{k = 1}^{\lfloor n / 3 \rfloor} 3k \right ) + \left ( \sum_{k = 1}^{\lfloor n / 5 \rfloor} 5k \right ) - \left ( \sum_{k = 1}^{\lfloor n / 15 \rfloor} 15k \right )[/eqn]
Which simplifies down to (the floor symbols indicate integer division):
[eqn]\displaystyle \frac{3}{2} \left \lfloor \frac{n}{3} \right \rfloor \left (\left \lfloor \frac{n}{3} \right \rfloor + 1 \right ) + \frac{5}{2} \left \lfloor \frac{n}{5} \right \rfloor \left (\left \lfloor \frac{n}{5} \right \rfloor + 1 \right ) - \frac{15}{2} \left \lfloor \frac{n}{15} \right \rfloor \left (\left \lfloor \frac{n}{15} \right \rfloor + 1 \right )[/eqn]
For [eqn]n = 1000[/eqn], this gives [eqn]234168[/eqn]. For [eqn]n = 999[/eqn], we get [eqn]233168[/eqn] as expected.
You can see the general pattern - sum up the multiples of each number required (here 3 and 5), and then subtract any numbers which are multiples of any two of them at the same time. You can generalize this to more than two numbers, but it gets pretty messy as you need [eqn]2^p - 1[/eqn] individual sums to get the correct result for [eqn]p[/eqn] different numbers, so it gets impractical if you need to consider many different divisors.

If they have shown me how to do it from 0 - 1000 id know how to do it from 0 - 10000^^.
But yea, its kinda too much for me since you most know good math for those problems. I didn't get far with it, 3/10 problems solved and am pleased since i don't understand 70% of those questions.
Thanks on good mood.

##### Share on other sites
Sigma notation can be difficult to read if you're not used to it. It's actually quite straightforward, but for some reason large Greek letters can stun the brain for several seconds. (This happens to me often and is probably a sign of poor nutrition or a brain injury resulting from sitting too close to the television as a kid.) Edited by Khatharr

##### Share on other sites

Sigma notation can be difficult to read if you're not used to it. It's actually quite straightforward, but for some reason large Greek letters can stun the brain for several seconds. (This happens to me often and is probably a sign of poor nutrition or a brain injury resulting from sitting too close to the television as a kid.)
Quoted for truth. Its my experience in Mathematical Analysis, Algebra and Algorithms courses summed up in a single quote.

1. 1
2. 2
Rutin
25
3. 3
4. 4
JoeJ
18
5. 5
gaxio
11

• 14
• 22
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631765
• Total Posts
3002213
×