Last active
August 29, 2015 14:11
-
-
Save tomcha/a70082940b4edb88aec5 to your computer and use it in GitHub Desktop.
prime number c++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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