/
ch-1.w
159 lines (139 loc) · 3.24 KB
/
ch-1.w
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
\documentclass{article}
\usepackage{listings}
\lstset{extendedchars=true , keepspaces=true , language=perl}
\begin{document}
Challenge 134
\section{Pandigital Numbers}
Write a script to generate first 5 Pandigital Numbers in base 10.
\section{Pandigital Numbers: Perl}
@s
@o perl/ch-1.pl
@{
@<preamble...@>
@<first n pandigital numbers@>
@<first 5 pandigital numbers@>
@<main@>
@}
@d preamble: use statements, introductory comments, etc.
@{
use strict;
use warnings;
##
# Write a script to generate first 5 Pandigital Numbers in base 10.
##
use boolean;
@}
@d check if pandigital
@{
my $test = $x;
push @@{$pandigitals}, $x if
($test =~ tr/0//d) > 0 &&
($test =~ tr/1//d) > 0 &&
($test =~ tr/2//d) > 0 &&
($test =~ tr/3//d) > 0 &&
($test =~ tr/4//d) > 0 &&
($test =~ tr/5//d) > 0 &&
($test =~ tr/6//d) > 0 &&
($test =~ tr/7//d) > 0 &&
($test =~ tr/8//d) > 0 &&
($test =~ tr/9//d) > 0;
$found = (@@{$pandigitals} == $n);
@}
@d first n pandigital numbers
@{
sub first_n_pandigitals{
my($n) = @@_;
my $found = false;
my $pandigitals = [];
my $x = 1_000_000_000;
do{
@<check if pandigital@>
$x++;
} while(!$found);
return $pandigitals;
}
@}
@d first 5 pandigital numbers
@{
sub first_5_pandigitals{
return first_n_pandigitals(5);
}
@}
@d main
@{
MAIN:{
my $pandigitals = first_5_pandigitals;
for my $x (@@{$pandigitals}){
print "$x\n";
}
}
@}
@S
\section{Pandigital Numbers: C++}
@s
@s
@o cxx/ch-1.cxx
@{
@<preamble: #...@>
@<class definition@>
@<first n pandigital numbers@>
@<first 5 pandigital numbers@>
@<main@>
@}
@d preamble: #include statements, introductory comments, etc.
@{
#include<iostream>
#include<vector>
#include<algorithm>
/*
* Write a script to generate first 5 Pandigital Numbers in base 10.
*/
@}
@d class definition
@{
class Pandigital{
public:
std::vector<int> first_n_pandigitals(int);
std::vector<int> first_5_pandigitals();
};
@}
@d first 5 pandigital numbers
@{
std::vector<int> Pandigital::first_5_pandigitals(){
return this->first_n_pandigitals(5);
}
@}
@d first n pandigital numbers
@{
std::vector<int> Pandigital::first_n_pandigitals(int n){
std::vector<int> pandigitals;
int x = 1000000000;
do{
@<check if pandigital@>
x++;
}while(pandigitals.size() < n);
return pandigitals;
}
@}
@d check if pandigital
@{
int test = x;
std::vector<int> test_vector = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
do{
test_vector.erase(std::remove(test_vector.begin(), test_vector.end(), test % 10), test_vector.end());
test = test / 10;
}while(test > 0);
if(test_vector.size() == 0)
pandigitals.push_back(x);
@}
@d main
@{
int main(int argc, char** argv){
Pandigital p;
std::vector<int> pandigitals = p.first_5_pandigitals();
for(int i = 0; i < pandigitals.size(); i++){
std::cout << pandigitals[i] << std::endl;
}
}
@}
\end{document}