Lucky and even lucky numbers: Difference between revisions

Added an example for D
(add PicoLisp)
(Added an example for D)
Line 88:
* Sequence [https://oeis.org/A045954 A045954 even lucky numbers or ELN] on The On-Line Encyclopedia of Integer Sequences.
* Entry [http://mathworld.wolfram.com/LuckyNumber.html lucky numbers] on The Eric Weisstein's World of Mathematics.<br><br>
 
=={{header|D}}==
<lang D>import std.algorithm;
import std.concurrency;
import std.conv;
import std.getopt;
import std.range;
import std.stdio;
 
auto lucky(bool even, int nmax=200_000) {
import std.container.array;
 
int start = even ? 2 : 1;
 
return new Generator!int({
auto ln = make!(Array!int)(iota(start,nmax,2));
 
// yield the first number
yield(ln[0]);
 
int n=1;
for(; n<ln.length/2+1; n++) {
yield(ln[n]);
 
int step = ln[n]-1;
 
// remove the non-lucky numbers related to the current lucky number
for (int i=step; i<ln.length; i+=step) {
ln.linearRemove(ln[].drop(i).take(1));
}
}
 
// yield all remaining values
foreach(val; ln[n..$]) {
yield(val);
}
});
}
 
void help(Option[] opt) {
defaultGetoptPrinter("./lucky j [k] [--lucky|--evenLucky]", opt);
 
writeln;
writeln(" argument(s) | what is displayed");
writeln("==============================================");
writeln("-j=m | mth lucky number");
writeln("-j=m --lucky | mth lucky number");
writeln("-j=m --evenLucky | mth even lucky number");
writeln("-j=m -k=n | mth through nth (inclusive) lucky numbers");
writeln("-j=m -k=n --lucky | mth through nth (inclusive) lucky numbers");
writeln("-j=m -k=n --evenLucky | mth through nth (inclusive) even lucky numbers");
writeln("-j=m -k=-n | all lucky numbers in the range [m, n]");
writeln("-j=m -k=-n --lucky | all lucky numbers in the range [m, n]");
writeln("-j=m -k=-n --evenLucky | all even lucky numbers in the range [m, n]");
}
 
void main(string[] args) {
int j;
int k;
bool evenLucky = false;
 
void luckyOpt() {
evenLucky = false;
}
auto helpInformation = getopt(
args,
std.getopt.config.passThrough,
std.getopt.config.required,
"j", "The starting point to generate lucky numbers", &j,
"k", "The ending point for generating lucky numbers", &k,
"lucky", "Specify to generate a list of lucky numbers", &luckyOpt,
"evenLucky", "Specify to generate a list of even lucky numbers", &evenLucky
);
 
if (helpInformation.helpWanted) {
help(helpInformation.options);
return;
}
 
if (k>0) {
lucky(evenLucky).drop(j-1).take(k-j+1).writeln;
} else if (k<0) {
auto f = (int a) => j<=a && a<=-k;
lucky(evenLucky, -k).filter!f.writeln;
} else {
lucky(evenLucky).drop(j-1).take(1).writeln;
}
}</lang>
 
{{out}}
<pre>.\lucky_and_even_lucky_numbers.exe -j=1 -k=20
[1, 3, 7, 9, 13, 15, 21, 25, 31, 33, 37, 43, 49, 51, 63, 67, 69, 73, 75, 79]
 
.\lucky_and_even_lucky_numbers.exe -j=1 -k=20 --evenLucky
[2, 4, 6, 10, 12, 18, 20, 22, 26, 34, 36, 42, 44, 50, 52, 54, 58, 68, 70, 76]
 
.\lucky_and_even_lucky_numbers.exe -j=6000 -k=-6100
[6009, 6019, 6031, 6049, 6055, 6061, 6079, 6093]
 
.\lucky_and_even_lucky_numbers.exe -j=6000 -k=-6100 --evenLucky
[6018, 6020, 6022, 6026, 6036, 6038, 6050, 6058, 6074, 6090, 6092]
 
.\lucky_and_even_lucky_numbers.exe -j=10000
[115591]
 
.\lucky_and_even_lucky_numbers.exe -j=10000 --evenLucky
[111842]</pre>
 
=={{header|Haskell}}==
1,452

edits