Largest Prime Factors

What is the largest prime factor of the number 600851475143 ?

There are some answers online, but I want to solve it myself. Can somebody please help me out? This is what I have. If I replace 600851475143 by 1000 (for example), it works fine until 2 divides 1000 to 500, 250, 125...but when it has to find the next divisible prime (ie 5) it cannot do that and the program stops working.

Please Help me out!

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<iostream>
using namespace std;
bool find_prime(int number)
{
    int factor, num;
    factor = 2;
    num = number;
    while(factor<num)
    {
        if(num % factor == 0)
        return false;

        factor++;
    }
    return true;
}
int is_prime(int number)
{
    unsigned long long prime;
    while(prime != true)
    {
        prime = find_prime(prime);
        if (prime == true)
        return prime;
        if (prime == false)
        prime++;
    }
}
int main()
{
    unsigned long long number;
    int prime_divisor, largest;
    number = 1000;
    prime_divisor = 2;
    largest = 0;
    while (number!=0)
    {
        if(number % prime_divisor == 0)
        {
            number = number/prime_divisor;
            cout<<number<<endl;
            if (prime_divisor>largest)
                 largest = prime_divisor;
            prime_divisor = 2;
        }
        else
        prime_divisor = is_prime((prime_divisor+1));

    }
    return 0;
}



Last edited on
You might begin by actually using the value fed to is_prime (and why is it an int? Planning on feeding some negative numbers to it?)
Topic archived. No new replies allowed.