-
Notifications
You must be signed in to change notification settings - Fork 18
/
exp7_1判定素数.cpp
46 lines (41 loc) · 897 Bytes
/
exp7_1判定素数.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
//实验7_1
//判定素数
//试商法判定素数
bool IsPrime_1(int n){
int i = 2;
for (;i<n;i++){
if (n%i == 0)
return true;//不是素数
}
return false;//是素数
}
//缩小试商法的判定范围
bool IsPrimer_2(int n){
int i = 2;
for (;i<sqrt(n);i++){
if (n%i == 0)
return true;//不是素数
}
return false;//是素数
}
int main(){
int n;
cin >> n;
if (IsPrime_1(n))
cout << n << " is not a prime number." << endl;
else
cout << n << " is a prime number." << endl;
cout << "-----------------------------------" << endl;
if (IsPrime_1(n))
cout << n << " is not a prime number." << endl;
else
cout << n << " is a prime number." << endl;
return 0;
}