I have reduced it for lower numbers but for some reason with higher numbers when i get the program to out put all the primes they are in minuses!!!
i need a way to make this even faster, or maybe theres a better mathematical answer, I know that for numbers in their 1,000,000,000,000 have an average of 1 prime every 26.6 numbers (i think thats right there is 37,607,912,018 primes in the number 1,000,000,000,000) is there anyway i can utilize this??
Interesting, the project euler problem is the largest prime factor of that number, i was finding the next nearest prime number to that number, however, i would like to continue my original challenge, but i suspect that it will still take a long time to find the nearest prime number but will be quite easy to find the highest prime factor what with all the doubling increasing of P
int main()
{
//initialize some long ass integers
longlong primeanswer = 1,p = 1,sqrt_of_i;
//flag for counting a discovery of a factor
int factorcount = 0;
//for loop counts down in twos from an odd number i is number to check
for (longlong i=600851475145;i>0; i=i-2)
{
//reset factor count flag to zero
factorcount =0;
//squaring i means that finding a factor of i wont take as long
sqrt_of_i = sqrt(i);
//loop usues p to get numbers fo checking against square of i
for(longlong p=1; p<=sqrt_of_i; p++ )
{
//if there is a remainder of 0 when diveded...
if((i%p)==0)
{
//then a factor has been found, the integer is incremented
factorcount++;
//if there is more than two factors the we are not dealing with a prime
if(factorcount>=2)
{
break;
}
}
}
//if we found only one factor (1) then we output the prime :D
if(factorcount<=1)
{
primeanswer = i;
cout<<primeanswer<<" ";
}
}
}
took me a while, i never knew that squaring i would make it that much faster