#include <iostream> using namespace std; int prim( long long x ) { int s = 0; for( long long i = 1; i <= x ; i++ ) { if( x % i == 0 ) { s++; } } if( s == 2 ) { return 1; } return 0; }
int main() { long long A = 600851475143; long long i = 2; long long C = 0; while( i < (A/2) ) { while( A % i == 0 ) { A = A / i; if( i > C ) { C = i; } } i++; } if( prim(C) ) { cout<<C; } return 0; }
This is the code I made for Project Euler problem 3 . I donโt understand why, when I run it, it gives me 1471. This is a good answer, but not the biggest one. But if I change i = 1471 , it will give the correct answer 6857 ... Where is the problem? Why doesn't this โautomaticallyโ give me a good answer 6868, but 1471 when I start with 2?
PS. I know that I do not need to use long long everywhere.
source share