When unsigned long long isn't long enough

Aug 15, 2011 at 3:32am
I am trying to solve a project euler problem just so I can get more experience. The problem states:
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?

However unsigned long long or unsigned double isn't big enough to hold this number to do calculations on it.
I using Dev-C++ as my IDE. What can I do to work with this number?


My code (if anybody is interested) is:
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;


    unsigned long long subject = 600851475143;
    bool isPrime(long long);
    bool isFactor(long long , long long);
    
    
int main()
{

    long long placeA = 0;
    long long placeB = 0;
    long long counter = 1;
    
    while(counter < subject){
                      
    placeB = placeA;
    if(isFactor(counter, subject) && isPrime(counter))              
                  placeA = counter;
                  
    counter++;
    //    cout << placeA << " " << placeB << " " << counter << endl;            
    }
    cout << fixed << setprecision(0);
    cout << placeA << " " << placeB << " " << counter << endl;

}


bool isFactor(long long numA, long long subjectA)
{
     if(0 == (subjectA % numA))

        return true;
     else
         return false;

}

bool isPrime(long long num)
{

    long long denom = 2;
    bool prime = true;
    if (num == 4)
       prime = false;
          while (denom < num/2){ 
                
                if(0 == (num % denom)){
                     prime = false;
                }                 
                                  
                if(prime == false)
                return false;
                                             
                denom = denom + 1;
                
          }
        return true;
    
}
Aug 15, 2011 at 3:47am
Grab a bignum library from somewhere.
Aug 15, 2011 at 3:49am
but that doesn’t help him with experience...
Aug 15, 2011 at 3:54am
@captdrizzle: what exactly is the problem you're experiencing? unsigned long long (and long long for that matter) is big enough to store 600851475143 and to calculate its prime factors. Likely there's a problem with your algorithm.
Last edited on Aug 15, 2011 at 3:55am
Aug 15, 2011 at 4:00am
I though long longs only work on 64 bit cpu...
Aug 15, 2011 at 4:02am
With the code above, Dev-C++ gives me the error message:

8 C:\CPP\Euler3\main.cpp integer constant is too large for "long" type

If I put in a smaller number it works just fine.

I tried using a bignum library, but I am unsure of how to implement it.
Last edited on Aug 15, 2011 at 4:05am
Aug 15, 2011 at 4:14am
Apparently, for Dev-C++, you need to use unsigned long long int or long long int.

http://gcc.gnu.org/onlinedocs/gcc/Long-Long.html
Last edited on Aug 15, 2011 at 4:16am
Aug 15, 2011 at 4:22am
I get the same error message with unsigned long long int. I was able to solve the euler problem by taking the number as input into a unsigned long long type instead of declaring it with "subject".
Problem aside, does anybody know what caused this? If I want to work with exceptionally large numbers in the future do I have to take them as user/file input?
Last edited on Aug 15, 2011 at 4:23am
Aug 15, 2011 at 4:25am
Maybe this will work?

unsigned long long /*int*/ subject = 600851475143ULL;

This is a little strange for me because, using Visual Studio, I can just do this no problem:

unsigned long long subject = 600851475143;
Last edited on Aug 15, 2011 at 4:37am
Aug 15, 2011 at 4:58am
Adding the ULL to the end of the number did it. I wonder why that is. Either way, thank you! ULL is just one of those tricks I will have to remember.
Aug 15, 2011 at 4:59am
ULL is a suffix that tells the compiler "interpret the number as an unsigned long long integer" instead of a normal, as it seemed to be (which apparently it was truncated before assigning it).
Topic archived. No new replies allowed.