[Solved] Euler Project – Greatest prime factor computational power can’t handle my script [closed]


Following up on my suggestion in a comment – re-work your algorithm to reduce the amount of number crunching:

#include <iostream>
#include <math.h>

int main()
{
    const long long int num_in = 600851475143;
    long long int curr = num_in;
    while(true){
        const long long int sr = static_cast<long long int>(sqrt(static_cast<long double>(curr)));
        long long int i = 2;
        for(; i <= sr; ++i){
            if(curr % i == 0){
                curr /= i;  // get rid of this prime
                break;      // and start the loop again
            }
        }
        if(i >= sr){
            // couldn't divide anymore
            break;
        }
    }
    std::cout << num_in << "'s largestprime factor is: " << curr << std::endl;
}

The code above outputs:

600851475143's largestprime factor is: 6857

Note, that you can use good-old C-style typecasting to shorten the long line with the sqrt() call in it, like so:

const long long int sr = (long long int)sqrt((long double)curr);

solved Euler Project – Greatest prime factor computational power can’t handle my script [closed]