How to code Euler #3 for any value?
<pre><code>/*
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
*/

#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;cmath&gt;

using namespace std;


int main()
{
    unsigned long num = 600851475143;
    unsigned long i;
    long double root = sqrt(num);
    long double tempRoot = 0;
    unsigned long factor = 0;
    unsigned long largest = 0;

    for (i=2; i&lt;root; i++)
    {
        if (num%i == 0)
        {   
            num = num/i;
            factor = i;
            cout &lt;&lt; factor &lt;&lt; endl;

            if (factor &gt; largest)
                {
                    largest = factor;
                }
        }
    }

    cout &lt;&lt; largest &lt;&lt; endl;    

return 0;
}
</code></pre>

<p>This solution works because coincidentally the factors of 600851475143 are all prime numbers. But when debugging the code I was inputting various values for the variable num (=600851475143). For example, when I input 135 it showed me all the factors, including the non-prime ones. How do I add a prime number checker for the factors? I tried using the same method that I used here within a nested if, but failed miserably. Any help would be appreciated.</p>

<p>Also, please indicate if I am using unnecessarily large variable types in case of some variables.</p>

<p>Thanks.</p>
