-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
180 lines (151 loc) · 4.5 KB
/
main.c
File metadata and controls
180 lines (151 loc) · 4.5 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
void printArray(int* arr, int length) {
printf("[ ");
for (int i = 0; i < length; i++) {
printf("%d ", arr[i]);
}
printf("]\n");
}
int factorial(int x){
if (x==0||x==1){
return 1;
}
return x * factorial(x-1);
}
void get_all_permutatins_inner(
int* digits, int digits_length,
int* rotation, int rotation_length,
int** rotations, int* rotations_length)
{
if (digits_length == 1) {
rotation[rotation_length] = digits[0];
rotations[*rotations_length] = malloc((rotation_length + 1) * sizeof(int));
memcpy(rotations[*rotations_length], rotation, (rotation_length + 1) * sizeof(int));
(*rotations_length)++;
return;
}
for (int i = 0; i < digits_length; i++) {
int* new_rotation = malloc((rotation_length + 1) * sizeof(int));
memcpy(new_rotation, rotation, rotation_length * sizeof(int));
new_rotation[rotation_length] = digits[i];
int* new_digits = malloc((digits_length - 1) * sizeof(int));
int index = 0;
for (int j = 0; j < digits_length; j++) {
if (j != i) {
new_digits[index++] = digits[j];
}
}
get_all_permutatins_inner(new_digits, digits_length - 1,
new_rotation, rotation_length + 1,
rotations, rotations_length);
free(new_rotation);
free(new_digits);
}
}
void get_all_permutations(int* digits_in, int digits_length_in, int*** rotations_out, int* rotations_length_out) {
int** rotations = malloc(factorial(digits_length_in) * sizeof(int*));
get_all_permutatins_inner(digits_in, digits_length_in, NULL, 0, rotations, rotations_length_out);
*rotations_out = rotations;
}
void get_all_circulations(int* digits_in, int digits_length_in, int*** rotations_out, int* rotations_length_out) {
int** rotations = malloc(digits_length_in * sizeof(int*));
for (int i = 0; i < digits_length_in; i++) {
int* rotation = malloc(digits_length_in * sizeof(int));
for (int j = 0; j < digits_length_in; j++) {
int index = (j + i) % digits_length_in;
rotation[j] = digits_in[index];
}
rotations[i] = rotation;
}
*rotations_out = rotations;
*rotations_length_out = digits_length_in;
}
int count_digits(int num) {
int count = 0;
if (num == 0)
return 1;
while (num != 0) {
num /= 10;
count++;
}
return count;
}
int intPow(int base, int exponent) {
if (exponent==0){
return 1;
}
int result = 1;
while (exponent > 0) {
result *= base;
exponent--;
}
return result;
}
int array_to_number(int* arr, int arr_length){
int result = 0;
for(int i=arr_length-1;i>=0;i--){
result += arr[i] * (intPow(10, arr_length-1-i));
}
return result;
}
bool is_prime(int num) {
if (num <= 1) {
return false;
}
if (num <= 3) {
return true;
}
if (num % 2 == 0 || num % 3 == 0) {
return false;
}
for (int i = 5; i * i <= num; i += 6) {
if (num % i == 0 || num % (i + 2) == 0) {
return false;
}
}
return true;
}
bool is_circular_prime(int number){
int digits_count = count_digits(number);
int* digits = malloc(digits_count*sizeof(int));
// Convert number to array of digits
int i = digits_count-1;
while (number != 0) {
digits[i] = number % 10;
number = number / 10;
i--;
}
int** rotations;
int rotations_length;
get_all_circulations(digits, digits_count, &rotations, &rotations_length);
bool results = true;
for (int i=0;i<rotations_length;i++){
if (is_prime(array_to_number(rotations[i], digits_count)) != true){
results = false;
break;
}
}
free(digits);
for (int i = 0; i < rotations_length; i++) {
free(rotations[i]);
}
free(rotations);
return results;
}
int main(int argc, char* argv[]) {
clock_t start = clock();
int count_circular_primes = 0;
for(int i=0; i<1000000;i++){
if (is_circular_prime(i)){
count_circular_primes++;
}
}
clock_t stop = clock();
double elapsed = (double)(stop-start)/CLOCKS_PER_SEC;
/* long elapsed = (stop-start)CL */
printf("number of circular primes<1000000 = %d (elapsed=%fs)", count_circular_primes, elapsed);
}