Project Euler #51: Prime digit replacements (C++)

Question
By replacing the 1st digit of the 2-digit number *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.

Answer : 121313

Hacker Rank Problem

Solution
 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
#include <vector>
#include <string>
#include <map>
#include <iostream>
#include <cmath>

unsigned int N = 7;
unsigned int K = 3;
unsigned int L = 7;
std::map<std::string, std::vector<unsigned int>> M;
unsigned int smPrime = 99999999;

void match(unsigned int num, std::string& regex, unsigned int dig, unsigned int howOften, unsigned int startPos = 0) {
    char asDig = dig + '0';
    for(unsigned int i = startPos; i < N; i++) {
        if(regex[i] != asDig) {
            continue;
        }
        if(i == 0 && asDig == '0') {
            continue;
        }
        regex[i] = '.';
        if(howOften == 1) {
            auto& addTo = M[regex];
            addTo.push_back(num);
            if(addTo.size() >= L && addTo.front() < smPrime) {
                smPrime = addTo.front();
            }
        } else {
            match(num, regex, dig, howOften - 1, i + 1);
        }
        regex[i] = asDig;
    }
}

int main() {
    std::cin >> N >> K >> L;
    unsigned int minNum = 1;
    for(unsigned int i = 1; i < N; i++) {
        minNum *= 10;
    }
    unsigned int maxNum = minNum * 10 - 1;
    std::vector<bool> primes(maxNum, true);
    primes[0] = primes[1] = false;
    for(unsigned int i = 2; (i * i) <= maxNum; i++) {
        if(primes[i]) {
            for(unsigned j = 2 * i; j <= maxNum; j += i) {
                primes[j] = false;
            }
        }
    }
    for(unsigned int i = minNum; i <= maxNum; i++) {
        if(primes[i]) {
            auto strNum = std::to_string(i);
            for(unsigned int dig = 0; dig <= 9; dig++) {
                match(i, strNum, dig, K);
            }
            if(N == 7) {
                if(K == 1 && i > 2 * std::pow(10, 6)) {
                    break;
                }
                if(K == 2 && i > 3 * std::pow(10, 6)) {
                    break;
                }
            }
        }
    }
    std::string mini;
    for(auto m : M) {
        if(m.second.size() < L) {
            continue;
        }
        if(m.second.front() != smPrime) {
            continue;
        }
        std::string s;
        for(unsigned i = 0; i < L; i++) {
            s += std::to_string(m.second[i]) + " ";
        }
        if(mini > s || mini.empty()) {
            mini = s;
        }
    }
    std::cout << mini << std::endl;
    return 0;
}

Comments

Popular Posts