测试求10的8次方内素数能力

枚举法

image-20230906221303972

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main()//求个10^8内素数玩玩
{
for(int i=2;i<pow(10,8);i++)
{
int flag=0;
for(int j=2;j<=pow(i,0.5);j++)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==1)continue;
//printf("%d\n",i);
}
printf("finish");
}

筛法

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
#include<malloc.h>
typedef struct link
{
int num;
struct link *next;
} link;

void Initlink(link * first)
{
first->num=666;
first->next=malloc(sizeof(link));
link * t=first->next;
t->num=2;
t->next=malloc(sizeof(link));
t=t->next;

t->num=3;
t->next=malloc(sizeof(link));
t=t->next;

t->num=5;
t->next=malloc(sizeof(link));
t=t->next;

t->num=7;
t->next=malloc(sizeof(link));
t=t->next;
t->next=NULL;
}

void print_all(link * head)
{
head=head->next;
while(head->next)
{
printf("%d\n",head->num);
head=head->next;

}
}

int main()//求个10^8内素数玩玩
{
link * head=malloc(sizeof(link));
Initlink(head);
int linknum=4;
int min=10;
for(int i=1; i<4; i++)
{
//printf("%d\n",panju);
link * t;
for(int j=min; j<min*min; j++)
{
t=head;
int flag=0;
t=t->next;
for(int h=0;t->num<=sqrt(j)+1;h++)
{
if(j%t->num==0)
{
flag++;
break;
}
t=t->next;
}
if(flag==0)
{
while(t->next)
{
t=t->next;
}
t->next=malloc(sizeof(link));
t->num=j;
t=t->next;
t->next=NULL;
linknum++;
}
if(j==100000)printf("%d",linknum);
}
min=min*min;
}
//print_all(head);
printf("%d",linknum);
//printf("finish");
}


计算余数(python)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
choice=input('input choice\n')
if choice=='1':
a = input('input a\n')
ea = eval(a)
print(ea)
b = input('input b\n')
eb = eval(b)
print(eb)
output = "a chu b=" + str(ea // eb) + '...' + str(ea % eb)
print(output)
input('hahaha')
else:
a1 = int(input('input a1\n'))
a2 = int(input('input a2\n'))
a = pow(a1,a2)
print(a)
b1 = int(input('input b1\n'))
b2 = int(input('input b2\n'))
b = b1 * b2
print(b)
output = "a chu b=" + str(a // b) + '...' + str(a % b)
print(output)
input('hahaha')