Skip to content

Commit 9bb1036

Browse files
Merge pull request #520 from precisecharmer/main
Add pandigital_num.cpp
2 parents 929cd42 + 467720e commit 9bb1036

File tree

2 files changed

+346
-0
lines changed

2 files changed

+346
-0
lines changed

CPP/pandigital_numbers/code.cpp

+341
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,341 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
#define ll long long
4+
const int inf = 1e9 + 7;
5+
6+
vector<int> func3(string str, vector<int> &values)
7+
{
8+
int num = stoi(str);
9+
int a, b;
10+
b = num % 10;
11+
num /= 10;
12+
a = num % 10;
13+
num /= 10;
14+
15+
vector<int> vec;
16+
17+
for (int i = 0; i < values.size(); i++)
18+
{
19+
if (values[i] == 0)
20+
{
21+
if ((i + a + b) % 3 == 0)
22+
{
23+
vec.push_back(i);
24+
}
25+
}
26+
}
27+
return vec;
28+
}
29+
30+
vector<int> func5(string str, vector<int> &values)
31+
{
32+
int num = stoi(str);
33+
int a, b;
34+
b = num % 10;
35+
num /= 10;
36+
a = num % 10;
37+
num /= 10;
38+
39+
vector<int> vec;
40+
41+
for (int i = 0; i < values.size(); i++)
42+
{
43+
if (values[i] == 0 && (i == 0 || i == 5))
44+
{
45+
vec.push_back(i);
46+
}
47+
}
48+
49+
return vec;
50+
}
51+
52+
vector<int> func7(string str, vector<int> &values)
53+
{
54+
int num = stoi(str);
55+
int a, b;
56+
b = num % 10;
57+
num /= 10;
58+
a = num % 10;
59+
num /= 10;
60+
61+
vector<int> vec;
62+
63+
for (int i = 0; i < values.size(); i++)
64+
{
65+
// Subtracting 2 times the last digit from the rest gives a multiple of 7. (Works because 21 is divisible by 7.)
66+
if (((a * 10 + b) - (2 * i)) % 7 == 0 && values[i] == 0)
67+
{
68+
vec.push_back(i);
69+
}
70+
}
71+
72+
return vec;
73+
}
74+
75+
vector<int> func11(string str, vector<int> &values)
76+
{
77+
int num = stoi(str);
78+
int a, b;
79+
b = num % 10;
80+
num /= 10;
81+
a = num % 10;
82+
num /= 10;
83+
84+
vector<int> vec;
85+
86+
for (int i = 0; i < values.size(); i++)
87+
{
88+
if ((abs(a + i - b)) % 11 == 0 && values[i] == 0)
89+
{
90+
vec.push_back(i);
91+
}
92+
}
93+
94+
return vec;
95+
}
96+
97+
vector<int> func13(string str, vector<int> &values)
98+
{
99+
int num = stoi(str);
100+
int a, b;
101+
b = num % 10;
102+
num /= 10;
103+
a = num % 10;
104+
num /= 10;
105+
106+
vector<int> vec;
107+
108+
for (int i = 0; i < values.size(); i++)
109+
{
110+
// Subtract the last two digits from four times the rest. The result must be divisible by 13.
111+
if ((abs(b * 10 + i - 4 * a)) % 13 == 0 && values[i] == 0)
112+
{
113+
vec.push_back(i);
114+
}
115+
}
116+
117+
return vec;
118+
}
119+
120+
vector<int> func17(string str, vector<int> &values)
121+
{
122+
int num = stoi(str);
123+
int a, b;
124+
b = num % 10;
125+
num /= 10;
126+
a = num % 10;
127+
num /= 10;
128+
129+
vector<int> vec;
130+
131+
for (int i = 0; i < values.size(); i++)
132+
{
133+
// Subtract 5 times the last digit from the rest.
134+
135+
if ((abs(a * 10 + b - 5 * i)) % 17 == 0 && values[i] == 0)
136+
{
137+
vec.push_back(i);
138+
}
139+
}
140+
141+
return vec;
142+
}
143+
144+
void func(int n)
145+
{
146+
// 406
147+
string str = to_string(n);
148+
vector<int> values(10, 0);
149+
150+
// vec[0] = vec[4] = vec[6] = 1;
151+
152+
int a, b, c;
153+
c = n % 10;
154+
n /= 10;
155+
b = n % 10;
156+
n /= 10;
157+
a = n % 10;
158+
n /= 10;
159+
160+
values[a] = values[b] = values[c] = 1;
161+
162+
vector<int> vec3 = func3(str, values);
163+
164+
for (int i = 0; i < vec3.size(); i++)
165+
{
166+
string stri = str;
167+
stri = stri + to_string(vec3[i]);
168+
169+
values[vec3[i]] = 1;
170+
171+
// cout << "STRI: " << stri << endl;
172+
173+
// cout << "CHECK VECTOR: " << endl;
174+
// for (int i = 0; i < values.size(); i++)
175+
// {
176+
// cout << values[i] << " ";
177+
// }
178+
// cout << endl;
179+
180+
vector<int> vec5 = func5(stri, values);
181+
182+
for (int j = 0; j < vec5.size(); j++)
183+
{
184+
string strj = stri;
185+
strj = strj + to_string(vec5[j]);
186+
187+
values[vec5[j]] = 1;
188+
189+
// cout << "STRJ: " << strj << endl;
190+
191+
// cout << "CHECK VECTOR: " << endl;
192+
// for (int i = 0; i < values.size(); i++)
193+
// {
194+
// cout << values[i] << " ";
195+
// }
196+
// cout << endl;
197+
198+
vector<int> vec7 = func7(strj, values);
199+
200+
for (int k = 0; k < vec7.size(); k++)
201+
{
202+
203+
string strk = strj;
204+
strk = strk + to_string(vec7[k]);
205+
206+
values[vec7[k]] = 1;
207+
208+
// cout << "STRK: " << strk << endl;
209+
210+
// cout << "CHECK VECTOR: " << endl;
211+
// for (int i = 0; i < values.size(); i++)
212+
// {
213+
// cout << values[i] << " ";
214+
// }
215+
// cout << endl;
216+
217+
vector<int> vec11 = func11(strk, values);
218+
219+
for (int l = 0; l < vec11.size(); l++)
220+
{
221+
222+
string strl = strk;
223+
strl = strl + to_string(vec11[l]);
224+
225+
values[vec11[l]] = 1;
226+
227+
// cout << "STRL: " << strl << endl;
228+
229+
vector<int> vec13 = func13(strl, values);
230+
231+
for (int m = 0; m < vec13.size(); m++)
232+
{
233+
234+
string strm = strl;
235+
strm = strm + to_string(vec13[m]);
236+
237+
values[vec13[m]] = 1;
238+
239+
// cout << "STRM: " << strm << endl;
240+
241+
vector<int> vec17 = func17(strm, values);
242+
243+
for (int n = 0; n < vec17.size(); n++)
244+
{
245+
246+
string strn = strm;
247+
strn = strn + to_string(vec17[n]);
248+
249+
values[vec17[n]] = 1;
250+
251+
// cout << "STRM: " << strn << endl;
252+
253+
// cout << "CHECK VECTOR: " << endl;
254+
// for (int i = 0; i < values.size(); i++)
255+
// {
256+
// cout << values[i] << " ";
257+
// }
258+
// cout << endl;
259+
260+
for (int a = 0; a < values.size(); a++)
261+
{
262+
if (values[a] == 0)
263+
{
264+
string abcd = to_string(a);
265+
string crst = abcd + strn;
266+
267+
cout << "FINAL STRING: " << crst << endl;
268+
}
269+
}
270+
271+
values[vec17[n]] = 0;
272+
}
273+
274+
// answer is size of vec17
275+
276+
values[vec13[m]] = 0;
277+
}
278+
279+
values[vec11[l]] = 0;
280+
}
281+
282+
values[vec7[k]] = 0;
283+
}
284+
285+
values[vec5[j]] = 0;
286+
}
287+
values[vec3[i]] = 0;
288+
}
289+
}
290+
291+
void solve()
292+
{
293+
294+
int start = 100, end = 1000;
295+
for (int i = start; i < end; i++)
296+
{
297+
int curr = i;
298+
if (curr % 2 == 0)
299+
{
300+
int a, b, c;
301+
a = curr % 10;
302+
curr /= 10;
303+
b = curr % 10;
304+
curr /= 10;
305+
c = curr % 10;
306+
curr /= 10;
307+
308+
if (a != b && b != c && c != a)
309+
{
310+
// cout << i << endl;
311+
func(i);
312+
}
313+
}
314+
}
315+
}
316+
317+
int main()
318+
{
319+
ios_base::sync_with_stdio(false);
320+
cin.tie(NULL);
321+
cout.tie(NULL);
322+
cin.exceptions(cin.failbit);
323+
// freopen("input.txt", "r", stdin);
324+
// freopen("output.txt", "w", stdout);
325+
326+
clock_t start = clock();
327+
328+
solve();
329+
clock_t end = clock();
330+
double elapsed = double(end - start) / CLOCKS_PER_SEC;
331+
printf("Time measured: %.4f seconds.", elapsed);
332+
return 0;
333+
}
334+
335+
/*
336+
337+
338+
339+
340+
341+
*/

CPP/pandigital_numbers/readme.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
First code is of Problem statement: https://projecteuler.net/problem=43
2+
3+
Idea to solve this problem is:
4+
we are given numbers that divides (di di+1 di+2), we will then form an array that satisifes condition and through multilple loops and by applying different fucntions like func3(), func5(), .. we will find how many values out of {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} satifies that and keep oh doing it.. (Also we mark the numbers we found as 1, so that we dont get those again)
5+
Finally we get all the numbers

0 commit comments

Comments
 (0)