Trying to solve mathematical problem using programing skills

Started by
12 comments, last by Cagnazzo 11 years, 4 months ago
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.
The program i made

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"
The answer is "233168"
what em i doing wrong?
Advertisement
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'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";
}
Read opened thread for this problem to find out better solution. It's really interesting.
C x 2 C x o C x C f nice C x C s C x C c
I feel dumb for only now realizing that Project Euler used a FizzBuzz-like situation on their first problem. :)
I think you can solve it in constant time on paper (or with one expression in your programming language), no need for loops :)

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.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”


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 :P.


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.
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.)
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

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.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

This topic is closed to new replies.

Advertisement