-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprime.c
128 lines (111 loc) · 2.94 KB
/
prime.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Prime number generator using linked lists to keep track of found primes to reduce number of calculations
* Made by Joel Grunbaum
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//#include <conio.h>
#include <pthread.h>
//Swap pthread for conio if running on windows instead of linux
//Linked list
typedef struct node{
unsigned long long num;
struct node *next;
} list_t;
//Only way I could make it exit on command
int t = 0;
//Comment out for compiling on windows instead of linux
void *check(void *g){
t = getchar();
pthread_exit(NULL);
}
int main(void){
printf("start\n");
FILE *ls;
list_t * head = malloc(sizeof(list_t));
list_t * current = head;
unsigned long long lastknown = 3;
//Checks for existence of file and if exists loads into memory
if(access("PrimeList.txt", F_OK)!=-1){
ls = fopen("PrimeList.txt","a+");
unsigned long long a;
current->next = malloc(sizeof(list_t));
do{
fscanf(ls, "%llu", &a);
current->num = a;
lastknown = current->num;
current->next = malloc(sizeof(list_t));
current = current->next;
}while(fgetc(ls)!=-1);
//Checks for empty file
if(head->num != 2)
goto elist;
}
//If no file, creates new one and starts list
else{
elist:
ls = fopen("PrimeList.txt","w");
head->num = 2;
head->next = malloc(sizeof(list_t));
head->next->num = 3;
fprintf(ls, "2\n3");
lastknown=3;
}
int isprime;
//Inform loading has finished
printf("begin loop\nPRESS ENTER TO END PROGRAM");
//Comment out for compiling on windows instead of linux
//Creates second thread to listen for user input
pthread_t thread[2];
pthread_create(&thread[1], NULL, check, (void *) lastknown);
//Main loop of program, loops through odd numbers and checks of they are factors of numbers in the list
//Swap "t == 0" for "!kbhit()" when compiling on windows instead of linux
for(unsigned long long n = (lastknown+2); t==0; n+=2){
current = head;
isprime=1;
while(current != NULL){
unsigned long long a = current->num;
if(a*a>n)
break;
if(a!=0){
if(n%a==0){
isprime = 0;
break;
}
}
current = current->next;
}
//Adds new primes to list
if(isprime == 1){
current = head;
while(current->next!=NULL){
current= current->next;
}
current->next = malloc(sizeof(list_t));
current = current->next;
current->num = n;
}
}
printf("end loop\n");
//Moves list to the last known in the file and then to the one after
current = head;
while(current->num != lastknown){
current = current->next;
}
current = current->next;
//Writes new primes to the list
while(current!=NULL){
if(current->num != 0)
fprintf(ls, "\n%llu", current->num);
current = current->next;
}
//Frees the list and closes the file
while(head!=NULL){
current = head;
head = head->next;
free(current);
}
fclose(ls);
printf("k thx bai\n");
}