#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cmath>
usingnamespace std;
int check(unsignedlonglong x)
{
for(unsignedlonglong y=(--x);y>0;--y)
{
if(y==1 && x%y==0)
{
return 1;
}
elseif(x%y!=0 && y!=1)
{
continue;
}
else
{
return 0;
}
}
}
int main()
{
system("cls")
/*Program to calculate the highest prime factor of given digit*/;
unsignedlonglong srt, num=600851475143;
srt=num;
for(unsignedlonglong x=srt;x<0;--x)
{
if(num%x==0)
{
if(check(x)==1)
{
cout<<x;
break;
}
else
{
continue;
}
}
else
{
continue;
}
}
cout<<"Program made by TheSherlockHolmie";
cout<<endl;
return EXIT_SUCCESS;
}
As can be seen, I used a for loop to find the factors of the number. To check if the factor obtained is prime or not, I am using a function check(), i made. I was expecting the output to give out the factor, but the only thing that appears is "Program made by TheSherlockHomie". I am using Dev C++ 5.8.2 on gcc 4.8.1 64 bit release. Pointers on the code will be gratefully appreciated.
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cmath>
usingnamespace std;
int check(unsignedlonglong x)
{
for(unsignedlonglong y=(x-1);y>0;--y)
{
if(y==1 && x%y==0)
{
return 1;
}
elseif(x%y!=0 && y!=1)
{
continue;
}
else
{
return 0;
}
}
}
int main()
{
system("cls")
/*Program to calculate the highest prime factor of given digit*/;
unsignedlonglong srt, num=600851475143;
srt=num;
for(unsignedlonglong x=srt;x<0;--x)
{
if(num%x==0)
{
if(check(x)==1)
{
cout<<x;
break;
}
else
{
continue;
}
}
else
{
continue;
}
}
cout<<"Program made by TheSherlockHolmie";
cout<<endl;
return EXIT_SUCCESS;
}
But even after this, there is no output.
@ne555, the check() function checks if the number is prime by dividing it with every value below it upto 1,and checking the remainder(x%1 does not count). If the number is prime, it returns value 1, and if it is not, returns 0.
@Delshire, the program does have a cout in line 37, as @MiiNiPaa said.
@MiiniPaa, thanks for your feedback. And thanks for the code, but it is not running. What approach/algorithm do you suggest to run this thing? Give me a hint, because I really want to do this myself, being a beginner notwithstanding.
@DyavolskiMost, yeah, it will. It was given to me as a problem, and I am required to give the answer. Luckily, there is no time limit, but I really want to solve it. I know that there is a very efficient algorithm that i am not able to think of.
Thanks all.
P.S. I am going on a holiday, so might take a bit long to reply to the next post.
It should run as it is essentually your code without excess parts and slightly optimised. It just takes ungodly amount of time to finish.
I suggest you to do prime number factorisation: find first divisor of the number (it will be prime by definition), then divide number by it until it cannot be divided anymore. Then find next divisor...
Last found divisor will be largest prime divisor for this number. Alternatively you can save all divisor in vector to do something with them