-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp041.cpp
57 lines (46 loc) · 1.05 KB
/
p041.cpp
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
// Pandigital prime
//
// https://projecteuler.net/problem=41
#include <iostream>
#include <algorithm>
#include <cstdint>
#include <cmath>
#include <cinttypes>
#include <cstring>
bool is_prime(uint32_t i)
{
if (i == 2) return true;
if (i % 2 == 0) return false;
uint32_t n = (uint32_t) sqrt(i);
for (uint32_t j = 3; j <= n; j += 2)
{
if (i % j == 0) return false;
}
return true;
}
uint32_t check(char *chiffres, size_t nb)
{
do {
uint32_t n = (uint32_t) strtoul(chiffres, NULL, 10);
if (is_prime(n))
return n;
} while (std::prev_permutation(chiffres, chiffres + nb));
return 0;
}
int main()
{
const char chiffres[] = "987654321";
for (size_t n = 0; n < 9; ++n)
{
char work[16];
// supprime les n premiers chiffres et le \0 final (pour strtoul)
memmove(work, chiffres + n, (9 - n) + 1);
uint32_t m = check(work, 9 - n);
if (m != 0)
{
std::cout << m << std::endl;
break;
}
}
return 0;
}