-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem429_omp.c
executable file
·89 lines (81 loc) · 2.32 KB
/
problem429_omp.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
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<omp.h>
#define TRUE 1
#define FALSE 0
typedef struct unitary_divisor{
unsigned long num;
unsigned int index;
unsigned int is_prime;
struct unitary_divisor *next, *prev;
} UD;
UD *make_ud(unsigned long n){
UD *p;
p = (UD*)malloc(sizeof(UD));
p->num = n;
p->index = 1;
p->is_prime = TRUE;
p->next = NULL;
}
unsigned long pow_modn(unsigned long base, unsigned int index, unsigned long n){
unsigned long ans = 1;
while(index > 0){
ans = (ans * base) % n;
index--;
}
return ans;
}
void calculate_index(UD * ptr, unsigned long max){
unsigned max_index, i, num = ptr->num;
max_index = (unsigned)floor(log(max)/log(ptr->num));
/* printf("(%lu)%lu->%u,", max, ptr->num, max_index); */
for(i = 1 ; i <= max_index ; i++){
num *= ptr->num;
ptr->index += (max / num);
printf("\nptr->i:%u,ptr->n:%lu,max:%lu", ptr->index, ptr->num, max);
}
return;
}
int main(int argc, char *argv){
unsigned long ans = 1,max,i, mod_constant = 1000000009;
UD *bottom, *trav1, *trav2, *temp, *prev;
printf("Enter the number whose factorial you want to factorize:");
scanf("%lu", &max);
printf("Initializing ..");
trav1 = bottom = make_ud(2);
for(i = 3 ; i <= max; i += 2){
trav1->next = make_ud(i);
trav1->next->prev = trav1;
trav1 = trav1->next;
}
printf("Done\nFactorizing & calculating .. ");
calculate_index(bottom, max);
i = (1 + pow_modn(bottom->num, 2 * bottom->index, mod_constant)) % mod_constant;
ans = ((ans * i) % mod_constant);
printf("\n%lu, %u :> %lu :> %lu, ", bottom->num, bottom->index, i, ans);
trav1 = bottom->next;
while(trav1 != NULL){
trav2 = trav1->next;
prev = trav1;
#pragma omp parallel
{
while(trav2 != NULL){
if((trav2->num) % (trav1->num) == 0){
prev->next = trav2->next;
trav2->is_prime = FALSE;
} else
prev = trav2;
trav2 = trav2->next;
}
}
calculate_index(trav1, max);
i = (1 + pow_modn(trav1->num, 2 * trav1->index, mod_constant)) % mod_constant;
ans = ((ans * i) % mod_constant);
printf("\n%lu, %u :> %lu :> %lu, ", trav1->num, trav1->index, i, ans);
while(trav != NULL && !trav->is_prime) trav1 = trav1->next;
}
printf("\n..Done\n");
printf("The required answer is %lu\n", ans);
return 0;
}