project eulor 1

http://projecteuler.net/problem=1

I did problem 1 in python and got the correct answer, 233168.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
metulburr@ubuntu:~$ python3
Python 3.3.1 (default, Apr 17 2013, 22:30:32) 
[GCC 4.7.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> lister = []
>>> for i in range(1000):
...     if i % 5 == 0:
...             lister.append(i)
...     elif i % 3 == 0:
...             lister.append(i)
... 
>>> sum(lister)
233168
>>> 

Using the same strategy in c++ i get a different answer, 234168. I am not sure what i did to get a different answer?
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
#include <iostream>
#include <vector>

using namespace std;

int main(){
	
	vector<int> multiples;
	int total = 0;
	
	for (int i=1; i<=1000; i++){
		if (i % 3 == 0){
			multiples.push_back(i);
		}
		else if(i % 5 == 0){
			multiples.push_back(i);
		}
	}
	
	for (int i=0; i < multiples.size(); i++){
		total += multiples[i];
	}
	
	cout << total << endl;
}
i figured it out. I had included 1000 in the for loop, whereas python range does not by default:
for (int i=1; i<1000; i++){
Topic archived. No new replies allowed.