-
Notifications
You must be signed in to change notification settings - Fork 0
/
035.c
74 lines (56 loc) · 1.12 KB
/
035.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
// How many circular primes are there below one million?
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
#include <stdlib.h>
int main() {
bool isPrime(long n);
bool rotationsArePrime(long n);
int count = 0;
for (long i = 2; i < 1000000; i++) {
if (isPrime(i) && rotationsArePrime(i)) {
count++;
}
}
printf("%i\n", count);
return 0;
}
bool isPrime(long n) {
int start;
if (n <= 1) {
return false;
}
start = sqrt(n);
for (int i = start; i > 1; i--) {
if (n % i == 0) {
return false;
}
}
return true;
}
bool rotationsArePrime(long n) {
void shiftDigits(char arr[]);
bool isPrime(long n);
char arr[7];
sprintf(arr, "%li", n);
int digit_count = floor(log10(n) + 1);
for (int i = 1; i < digit_count; i++) {
shiftDigits(arr);
if (!isPrime(atol(arr))) {
return false;
}
}
return true;
}
void shiftDigits(char arr[]) {
char first_digit;
int i = 0;
first_digit = arr[0];
// Shift array
while (arr[i + 1] != '\0') {
arr[i] = arr[i + 1];
i++;
}
arr[i] = first_digit;
arr[++i] = '\0';
}