Skip to content

Instantly share code, notes, and snippets.

@tomcha
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomcha/a70082940b4edb88aec5 to your computer and use it in GitHub Desktop.
Save tomcha/a70082940b4edb88aec5 to your computer and use it in GitHub Desktop.
prime number c++
#include <iostream>
#include <vector>
#include <time.h>
#include <float.h>
#include <math.h>
using namespace std;
//int checkPrime(int num, vector<int> array);
int checkPrime(int num, vector<int> &array);
int main(){
clock_t start = clock();
cout << 2 << endl;
cout << 3 << endl;
int num = 5;
vector<int> array;
array.push_back(2);
array.push_back(3);
int i = 1;
while(i < 100000){
int checkedNum;
checkedNum = checkPrime(num, array);
if(checkedNum != 0){
cout << checkedNum << endl;
}
num += 2;
i++;
}
clock_t stop = clock();
cout << (double)(stop - start) / CLOCKS_PER_SEC * 1000 << "ミリ秒" << endl;
}
//int checkPrime(int num, vector<int> array){
int checkPrime(int num, vector<int> &array){
for(int i =0; i < array.size(); i++){
if((num % array[i]) == 0){
return 0;
}
if((double)array[i] > sqrt((double)num)){
break;
}
}
array.push_back(num);
return num;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment