Reputation: 1573
I am pretty new to c++ but I am trying to set up a really simple c++ project. Below you can find the code so far. But what I am having trouble with is the math function p = n^2-8n+7;
. Any suggestions how I could improve it??
#include <iostream>
#include <stdio.h>
using namespace std;
int main() {
/* Variable Declaration */
int p,n,i;
for (n=0; n<100; n++) {
/* Math Function */
p = n^2-8n+7;
/* Check if prime */
for (i=2; i<p; i++) {
if (!(p%i)) break;
else cout << "(" << n << "," << p << ");" << endl;
}
}
/* Ready */
printf("\n\n\a");
system("pause");
}
Upvotes: 0
Views: 1753
Reputation: 24433
You need to change
n^2-8n+7;
to
p = n * n - 8 *n + 7;
You are missing the * for multiplication and also instead of ^ you either need to mutliply two times or use pow function
Upvotes: 8
Reputation: 2598
The operator ^ is not equal to potency, but rather the binary operator 'XOR'.
For n^2 either use (n*n) or, for higher potencies, the premade 'pow' function, which is described here: http://www.cplusplus.com/reference/clibrary/cmath/pow/
For your example, that would be pow(n, 2); 8n doesn't work either, you need to write it fully as '8 * n'.
Upvotes: 5
Reputation: 5341
To get square use n*n
instead of n^2
. ^
is the bitwise xor operator.
Upvotes: 6