project euler # 3

Alright, so I was doing some project Euler stuff and am on # 3. My code seems to work for other numbers, but when I do the number required ( 600851475143) I get a different answer each time and none of them have been the correct one :(

*edited out code so others get to try it too!*
Last edited on
I think that an integer number can not store such big value as

600851475143

Maybe you should use another type instead int. For example long long.
Success! Thanks so much! I had tried long before and it didn't work. Long int doesn't work either, but long long does. What exactly is the difference between all of those?
long and long int are the same ( the int is implied as it is with others like short and unsigned)

long can hold a bigger number than int, and long long can hold an even bigger number again.

Google "C++ data types" and read all about it.

Long int won't work because of the capital 'L' and is a syntax error.

I hope that now you have learnt it is good to be aware of what the limits are for the types you are using.

Good Luck with your future programs !! :)
long can hold a bigger number than int

Nope, not always! It's only on some really old system with a 16-bit int. Otherwise, they're the same. Short is ALWAYS 16, long is LAWAYS 32, and long long is ALWAYS 64 bits, and int can vary, but it's a very reasonable assumpiton that it's 32 bits.
Thanks guys!
Topic archived. No new replies allowed.