Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

integer constant is too large for "long" type [duplicate]

Tags:

c++

dev-c++

Possible Duplicate:
long long in C/C++

Writing a simple program for a project Euler problem. Refuses to compile because "integer constant is too large for "long" type", even though it should be well within the size limits of an unsigned long long. Using the dev-c++ compiler.

code in question:

#include <iostream>

bool isprime (unsigned long long i)
{
    if(i==1||i==0) return false;
    if(i==2) return true;
    for(unsigned long long k=2;k!=i-1;k++)
    {      
        if(i%k==0) return false;
    }
    return true;
}

int main()
{
    for(unsigned long long i=600851475143;i>=0;i--) //problematic line
    {
        if(isprime(i))
        {
            std::cout<<i;
            std::cin.get();
            return 0;
        }
    }
}
like image 238
Bacu Avatar asked Apr 04 '11 16:04

Bacu


People also ask

How do you increase the size of an integer?

The only way you can "increase" an int foo; (not a long foo; , int is not a long) is: You are compiling with Turbo-C or a legacy 16-bit DOS compiler on a modern computer, likely because your university requires you to use that, because that's what your professor knows.


2 Answers

Try an "ULL" suffix: 600851475143ULL

like image 96
Fred Larson Avatar answered Oct 11 '22 19:10

Fred Larson


Your literal as typed has type int which isn't big enough to hold the value. Try 600851475143ULL as a first fix.

Note even with that, your for loop will never terminate since an unsigned can never be less than 0. Instead, use a long long and 600851475143LL.

like image 42
Mark B Avatar answered Oct 11 '22 19:10

Mark B