-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path041.c
58 lines (46 loc) · 998 Bytes
/
041.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
// What is the largest n-digit pandigital prime that exists?
// What is the largest n-digit pandigital prime that exists?
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
int main() {
bool isPandigital(char *arr);
bool isPrime(long n);
char seq[10];
for (long i = 7654321; i >= 1234567; i--) {
sprintf(seq, "%li", i);
if ((i % 2 != 0) &&
isPandigital(seq) && isPrime(i)) {
printf("FOUND! %li\n", i);
break;
}
}
return 0;
}
// Checks whether sequence uses digits 1 thru 7 exactly once
bool isPandigital(char *arr) {
bool used[10] = {false};
int n;
while (*arr != '\0') {
n = *arr - '0';
if (used[n] == true || n == 0 || n > 7) {
return false;
}
used[n] = true;
arr++;
}
return true;
}
bool isPrime(long n) {
int start;
if (n <= 1) {
return false;
}
start = sqrt(n);
for (int i = 2; i <= start; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}