-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path51.c
69 lines (64 loc) · 1.92 KB
/
51.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
/**
* By replacing the 1st digit of *3, it turns out that six of the nine possible
* values: 13, 23, 43, 53, 73, and 83, are all prime.
*
* By replacing the 3rd and 4th digits of 56**3 with the same digit, this
* 5-digit number is the first example having seven primes among the ten
* generated numbers, yielding the family: 56003, 56113, 56333, 56443, 56663,
* 56773, and 56993. Consequently 56003, being the first member of this family,
* is the smallest prime with this property.
*
* Find the smallest prime which, by replacing part of the number (not
* necessarily adjacent digits) with the same digit, is part of an eight prime
* value family.
*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include "helpers.h"
#include "primes.100k.h"
int main(int argc, const char *argv[])
{
int i, j, k, l;
int count = 0;
int magnitude;
int tmp0, tmp1, tmp2;
int *digits = calloc(10, sizeof(int));
int mark = 0;
char buf0[10];
int family_size;
for (i = 0; i < PRIME_COUNT; i++) {
tmp0 = PRIMES[i];
while (tmp0) {
digits[tmp0 % 10]++;
tmp0 /= 10;
}
for (j = 0; j < 10; j++) {
if (digits[j] > 1) {
family_size = 1;
for (k = 1; k < 10; k++) {
sprintf(buf0, "%d", PRIMES[i]);
magnitude = (int) log10(PRIMES[i]) + 1;
for (l = 0; l < magnitude; l++) {
tmp1 = buf0[l] - 48; // 48 is ascii value for '0'
if (tmp1 == j) {
buf0[l] = ((tmp1 + k) % 10) + 48;
}
}
sscanf(buf0, "%d", &tmp2);
// Check magnitude in case a 0 was most significant
// making a smaller number but still prime
if ((int) log10(tmp2) + 1 == magnitude && prime(tmp2)) {
family_size++;
}
}
if (family_size == 8) {
printf("%d\n", PRIMES[i]);
return 0;
}
}
digits[j] = 0;
}
}
return 0;
}