0
#import <cmath>
#import <iostream>
using namespace std;

int main {
    int a;
    for(int x = 600851475142; x>cmath::sqrt(600851475143); x--) {
        if (600851475143%x==0) { //is it a factor of 600851475143?
            if p(x) { //is it also a prime?
                cout << x;
                return 0;
            }
        }
    }
}

bool p(int n) {
    for(int x = n; x<cmath::sqrt(n)+1; x++) {
        if (n%x==0) {
            return false;
        }
    }
    return true;
}

Here is my code for project euler #3 (http://projecteuler.net/problem=3). I am relatively new to C++.

Basically my approach was to count down from 600851475143, test if the number was a factor of 600851475143 and then see if it was prime. If it was then return the number and exit. However, when I compile my code I get the error:

error: function definition does not declare parameters
In function 'bool p(int)':
error: 'cmath' has not been declared

Any help would be appreciated

picklebobdogflog
  • 179
  • 2
  • 11

2 Answers2

2

Looking at your code, it really seems you are at-least new to C++, if not to programming itself.

1) In C++, we use #include instead of #import

2) you need to declare a function before calling it. Something like:

bool p(int n);

before main function

3) Parenthesis is missing in main function. Should be: 'int main() {'

4) You don't need cmath:: for sqrt. Write some thing like: (int) sqrt((double)600851475143) This is because sqrt does not take integer in parameter

5) 'if p(x) {' should be 'if (p(x)) {'

6) for-loop in function is always increasing.

7) it's a good idea to calculate sqrt before for-loop instead of in for-loop.

8) Welcome to programming world!

Mraaz
  • 71
  • 2
  • Thanks, I am new to C++ and relatively new to programming in general. This helps a lot! – picklebobdogflog Apr 20 '13 at 18:27
  • Made these changes, Here is my code #include #include using namespace std; bool p(int n) { int limit = sqrt(n)+1; for(int x = n; x>limit; x++) { if (n%x==0) { return false; } } return true; } int main () { int a; for(long x = 0; x<600851475143; x++) { if (600851475143%x==0) { //is it a factor? if (p(x)) { cout << x; return 0; } } } } Now I compile fine but I get a floating point exception when I run my program – picklebobdogflog Apr 20 '13 at 18:31
  • 1
    in 6) I'm talking about for-loop in function 'p'. – Mraaz Apr 20 '13 at 18:32
  • Ok, made that x>limit, still getting the error @mraaz – picklebobdogflog Apr 20 '13 at 18:34
  • 1
    It should be x-- instead of x++ – Mraaz Apr 20 '13 at 18:37
  • Fixed that, still getting a floating point exception @mraaz – picklebobdogflog Apr 20 '13 at 18:39
  • 1
    I think, you need a minor correction in your logic. Think about it. Also, for both square root calculations, try casting to double and back to int, like I stated it 4) – Mraaz Apr 20 '13 at 18:50
0

The name of the header is cmath, but sqrt is in namespace std.

Also: you probably want to compute the square root before the loop, then test against that result:

bool p(int n) { 
    int limit = std::sqrt(n)+1;
    for (int x=n; x<limit; x++)
    // ...
}
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111