# Project Euler 3

• Jan 11th 2013, 01:05 PM
Hazique35
Project Euler 3
Hello, I am really confused on this problem on projecteuler. Its so annoying!!! And its only problem 3! Im using C++ and I am really confused on how to find a technique to find whether a number is prime. My method at the moment is increasing the number that the number I am dividing it by. (600851475143). This method isnt working because I cant really test to see whether a number has more than one decimal point. (Like when 600851475143 / 3. I can use a floating point number because it cannot fit a number that big. Also rounding in C++ isnt that accurate.) Heres an example of some code that I use.

for (int i=0; i <99999; i++);
//code to divide number

Sorry if this isnt that clear... I just need some hints to push me in the right direction.

Thanks!
• Jan 11th 2013, 07:09 PM
zzephod
Re: Project Euler 3
Quote:

Originally Posted by Hazique35
Hello, I am really confused on this problem on projecteuler. Its so annoying!!! And its only problem 3! Im using C++ and I am really confused on how to find a technique to find whether a number is prime. My method at the moment is increasing the number that the number I am dividing it by. (600851475143). This method isnt working because I cant really test to see whether a number has more than one decimal point. (Like when 600851475143 / 3. I can use a floating point number because it cannot fit a number that big. Also rounding in C++ isnt that accurate.) Heres an example of some code that I use.

for (int i=0; i <99999; i++);
//code to divide number

Sorry if this isnt that clear... I just need some hints to push me in the right direction.

Thanks!

Use double (the default floating type for most compilers). That will allow 14 digits of integer arithmetic.

.
• Jan 12th 2013, 05:40 AM
Hazique35
Re: Project Euler 3
hmmm... Ive tried that. (probably made a mistake). Ill try it again though.

Thanks!
• Jan 12th 2013, 07:03 AM
Hazique35
Re: Project Euler 3
I'm trying double and it is big enough to hold the number, but Im still having trouble creating the for loop.
• Jan 20th 2013, 05:49 AM
a tutor
Re: Project Euler 3
Post your code if you're still stuck.
• Jan 20th 2013, 06:55 AM
janvdl
Re: Project Euler 3
Quote:

Originally Posted by Hazique35
Hello, I am really confused on this problem on projecteuler. Its so annoying!!! And its only problem 3! Im using C++ and I am really confused on how to find a technique to find whether a number is prime. My method at the moment is increasing the number that the number I am dividing it by. (600851475143). This method isnt working because I cant really test to see whether a number has more than one decimal point. (Like when 600851475143 / 3. I can use a floating point number because it cannot fit a number that big. Also rounding in C++ isnt that accurate.) Heres an example of some code that I use.

for (int i=0; i <99999; i++);
//code to divide number

Sorry if this isnt that clear... I just need some hints to push me in the right direction.

Thanks!

I have a feeling that your way of checking primes might be inefficient. Perhaps not, but you never know. I've attached my code.

Use unsigned long longs for the big numbers. Your number returns 0, i.e. false. It's not a prime. ;)

Code:

```#include <iostream> #include <math.h> #include <stdio.h> using namespace std; bool isPrime(unsigned long long n) {     unsigned long long root = floor(sqrt(n)) + 1;     for (unsigned long long i = 2; i <= root; i++)     {         if (n % i == 0)             return false;     }     return true; } int main() {     bool Prime = isPrime(600851475143ULL);     cout << Prime << endl;     return 0; }```