#include <iostream>

#include <vector>

#include <cmath>

 

using namespace std;

 

// Tests whether a function is prime

bool test_prime(int num){

	int count = 0,x=2;

	double numx = (double) num;

	// Go through all primes

	for(x=0;x<sqrt(numx);x++){

		if(num % x == 0){

			return false;

		}

	}

 

	return true;	

}

 

int main(int argc, char *argv[]){

	int num;

 

	if(argc != 2){

		cerr << "Usage: " << argv[0] << " positive-integer" << endl;

		return -1;

	}else{

		num = atoi(argv[1]);

	}

 

	bool is_prime = test_prime(num);

	if(is_prime){

		cout << num << " is prime" << endl;

	}else{

		cout << num << " isn't prime" << endl;

	}

 

}

Related posts:


blog comments powered by Disqus