My program for Multiples of 3 and 5 is'nt working.

I'm trying to do the Projecteluer's first question. And my program seems to be failing me and I've got no clue what it might be.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>

using namespace std;

int main()
{
	int a = 0;
	int b = 0;
	int c = 0;
	int d = 0;
	int jump = 0;

	while (a < 1000)
	{
		a += 3;
		c = a;
		d = c + d;
	}

	if (a >= 1000)
	{
		d = d - a;
	}

	while (b < 1000)
	{
		jump++;
		if (jump == 2) { b += 5; jump = 0; }
		b += 5;
		c = b;
		d = c + d;
	}

	if (b >= 1000)
	{
		d = d - b;
	}

	cout << d << endl;
	system("Pause");
	return 0;
}
what do you want the program to do exactly?
also a will never be >= 1000 because the while loop ends before a can reach 1000 (a < 1000)
it did accualy go over for example "while (a < 10) { a += 3}" would end up with a = 12 (because 9 is lower than ten so i adds 3 a forth time) so i had to put in that thing for saftey. But i want the multiples of 3 and 5 all the way up to 1000 and then add them together. But it seems my program is'nt doing the job also multiples of that are the same for example both 5 and 3 get 15 my program is'nt allowed cout both of them so i did that jump thing. I'm getting 233498 while the answear is 233168.
I see your point with the going over 1000, it went to 1002.

Now I am still not sure what you want from your program:/

They all start at value of 0, then a goes up in value by 3 until its bigger than 1000

Then b goes up by 5 but only every 3 counts, and what is c for?

Could you give me a simple example of what your program is supposed to do?
also if you're not sure what your loops are up to, output your variables to see what is going on
1
2
3
4
5
6
7
8
	while (a < 1000)
	{
		a += 3;
		c = a;
		d = c + d;
		
		cout << "a: " << a << "\tc: " << c << "\td: " << d << endl;
	}
it's supposed to get the multiples of 5 and 3 for example istead of 1000 I'll use 10. 3,6,9 are the multiples of 3 to 10 and the multiples of 5 to 10 is well 5. then im supposed to add these numbers 3, 6, 9, 5, the sum is 23 that's what I'm but instead of having ten as the limit I'm supposed to do a program that count like that to 1000 and then summs it up. Sorry for explaining so bad my I'm not very good a cpp and english.
Just tried the code you sent me (cool now I know how to keep track thnx) The numbers it displayed seemed to be correct a is going up with 3's and c is just a copy to save the number in while i add it in d. So it seems as im just as confused as i was before.
I understand now what you want from your program, you might have over complicated it a bit

d is a final result, right?

if so why won't you just keep increment a += 3 and at the same time add it up to d +=a

so that's your multiples of 3 added up

then make a second loop that will increment by 5 (b += 5) and add that up to the final result (d += b)

I don't see a need for c at all. Also why do you have those if statements in line 20 and 34 that? Why would you d = d - a; and d = d - b;???

Try smaller, with limit of 10 or 100 and output every single a and b to make sure it does what it supposed to do and if it does change limit to 1000
Thnx that helped me a lot with learning on how to write programs :D. But now for the final issue. 5 goes to 10 and then 15, and 3 goest to 6, 9, 12 ,15. And the problem is I'm not allowed to add up the same numbers to "d" so i tried making the "jump" so it would just jump over one by adding another 5 so it goes 5, 10, 20 instead of 5, 10, 15. so that i dont add up the same numbers in d.
so if a number is a multiplication of 5 but it is at the same time dividable by 3 you don't want it to be added up again? Like 15, 30, 45 ets was already added when your first loop was doing it's thing?

in that case do you know what mod(%) does?

http://www.cprogramming.com/tutorial/modulus.html

you can use % to check if number is dividable by 3 and if so don't add it up, in another words add it to the final result if it is not dividable by 3
Thnx it worked my final code got way shoter. Also i learned a whole lot from you.
Great job:)
Gromeer wrote:
Thnx it worked my final code got way shoter.


Hi,

Are you aware that there is a simple formula to work out the sum of integers? Google that :+) The entire code for this problem is about 10 lines, the heart of it is only 3 lines.

Realise with project Euler, brute force is not the way to go, often the numbers are too big or it takes too long - so one has to think of a clever way of solving the problem. One big clue, often, is to think of ways to reduce the size of the problem set in some way - the less there is to do, the easier & quicker it will be.
Last edited on
Topic archived. No new replies allowed.