Error with the algorithm for finding prime numbers

closed account (zbfzwbRD)
I want to find all the prime numbers in the given numbers, but I can't.

For example
8
3 4 5 7 9 -9 -7 0

the output should be
4

and I have 5


#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;

bool IsPrime(int x){
x = abs(x);
if(x == 2){
return true;
}

bool test = true;
for(int i=2; i*i<=x; i++){
if(x%i==0){
test = false;
break;
}else{
test = true;
}

}
return test;
}

int main(){
int n;
cin >> n;
vector<int> v;
for(int i=0; i<=n; i++){
int x;
cin >> x;
v.push_back(x);
}
int cnt = count_if(v.begin(), v.end(), IsPrime);
cout << cnt;

}
closed account (zbfzwbRD)
okm i got it, i will try it now
Topic archived. No new replies allowed.