Problem with 'long' type.

Hello,

I have problem with declaring long long int. Perhaps I just don't know syntax.

I want to write a program to find the largest prime factor of composite number.

Here's code:
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
#include <iostream>
#include <math.h>

using namespace std;

long largestPrime(unsigned long long int numb) {
	long largestPrime = 1;
	if( (numb % 2) != 0) {
		for(unsigned long i = 3; i < sqrt(numb); i+=2) {
			if( (numb % i) == 0) {
				largestPrime = i;
			}
		}
	}
	else {
		for(unsigned long i = 2; i < sqrt(numb); i++) {
			if( (numb % i) == 0) {
				largestPrime=i;
			}
		}
	}
	return largestPrime;
}

int main() {

largestPrime(600851475143);

cin.get();
return 0;
}

At #27(If I remember line correctly), at :

largestPrime(600851475143);

I get compiler error:

error: integer constant is too large for ‘long’ type

However, in my function I declared numb as unsigned long long int, which should be sufficient enough. What am I doing wrong/is my compiler bad?
Last edited on
Put ULL after the number to tell the compiler it's an unsigned long long number.
largestPrime(600851475143ULL);
I did not get any compile time error!Using Visual Studio 2010
@SameerThigale
I did not get any compile time error!Using Visual Studio 2010

It means that the compiler itself shall select the appropriate type for the constant. And it also means that

largestPrime(600851475143ULL);

does not help because as it seems the compiler of the thread author does not support long long type.
Topic archived. No new replies allowed.