Quantcast
Channel: YGC » c++
Viewing all articles
Browse latest Browse all 18

project euler -- problem 51

$
0
0
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.

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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
 
#include<iostream>
#include<cmath>
using namespace std;
 
const int size = 6;
const int family_size = 8;
 
bool has_same_digits(int n);
bool is_same_digit(int n, int i);
bool is_prime(int n);
int* get_digits(int n);
int* get_position(int n, int i);
int get_num(int* d);
int get_digit_count(int n, int i);
 
int main() {
  for (int n=pow(10.0, size-1); n < pow(10.0,size); n++) {
    bool flag = 0;
    if (is_prime(n) && has_same_digits(n)) { 
      for(int i=0; i < 10; i++){
	if (is_same_digit(n,i)) {
	  int* pos;
	  int* d;
	  d = get_digits(n);
	  pos = get_position(n, i);
	  int cnt=0;
	  int len=get_digit_count(n,i);
	  for (int t=0; t<10; t++) {
	    int idx = 0;
	    while(idx < len) {
	      d[pos[idx]] = t;
	      idx++;
	    }
	    int num=get_num(d);
	    if (is_prime(num) && num > pow(10.0, size-1))
	      cnt++;
	  }
	  if (cnt == family_size) {
	    cout << n << endl;
	    flag = 1;
	    break;
	  }
	}
      }
    }
    if (flag) 
      break;
  }
  return 0;
}
 
 
int* get_digits(int n) {
  int* d = new int[size];
  for (int i=0; i < size; i++) {
    d[i] = n % 10;
    n /= 10;
  }
  return d;
}
 
int get_num(int* d) {
  int num = 0;
  for(int i=0; i < size; i++) {
    num += d[i] * (int) pow(10.0, i);
  }
  return num;
}
 
int get_digit_count(int n, int i) {
  int* d = get_digits(n);
  int cnt = 0;
  for (int j=0; j < size; j++) {
    if (d[j] == i)
      cnt++;
  }
  return cnt;
}
 
int* get_position(int n, int i) {
  int* d = get_digits(n);
  int cnt = get_digit_count(n, i);
  int* p = new int[cnt];
  int p_idx = 0;
  for (int j=0; j < size; j++) {
    if (d[j] == i){
      p[p_idx++] = j; 
    }
  }
  return p;
}
 
bool is_same_digit(int n, int i) {
  int* d = get_digits(n);
  int cnt=0;
  for (int j=0; j < size; j++) {
    if (d[j] == i)
      cnt++;
  }
  if (cnt >= 2)
    return 1;
  return 0;
}
 
bool has_same_digits(int n) {
  int* d = get_digits(n);
  for (int i=0; i < size; i++) {
    for (int j=0; j < i; j++) {
      if (d[i] == d[j])
	return 1;
    }
  }
  return 0;
}
 
bool is_prime(int n) {
  for (int i=2; i < sqrt(n); i++) {
    if ( n % i == 0 ) {
      return 0;
      break;
    }
  }
  return 1;
}

Related Posts


Viewing all articles
Browse latest Browse all 18

Trending Articles