-
Notifications
You must be signed in to change notification settings - Fork 0
/
primecheck.c
105 lines (86 loc) · 2.01 KB
/
primecheck.c
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* Checking function for a prime number generator
* By Joel Grunbaum
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
unsigned long long num;
struct node *next;
} llist;
int main(void){
FILE *ls;
ls = fopen("PrimeList.txt","r");
if(ls == NULL)
exit(1);
unsigned long long num, last;
llist *head = malloc(sizeof(llist));
llist *current = head;
unsigned long long listlen1, listlen2, listlen;
current->num = fscanf(ls, "%llu", ¤t->num);
//Load file into memory
do{
current->next = malloc(sizeof(llist));
current = current->next;
fscanf(ls, "%llu", ¤t->num);
if(last<current->num)
last = current->num;
listlen1++;
}while(fgetc(ls)!=-1);
//IDK why but it alternates between listlen1 and listlen2 for which is accurate
current = head;
while(current!=NULL){
current = current->next;
listlen2++;
}
listlen1--;
if(listlen2<listlen1)
listlen = listlen2;
else
listlen = listlen1;
current = head;
printf("starting to check\n");
int count1 = 0, count2 = 0;
printf("checking for non-primes in list\n");
do{
num = current->num;
for(unsigned long long i = 2; i*i<=num; i++){
if(num%i == 0){
printf("%llu isn't prime!\n", num);
count2++;
break;
}
}
current = current->next;
}while(current != NULL);
printf("checking for missing primes\n");
for(unsigned long long i = 2; i<=last; i++){
int k=0;
for(unsigned long long j = 2; j*j<=i; j++){
if(i%j==0){
k++;
}
}
if(k==0){
current = head;
while(current != NULL){
if(current->num!=i){
k++;
}
current = current->next;
}
if(k < listlen){
printf("%d\n", k);
printf("%llu is a prime not in the list\n", i);
count1++;
}
}
}
printf("finished check, there were %d non primes in the list and %d primes out of the list\n", count2, count1);
while(head!=NULL){
current = head;
head = head->next;
free(current);
}
fclose(ls);
}