Project Euler Problem 3
Dec 20, 2012 at 7:02pm UTC
I keep getting an error but I think its a stupid mistake which I cannot find o.o just started so... any help is appreciated.
Problem :
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
found it just had to add ULL suffix
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
#include <iostream>
using namespace std;
bool isprime(long long n);
long long fbp (long long n);
int main()
{
//=============================================================================
/* Euler 1
sum=0;
for(int i=0;i<1000;i++)
{
if(i%3==0 ||i%5==0 )
{
sum=sum+i;
}
}
cout<< sum;
*/
//=============================================================================
//=============================================================================
/* Euler 2
int current, next, total,temp;
current =1;
next = 2;
total=0;
while(total<4e6)
{
if(current%2==0)
{
total+=current;
}
temp= current;
current = next;
next = temp+current;
}
cout << total << endl;
*/
//=============================================================================
unsigned long long lpf = 600851475143;
cout << fbp(lpf) << endl;
return 0;
}
long long fbp(long long n)
{
long long factor;
for (long long i = 3; i <= n; i += 2)
{
if (n % i == 0)
{
if (isprime(i))
{
factor = i;
cout << factor << endl;
}
}
}
return factor;
}
bool isprime(long long n)
{
if (n%2== 0)
{
return 0;
}
for (long long i = 3; i <= ((n/2)+1);i +=2)
{
if (n%i== 0)
{
return 0;
}
}
return 1;
}
Last edited on Dec 20, 2012 at 7:30pm UTC
Topic archived. No new replies allowed.