Note that the algorithm you've chosen:
1. Doesn't filter out non-prime divisors of num.
2. Will require over 600 billion iterations.
See if you can find a way to significantly trim down the number of iterations.
Thanks helios.
So if I plan on using a type long long, do I always declare it long long AND when include LL suffix of the number when I define the value?
Also, I don't know if I can shorten the length of the program any. It ran pretty fast on my laptop, but if you see that I could've solved the problem in a easier way, please give me a hint so I can try it.
#include <iostream>
#include <cmath>
usingnamespace std;
longlong num;
int fact;
int i;
int found;
int pfact;
int remain;
int main(){
num = 600851475143LL;
found = 0;
pfact = 1;
while (found == 0){
remain = num % pfact;
pfact = pfact + 2;
if (remain == 0){
fact = pfact - 2;
num = num / fact;
if (num == 1){
found = 1;
}
}
}
cout << "The largest prime is " << fact << endl;;
system("pause");
if it is type long long it is a given it is long long.
The only times I can think to use LL , u , L are:
1. Parameter ex:
1 2 3 4
function( int ); //proto
function( long ); //proto
function( longlong ); //proto
function( 123LL ); //calling the long long proto instead of int or long
2. using an auto type ex: constauto a = 123LL; //this is a long long type now
I could be wrong though
The factor is 71
num = 8345159376
The factor is 74
num = 111268791
The factor is 153
num = 722524
The factor is 16421
num = 43
and then hangs. It doesn't crash, it just sits there computing forever.
The output isn't even right, since 74 is obviously not prime, and the largest prime that divides the number is 6857, which is lower than 16421.
Also, I don't know if I can shorten the length of the program any.
Program length affects execution speed only indirectly.