Lucky and even lucky numbers: Difference between revisions

added RPL
m (→‎{{header|Haskell}}: simplified argument parsing)
(added RPL)
 
(43 intermediate revisions by 19 users not shown)
Line 1:
{{draft task}}
<small>Note that in the following explanation list indices are assumed to start at ''one''.</small>
 
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|11l}}==
{{trans|Nim}}
 
<syntaxhighlight lang="11l">-V NoValue = 0
 
F initLuckyNumbers(nelems, evenlucky, limit = -1)
[Int] result
L(i) 0 .< nelems
V k = i
L(j) (result.len - 1 .< 0).step(-1)
k = k * result[j] I/ (result[j] - 1)
V n = 2 * k + 1 + evenlucky
I limit != -1 & n > limit
L.break
result.append(n)
R result
 
F name(evenlucky)
R [‘Lucky’, ‘Even lucky’][evenlucky]
 
F printSingle(j, evenlucky)
V luckySeq = initLuckyNumbers(j, evenlucky)
print(name(evenlucky)‘ number at index ’j‘ is ’luckySeq[j - 1])
 
F printRange(j, k, evenlucky)
V luckySeq = initLuckyNumbers(k, evenlucky)
print(name(evenlucky)‘ numbers at indexes ’j‘ to ’k‘ are: ’, end' ‘’)
L(idx) j - 1 .< k
I idx != j - 1
print(‘, ’, end' ‘’)
print(luckySeq[idx], end' ‘’)
print()
 
F printInRange(j, k, evenlucky)
V luckySeq = initLuckyNumbers(k, evenlucky, k)
print(name(evenlucky)‘ numbers between ’j‘ to ’k‘ are: ’, end' ‘’)
V first = 1B
L(val) luckySeq
I val > j
I first
first = 0B
E
print(‘, ’, end' ‘’)
print(val, end' ‘’)
print()
 
F process_args(args)
assert(args.len C 1..3, ‘Wrong number of arguments’)
 
// First argument: "j" value.
V j = Int(args[0])
 
V k = NoValue
// Second argument: "k" value or a comma.
I args.len > 1
I args[1] == ‘,’
// Must be followed by the kind of lucky number.
assert(args.len == 3, ‘Missing kind argument’)
E
k = Int(args[1])
assert(k != 0, ‘Expected a non null number’)
 
V evenlucky = 0
// Third argument: number kind.
I args.len == 3
V kind = args[2].lowercase()
assert(kind C (‘lucky’, ‘evenlucky’), ‘Wrong kind’)
I kind == ‘evenlucky’
evenlucky = 1
 
I k == NoValue
// Print jth value.
printSingle(j, evenlucky)
E I k > 0
// Print jth to kth values.
printRange(j, k, evenlucky)
E
// Print values in range j..(-k).
printInRange(j, -k, evenlucky)
 
:start:
I 1B
L(args) [‘1 20’, ‘1 20 evenlucky’, ‘6000 -6100’, ‘6000 -6100 evenlucky’, ‘10000’, ‘10000 , lucky’, ‘10000 , evenlucky’]
print(‘Command line arguments: ’args)
process_args(args.split(‘ ’))
print()
E
process_args(:argv[1..])</syntaxhighlight>
 
{{out}}
<pre>
Command line arguments: 1 20
Lucky numbers at indexes 1 to 20 are: 1, 3, 7, 9, 13, 15, 21, 25, 31, 33, 37, 43, 49, 51, 63, 67, 69, 73, 75, 79
 
Command line arguments: 1 20 evenlucky
Even lucky numbers at indexes 1 to 20 are: 2, 4, 6, 10, 12, 18, 20, 22, 26, 34, 36, 42, 44, 50, 52, 54, 58, 68, 70, 76
 
Command line arguments: 6000 -6100
Lucky numbers between 6000 to 6100 are: 6009, 6019, 6031, 6049, 6055, 6061, 6079, 6093
 
Command line arguments: 6000 -6100 evenlucky
Even lucky numbers between 6000 to 6100 are: 6018, 6020, 6022, 6026, 6036, 6038, 6050, 6058, 6074, 6090, 6092
 
Command line arguments: 10000
Lucky number at index 10000 is 115591
 
Command line arguments: 10000 , lucky
Lucky number at index 10000 is 115591
 
Command line arguments: 10000 , evenlucky
Even lucky number at index 10000 is 111842
 
</pre>
 
=={{header|C}}==
{{trans|C++}}
<syntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
 
#define LUCKY_SIZE 60000
int luckyOdd[LUCKY_SIZE];
int luckyEven[LUCKY_SIZE];
 
void compactLucky(int luckyArray[]) {
int i, j, k;
 
for (i = 0; i < LUCKY_SIZE; i++) {
if (luckyArray[i] == 0) {
j = i;
break;
}
}
 
for (j = i + 1; j < LUCKY_SIZE; j++) {
if (luckyArray[j] > 0) {
luckyArray[i++] = luckyArray[j];
}
}
 
for (; i < LUCKY_SIZE; i++) {
luckyArray[i] = 0;
}
}
 
void initialize() {
int i, j;
 
// unfiltered
for (i = 0; i < LUCKY_SIZE; i++) {
luckyEven[i] = 2 * i + 2;
luckyOdd[i] = 2 * i + 1;
}
 
// odd filter
for (i = 1; i < LUCKY_SIZE; i++) {
if (luckyOdd[i] > 0) {
for (j = luckyOdd[i] - 1; j < LUCKY_SIZE; j += luckyOdd[i]) {
luckyOdd[j] = 0;
}
compactLucky(luckyOdd);
}
}
 
// even filter
for (i = 1; i < LUCKY_SIZE; i++) {
if (luckyEven[i] > 0) {
for (j = luckyEven[i] - 1; j < LUCKY_SIZE; j += luckyEven[i]) {
luckyEven[j] = 0;
}
compactLucky(luckyEven);
}
}
}
 
void printBetween(size_t j, size_t k, bool even) {
int i;
 
if (even) {
if (luckyEven[j] == 0 || luckyEven[k] == 0) {
fprintf(stderr, "At least one argument is too large\n");
exit(EXIT_FAILURE);
}
printf("Lucky even numbers between %d and %d are:", j, k);
for (i = 0; luckyEven[i] != 0; i++) {
if (luckyEven[i] > k) {
break;
}
if (luckyEven[i] > j) {
printf(" %d", luckyEven[i]);
}
}
} else {
if (luckyOdd[j] == 0 || luckyOdd[k] == 0) {
fprintf(stderr, "At least one argument is too large\n");
exit(EXIT_FAILURE);
}
printf("Lucky numbers between %d and %d are:", j, k);
for (i = 0; luckyOdd[i] != 0; i++) {
if (luckyOdd[i] > k) {
break;
}
if (luckyOdd[i] > j) {
printf(" %d", luckyOdd[i]);
}
}
}
printf("\n");
}
 
void printRange(size_t j, size_t k, bool even) {
int i;
 
if (even) {
if (luckyEven[k] == 0) {
fprintf(stderr, "The argument is too large\n");
exit(EXIT_FAILURE);
}
printf("Lucky even numbers %d to %d are:", j, k);
for (i = j - 1; i < k; i++) {
printf(" %d", luckyEven[i]);
}
} else {
if (luckyOdd[k] == 0) {
fprintf(stderr, "The argument is too large\n");
exit(EXIT_FAILURE);
}
printf("Lucky numbers %d to %d are:", j, k);
for (i = j - 1; i < k; i++) {
printf(" %d", luckyOdd[i]);
}
}
printf("\n");
}
 
void printSingle(size_t j, bool even) {
if (even) {
if (luckyEven[j] == 0) {
fprintf(stderr, "The argument is too large\n");
exit(EXIT_FAILURE);
}
printf("Lucky even number %d=%d\n", j, luckyEven[j - 1]);
} else {
if (luckyOdd[j] == 0) {
fprintf(stderr, "The argument is too large\n");
exit(EXIT_FAILURE);
}
printf("Lucky number %d=%d\n", j, luckyOdd[j - 1]);
}
}
 
void help() {
printf("./lucky j [k] [--lucky|--evenLucky]\n");
printf("\n");
printf(" argument(s) | what is displayed\n");
printf("==============================================\n");
printf("-j=m | mth lucky number\n");
printf("-j=m --lucky | mth lucky number\n");
printf("-j=m --evenLucky | mth even lucky number\n");
printf("-j=m -k=n | mth through nth (inclusive) lucky numbers\n");
printf("-j=m -k=n --lucky | mth through nth (inclusive) lucky numbers\n");
printf("-j=m -k=n --evenLucky | mth through nth (inclusive) even lucky numbers\n");
printf("-j=m -k=-n | all lucky numbers in the range [m, n]\n");
printf("-j=m -k=-n --lucky | all lucky numbers in the range [m, n]\n");
printf("-j=m -k=-n --evenLucky | all even lucky numbers in the range [m, n]\n");
}
 
void process(int argc, char *argv[]) {
bool evenLucky = false;
int j = 0;
int k = 0;
 
bool good = false;
int i;
 
for (i = 1; i < argc; ++i) {
if ('-' == argv[i][0]) {
if ('-' == argv[i][1]) {
// long args
if (0 == strcmp("--lucky", argv[i])) {
evenLucky = false;
} else if (0 == strcmp("--evenLucky", argv[i])) {
evenLucky = true;
} else {
fprintf(stderr, "Unknown long argument: [%s]\n", argv[i]);
exit(EXIT_FAILURE);
}
} else {
// short args
if ('j' == argv[i][1] && '=' == argv[i][2] && argv[i][3] != 0) {
good = true;
j = atoi(&argv[i][3]);
} else if ('k' == argv[i][1] && '=' == argv[i][2]) {
k = atoi(&argv[i][3]);
} else {
fprintf(stderr, "Unknown short argument: [%s]\n", argv[i]);
exit(EXIT_FAILURE);
}
}
} else {
fprintf(stderr, "Unknown argument: [%s]\n", argv[i]);
exit(EXIT_FAILURE);
}
}
 
if (!good) {
help();
exit(EXIT_FAILURE);
}
 
if (k > 0) {
printRange(j, k, evenLucky);
} else if (k < 0) {
printBetween(j, -k, evenLucky);
} else {
printSingle(j, evenLucky);
}
}
 
void test() {
printRange(1, 20, false);
printRange(1, 20, true);
 
printBetween(6000, 6100, false);
printBetween(6000, 6100, true);
 
printSingle(10000, false);
printSingle(10000, true);
}
 
int main(int argc, char *argv[]) {
initialize();
 
//test();
 
if (argc < 2) {
help();
return 1;
}
process(argc, argv);
 
return 0;
}</syntaxhighlight>
{{out}}
<pre>LuckyNumbers.exe -j=1 -k=20
Lucky numbers 1 to 20 are: 1 3 7 9 13 15 21 25 31 33 37 43 49 51 63 67 69 73 75 79
 
LuckyNumbers.exe -j=1 -k=20 --evenLucky
Lucky even numbers 1 to 20 are: 2 4 6 10 12 18 20 22 26 34 36 42 44 50 52 54 58 68 70 76
 
LuckyNumbers.exe -j=6000 -k=-6100
Lucky numbers between 6000 and 6100 are: 6009 6019 6031 6049 6055 6061 6079 6093
 
LuckyNumbers.exe -j=6000 -k=-6100 --evenLucky
Lucky even numbers between 6000 and 6100 are: 6018 6020 6022 6026 6036 6038 6050 6058 6074 6090 6092
 
LuckyNumbers.exe -j=10000
Lucky number 10000=115591
 
LuckyNumbers.exe -j=10000 --evenLucky
Lucky even number 10000=111842</pre>
 
=={{header|C++}}==
{{trans|Go}}
<syntaxhighlight lang="cpp">#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
 
const int luckySize = 60000;
std::vector<int> luckyEven(luckySize);
std::vector<int> luckyOdd(luckySize);
 
void init() {
for (int i = 0; i < luckySize; ++i) {
luckyEven[i] = i * 2 + 2;
luckyOdd[i] = i * 2 + 1;
}
}
 
void filterLuckyEven() {
for (size_t n = 2; n < luckyEven.size(); ++n) {
int m = luckyEven[n - 1];
int end = (luckyEven.size() / m) * m - 1;
for (int j = end; j >= m - 1; j -= m) {
std::copy(luckyEven.begin() + j + 1, luckyEven.end(), luckyEven.begin() + j);
luckyEven.pop_back();
}
}
}
 
void filterLuckyOdd() {
for (size_t n = 2; n < luckyOdd.size(); ++n) {
int m = luckyOdd[n - 1];
int end = (luckyOdd.size() / m) * m - 1;
for (int j = end; j >= m - 1; j -= m) {
std::copy(luckyOdd.begin() + j + 1, luckyOdd.end(), luckyOdd.begin() + j);
luckyOdd.pop_back();
}
}
}
 
void printBetween(size_t j, size_t k, bool even) {
std::ostream_iterator<int> out_it{ std::cout, ", " };
 
if (even) {
size_t max = luckyEven.back();
if (j > max || k > max) {
std::cerr << "At least one are is too big\n";
exit(EXIT_FAILURE);
}
 
std::cout << "Lucky even numbers between " << j << " and " << k << " are: ";
std::copy_if(luckyEven.begin(), luckyEven.end(), out_it, [j, k](size_t n) {
return j <= n && n <= k;
});
} else {
size_t max = luckyOdd.back();
if (j > max || k > max) {
std::cerr << "At least one are is too big\n";
exit(EXIT_FAILURE);
}
 
std::cout << "Lucky numbers between " << j << " and " << k << " are: ";
std::copy_if(luckyOdd.begin(), luckyOdd.end(), out_it, [j, k](size_t n) {
return j <= n && n <= k;
});
}
std::cout << '\n';
}
 
void printRange(size_t j, size_t k, bool even) {
std::ostream_iterator<int> out_it{ std::cout, ", " };
if (even) {
if (k >= luckyEven.size()) {
std::cerr << "The argument is too large\n";
exit(EXIT_FAILURE);
}
std::cout << "Lucky even numbers " << j << " to " << k << " are: ";
std::copy(luckyEven.begin() + j - 1, luckyEven.begin() + k, out_it);
} else {
if (k >= luckyOdd.size()) {
std::cerr << "The argument is too large\n";
exit(EXIT_FAILURE);
}
std::cout << "Lucky numbers " << j << " to " << k << " are: ";
std::copy(luckyOdd.begin() + j - 1, luckyOdd.begin() + k, out_it);
}
}
 
void printSingle(size_t j, bool even) {
if (even) {
if (j >= luckyEven.size()) {
std::cerr << "The argument is too large\n";
exit(EXIT_FAILURE);
}
std::cout << "Lucky even number " << j << "=" << luckyEven[j - 1] << '\n';
} else {
if (j >= luckyOdd.size()) {
std::cerr << "The argument is too large\n";
exit(EXIT_FAILURE);
}
std::cout << "Lucky number " << j << "=" << luckyOdd[j - 1] << '\n';
}
}
 
void help() {
std::cout << "./lucky j [k] [--lucky|--evenLucky]\n";
std::cout << "\n";
std::cout << " argument(s) | what is displayed\n";
std::cout << "==============================================\n";
std::cout << "-j=m | mth lucky number\n";
std::cout << "-j=m --lucky | mth lucky number\n";
std::cout << "-j=m --evenLucky | mth even lucky number\n";
std::cout << "-j=m -k=n | mth through nth (inclusive) lucky numbers\n";
std::cout << "-j=m -k=n --lucky | mth through nth (inclusive) lucky numbers\n";
std::cout << "-j=m -k=n --evenLucky | mth through nth (inclusive) even lucky numbers\n";
std::cout << "-j=m -k=-n | all lucky numbers in the range [m, n]\n";
std::cout << "-j=m -k=-n --lucky | all lucky numbers in the range [m, n]\n";
std::cout << "-j=m -k=-n --evenLucky | all even lucky numbers in the range [m, n]\n";
}
 
int main(int argc, char **argv) {
bool evenLucky = false;
int j = 0;
int k = 0;
 
// skip arg 0, because that is just the executable name
if (argc < 2) {
help();
exit(EXIT_FAILURE);
}
 
bool good = false;
for (int i = 1; i < argc; ++i) {
if ('-' == argv[i][0]) {
if ('-' == argv[i][1]) {
// long args
if (0 == strcmp("--lucky", argv[i])) {
evenLucky = false;
} else if (0 == strcmp("--evenLucky", argv[i])) {
evenLucky = true;
} else {
std::cerr << "Unknown long argument: [" << argv[i] << "]\n";
exit(EXIT_FAILURE);
}
} else {
// short args
if ('j' == argv[i][1] && '=' == argv[i][2] && argv[i][3] != 0) {
good = true;
j = atoi(&argv[i][3]);
} else if ('k' == argv[i][1] && '=' == argv[i][2]) {
k = atoi(&argv[i][3]);
} else {
std::cerr << "Unknown short argument: " << argv[i] << '\n';
exit(EXIT_FAILURE);
}
}
} else {
std::cerr << "Unknown argument: " << argv[i] << '\n';
exit(EXIT_FAILURE);
}
}
if (!good) {
help();
exit(EXIT_FAILURE);
}
 
init();
filterLuckyEven();
filterLuckyOdd();
if (k > 0) {
printRange(j, k, evenLucky);
} else if (k < 0) {
printBetween(j, -k, evenLucky);
} else {
printSingle(j, evenLucky);
}
 
return 0;
}</syntaxhighlight>
{{out}}
<pre>&gt;LuckyNumbers.exe -j=1 -k=20
Lucky numbers 1 to 20 are: 1, 3, 7, 9, 13, 15, 21, 25, 31, 33, 37, 43, 49, 51, 63, 67, 69, 73, 75, 79,
 
&gt;LuckyNumbers.exe -j=1 -k=20 --evenLucky
Lucky even numbers 1 to 20 are: 2, 4, 6, 10, 12, 18, 20, 22, 26, 34, 36, 42, 44, 50, 52, 54, 58, 68, 70, 76,
 
&gt;LuckyNumbers.exe -j=6000 -k=-6100
Lucky numbers between 6000 and 6100 are: 6009, 6019, 6031, 6049, 6055, 6061, 6079, 6093,
 
&gt;LuckyNumbers.exe -j=6000 -k=-6100 --evenLucky
Lucky even numbers between 6000 and 6100 are: 6018, 6020, 6022, 6026, 6036, 6038, 6050, 6058, 6074, 6090, 6092,
 
&gt;LuckyNumbers.exe -j=10000
Lucky number 10000=115591
 
&gt;LuckyNumbers.exe -j=10000 --evenLucky
Lucky even number 10000=111842</pre>
 
=={{header|D}}==
<syntaxhighlight 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;
}
}</syntaxhighlight>
 
{{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|F_Sharp|F#}}==
===The functions===
<syntaxhighlight lang="fsharp">
// Odd and Even Lucky Numbers. Nigel Galloway: October 3rd., 2020
let rec fN i g e l=seq{yield! i|>Seq.skip g|>Seq.take(e-g-1)
let n=Seq.chunkBySize e i|>Seq.collect(Seq.take(e-1)) in yield! fN n (e-1) (Seq.item l n) (l+1)}
let oLuck,eLuck=let rec fG g=seq{yield g; yield! fG(g+2)} in (fN(fG 1) 0 3 2,fN(fG 2) 0 4 2)
</syntaxhighlight>
 
===The task===
Using a fsi console:
{{out}}
<pre>
> oLuck|>Seq.take 20|>Seq.iter(printf "%d "); printfn "";;
1 3 7 9 13 15 21 25 31 33 37 43 49 51 63 67 69 73 75 79
 
> eLuck|>Seq.take 20|>Seq.iter(printf "%d "); printfn "";;
2 4 6 10 12 18 20 22 26 34 36 42 44 50 52 54 58 68 70 76
 
> oLuck|>Seq.skipWhile(fun n->n<6000)|>Seq.takeWhile(fun n->n<6100)|>Seq.iter(printf "%d "); printfn "";;
6009 6019 6031 6049 6055 6061 6079 6093
 
> eLuck|>Seq.skipWhile(fun n->n<6000)|>Seq.takeWhile(fun n->n<6100)|>Seq.iter(printf "%d "); printfn "";;
6018 6020 6022 6026 6036 6038 6050 6058 6074 6090 6092
</pre>
=={{header|Go}}==
{{trans|Kotlin}}
<syntaxhighlight lang="go">package main
 
import (
"fmt"
"log"
"os"
"strconv"
"strings"
)
 
const luckySize = 60000
 
var luckyOdd = make([]int, luckySize)
var luckyEven = make([]int, luckySize)
 
func init() {
for i := 0; i < luckySize; i++ {
luckyOdd[i] = i*2 + 1
luckyEven[i] = i*2 + 2
}
}
 
func filterLuckyOdd() {
for n := 2; n < len(luckyOdd); n++ {
m := luckyOdd[n-1]
end := (len(luckyOdd)/m)*m - 1
for j := end; j >= m-1; j -= m {
copy(luckyOdd[j:], luckyOdd[j+1:])
luckyOdd = luckyOdd[:len(luckyOdd)-1]
}
}
}
 
func filterLuckyEven() {
for n := 2; n < len(luckyEven); n++ {
m := luckyEven[n-1]
end := (len(luckyEven)/m)*m - 1
for j := end; j >= m-1; j -= m {
copy(luckyEven[j:], luckyEven[j+1:])
luckyEven = luckyEven[:len(luckyEven)-1]
}
}
}
 
func printSingle(j int, odd bool) error {
if odd {
if j >= len(luckyOdd) {
return fmt.Errorf("the argument, %d, is too big", j)
}
fmt.Println("Lucky number", j, "=", luckyOdd[j-1])
} else {
if j >= len(luckyEven) {
return fmt.Errorf("the argument, %d, is too big", j)
}
fmt.Println("Lucky even number", j, "=", luckyEven[j-1])
}
return nil
}
 
func printRange(j, k int, odd bool) error {
if odd {
if k >= len(luckyOdd) {
return fmt.Errorf("the argument, %d, is too big", k)
}
fmt.Println("Lucky numbers", j, "to", k, "are:")
fmt.Println(luckyOdd[j-1 : k])
} else {
if k >= len(luckyEven) {
return fmt.Errorf("the argument, %d, is too big", k)
}
fmt.Println("Lucky even numbers", j, "to", k, "are:")
fmt.Println(luckyEven[j-1 : k])
}
return nil
}
 
func printBetween(j, k int, odd bool) error {
var r []int
if odd {
max := luckyOdd[len(luckyOdd)-1]
if j > max || k > max {
return fmt.Errorf("at least one argument, %d or %d, is too big", j, k)
}
for _, num := range luckyOdd {
if num < j {
continue
}
if num > k {
break
}
r = append(r, num)
}
fmt.Println("Lucky numbers between", j, "and", k, "are:")
fmt.Println(r)
} else {
max := luckyEven[len(luckyEven)-1]
if j > max || k > max {
return fmt.Errorf("at least one argument, %d or %d, is too big", j, k)
}
for _, num := range luckyEven {
if num < j {
continue
}
if num > k {
break
}
r = append(r, num)
}
fmt.Println("Lucky even numbers between", j, "and", k, "are:")
fmt.Println(r)
}
return nil
}
 
func main() {
nargs := len(os.Args)
if nargs < 2 || nargs > 4 {
log.Fatal("there must be between 1 and 3 command line arguments")
}
filterLuckyOdd()
filterLuckyEven()
j, err := strconv.Atoi(os.Args[1])
if err != nil || j < 1 {
log.Fatalf("first argument, %s, must be a positive integer", os.Args[1])
}
if nargs == 2 {
if err := printSingle(j, true); err != nil {
log.Fatal(err)
}
return
}
 
if nargs == 3 {
k, err := strconv.Atoi(os.Args[2])
if err != nil {
log.Fatalf("second argument, %s, must be an integer", os.Args[2])
}
if k >= 0 {
if j > k {
log.Fatalf("second argument, %d, can't be less than first, %d", k, j)
}
if err := printRange(j, k, true); err != nil {
log.Fatal(err)
}
} else {
l := -k
if j > l {
log.Fatalf("second argument, %d, can't be less in absolute value than first, %d", k, j)
}
if err := printBetween(j, l, true); err != nil {
log.Fatal(err)
}
}
return
}
 
var odd bool
switch lucky := strings.ToLower(os.Args[3]); lucky {
case "lucky":
odd = true
case "evenlucky":
odd = false
default:
log.Fatalf("third argument, %s, is invalid", os.Args[3])
}
if os.Args[2] == "," {
if err := printSingle(j, odd); err != nil {
log.Fatal(err)
}
return
}
 
k, err := strconv.Atoi(os.Args[2])
if err != nil {
log.Fatal("second argument must be an integer or a comma")
}
if k >= 0 {
if j > k {
log.Fatalf("second argument, %d, can't be less than first, %d", k, j)
}
if err := printRange(j, k, odd); err != nil {
log.Fatal(err)
}
} else {
l := -k
if j > l {
log.Fatalf("second argument, %d, can't be less in absolute value than first, %d", k, j)
}
if err := printBetween(j, l, odd); err != nil {
log.Fatal(err)
}
}
}</syntaxhighlight>
 
{{out}}
<pre>
$ ./lucky 1 20
Lucky numbers 1 to 20 are:
[1 3 7 9 13 15 21 25 31 33 37 43 49 51 63 67 69 73 75 79]
 
$ ./lucky 1 20 evenLucky
Lucky even numbers 1 to 20 are:
[2 4 6 10 12 18 20 22 26 34 36 42 44 50 52 54 58 68 70 76]
 
$ ./lucky 6000 -6100
Lucky numbers between 6000 and 6100 are:
[6009 6019 6031 6049 6055 6061 6079 6093]
 
$ ./lucky 6000 -6100 evenLucky
Lucky even numbers between 6000 and 6100 are:
[6018 6020 6022 6026 6036 6038 6050 6058 6074 6090 6092]
 
$ ./lucky 10000 , lucky
Lucky number 10000 = 115591
 
$ ./lucky 10000 , evenLucky
Lucky even number 10000 = 111842
</pre>
 
=={{header|Haskell}}==
 
Haskell is a very nice language for this problem because it is a lazy language. Here regular expressions and data types are used.
<syntaxhighlight lang="haskell">
 
<lang Haskell>
import System.Environment
import Data.Char
import Text.Regex.Posix
 
Line 133 ⟶ 1,044:
 
nth :: Int -> Lucky -> Int
nth j l Lucky = luckyNumbers oddNumbers !! (j-1)
nth j l EvenLucky = luckyNumbers evenNumbers !! (j-1)
 
range :: Int -> Int -> Lucky -> [Int]
Line 174 ⟶ 1,085:
_ -> do
putStrLn "Invalid input, wrong number of arguments"
putStrLn "Type --help"</langsyntaxhighlight>
{{out}}
<pre>$ luckyNumbers 1 20
Line 195 ⟶ 1,106:
Implementation:
 
<langsyntaxhighlight Jlang="j">luckySeq=:3 :0
1 luckySeq y
:
Line 222 ⟶ 1,133:
)
 
thru=: <./ + i.@(+*)@-~</langsyntaxhighlight>
 
Task:
 
<langsyntaxhighlight Jlang="j"> program 1 20
1 3 7 9 13 15 21 25 31 33 37 43 49 51 63 67 69 73 75 79
program 1 20,evenLucky
Line 233 ⟶ 1,144:
6009 6019 6031 6049 6055 6061 6079 6093
program 6000,-6100,evenLucky
6018 6020 6022 6026 6036 6038 6050 6058 6074 6090 6092</langsyntaxhighlight>
 
Note that I've used the J command line rather than a unix or windows command line. This is because J is portable to a wide variety of environments (including phones) and there's no reliably common command line that exists across all these environments. Therefore, J must provide its own, and J's command line requires some slight syntax changes from the suggestions implicit in this task.
 
=={{header|Perl 6Java}}==
<syntaxhighlight lang="java">
<lang perl6>sub luck(\a,\b) {
import java.util.ArrayList;
gather {
import java.util.Collections;
my @taken = take a;
import java.util.List;
my @rotor;
my $i = b;
 
public class LuckyNumbers {
loop {
 
loop (my $j = 0; $j < @rotor; $j++) {
private static int MAX = 200000;
--@rotor[$j] or last;
private static List<Integer> luckyEven = luckyNumbers(MAX, true);
}
private static List<Integer> luckyOdd = luckyNumbers(MAX, false);
if $j < @rotor {
@rotor[$j] = @taken[$j+1];
public static void main(String[] args) {
}
// Case 1 and 2
else {
if ( args.length == 1 || ( args.length == 2 && args[1].compareTo("lucky") == 0 ) ) {
push @taken, take $i;
int n = Integer.parseInt(args[0]);
push @rotor, $i - @taken;
System.out.printf("LuckyNumber(%d) = %d%n", n, luckyOdd.get(n-1));
}
$i += 2; }
// Case 3
}
else if ( args.length == 2 && args[1].compareTo("evenLucky") == 0 ) {
int n = Integer.parseInt(args[0]);
System.out.printf("EvenLuckyNumber(%d) = %d%n", n, luckyEven.get(n-1));
}
// Case 4 through 9
else if ( args.length == 2 || args.length == 3 ) {
int j = Integer.parseInt(args[0]);
int k = Integer.parseInt(args[1]);
// Case 4 and 5
if ( ( args.length == 2 && k > 0 ) || (args.length == 3 && k > 0 && args[2].compareTo("lucky") == 0 ) ) {
System.out.printf("LuckyNumber(%d) through LuckyNumber(%d) = %s%n", j, k, luckyOdd.subList(j-1, k));
}
// Case 6
else if ( args.length == 3 && k > 0 && args[2].compareTo("evenLucky") == 0 ) {
System.out.printf("EvenLuckyNumber(%d) through EvenLuckyNumber(%d) = %s%n", j, k, luckyEven.subList(j-1, k));
}
// Case 7 and 8
else if ( ( args.length == 2 && k < 0 ) || (args.length == 3 && k < 0 && args[2].compareTo("lucky") == 0 ) ) {
int n = Collections.binarySearch(luckyOdd, j);
int m = Collections.binarySearch(luckyOdd, -k);
System.out.printf("Lucky Numbers in the range %d to %d inclusive = %s%n", j, -k, luckyOdd.subList(n < 0 ? -n-1 : n, m < 0 ? -m-1 : m+1));
}
// Case 9
else if ( args.length == 3 && k < 0 && args[2].compareTo("evenLucky") == 0 ) {
int n = Collections.binarySearch(luckyEven, j);
int m = Collections.binarySearch(luckyEven, -k);
System.out.printf("Even Lucky Numbers in the range %d to %d inclusive = %s%n", j, -k, luckyEven.subList(n < 0 ? -n-1 : n, m < 0 ? -m-1 : m+1));
}
}
}
private static List<Integer> luckyNumbers(int max, boolean even) {
List<Integer> luckyList = new ArrayList<>();
for ( int i = even ? 2 : 1 ; i <= max ; i += 2 ) {
luckyList.add(i);
}
int start = 1;
boolean removed = true;
while ( removed ) {
removed = false;
int increment = luckyList.get(start);
List<Integer> remove = new ArrayList<>();
for ( int i = increment-1 ; i < luckyList.size() ; i += increment ) {
remove.add(0, i);
removed = true;
}
for ( int i : remove ) {
luckyList.remove(i);
}
start++;
}
return luckyList;
}
 
}
</syntaxhighlight>
 
{{out}}
constant @lucky = luck(1,3);
<pre>
constant @evenlucky = luck(2,4);
C:\My_Data\software\EclipseOxygenProjects\Rosetta Code\bin>java LuckyNumbers 1 20
LuckyNumber(1) through LuckyNumber(20) = [1, 3, 7, 9, 13, 15, 21, 25, 31, 33, 37, 43, 49, 51, 63, 67, 69, 73, 75, 79]
 
C:\My_Data\software\EclipseOxygenProjects\Rosetta Code\bin>java LuckyNumbers 1 20 evenLucky
subset Luck where m:i/^ 'even'? 'lucky' $/;
EvenLuckyNumber(1) through EvenLuckyNumber(20) = [2, 4, 6, 10, 12, 18, 20, 22, 26, 34, 36, 42, 44, 50, 52, 54, 58, 68, 70, 76]
 
C:\My_Data\software\EclipseOxygenProjects\Rosetta Code\bin>java LuckyNumbers 6000 -6100
multi MAIN (Int $num where * > 0) {
Lucky Numbers in the range 6000 to 6100 inclusive = [6009, 6019, 6031, 6049, 6055, 6061, 6079, 6093]
say @lucky[$num-1];
 
C:\My_Data\software\EclipseOxygenProjects\Rosetta Code\bin>java LuckyNumbers 6000 -6100 evenLucky
Even Lucky Numbers in the range 6000 to 6100 inclusive = [6018, 6020, 6022, 6026, 6036, 6038, 6050, 6058, 6074, 6090, 6092]
 
C:\My_Data\software\EclipseOxygenProjects\Rosetta Code\bin>java LuckyNumbers 10000
LuckyNumber(10000) = 115591
 
C:\My_Data\software\EclipseOxygenProjects\Rosetta Code\bin>java LuckyNumbers 10000 evenLucky
EvenLuckyNumber(10000) = 111842
</pre>
 
=={{header|JavaScript}}==
First: the function.
<syntaxhighlight lang="javascript">
function luckyNumbers(opts={}) {
/**************************************************************************\
| OPTIONS |
|**************************************************************************|
| even ...... boolean ............. return even/uneven numbers |
| (default: false) |
| |
| nth ....... number ............... return nth number |
| |
| through ... number ............... return numbers from #1 to number |
| OR array[from, to] ... return numbers on index |
| from array[from] to array[to] |
| |
| range ..... array[from, to] ...... return numbers between from and to |
\**************************************************************************/
 
opts.even = opts.even || false;
if (typeof opts.through == 'number') opts.through = [0, opts.through];
let out = [],
x = opts.even ? 2 : 1,
max = opts.range ? opts.range[1] * 3
: opts.through ? opts.through[1] * 12
: opts.nth ? opts.nth * 15
: 2000;
 
for (x; x <= max; x = x+2) out.push(x); // fill
for (x = 1; x < Math.floor(out.length / 2); x++) { // sieve
let i = out.length;
while (i--)
(i+1) % out[x] == 0 && out.splice(i, 1);
}
 
if (opts.nth) return out[opts.nth-1];
if (opts.through) return out.slice(opts.through[0], opts.through[1]);
if (opts.range) return out.filter(function(val) {
return val >= opts.range[0] && val <= opts.range[1];
});
return out;
}
 
/* TESTING */
multi MAIN (Int $num where * > 0, ',', Luck $howlucky = 'lucky') {
// blank
say @::(lc $howlucky)[$num-1];
console.log( luckyNumbers() );
// showing the first twenty lucky numbers
console.log( luckyNumbers({through: 20}) );
// showing the first twenty even lucky numbers
console.log( luckyNumbers({even: true, through: 20}) );
// showing all lucky numbers between 6,000 and 6,100 (inclusive)
console.log( luckyNumbers({range: [6000, 6100]}) );
// showing all even lucky numbers in the same range as above
console.log( luckyNumbers({even: true, range: [6000, 6100]}) );
// showing the 10,000th lucky number (extra credit)
console.log( luckyNumbers({nth: 10000}) );
// showing the 10,000th even lucky number (extra credit)
console.log( luckyNumbers({even: true, nth: 10000}) );
</syntaxhighlight>
{{out}}<pre>
> Array(276) [ 1, 3, 7, 9, 13, 15, 21, 25, 31, 33, … ]
> Array(20) [ 1, 3, 7, 9, 13, 15, 21, 25, 31, 33, … ]
> Array(20) [ 2, 4, 6, 10, 12, 18, 20, 22, 26, 34, … ]
> Array(8) [ 6009, 6019, 6031, 6049, 6055, 6061, 6079, 6093 ]
> Array(11) [ 6018, 6020, 6022, 6026, 6036, 6038, 6050, 6058, 6074, 6090, … ]
> 115591
> 111842
</pre>
 
And that's just pretty much it.
 
But now: our line interpreter!
 
We'll use an HTML-prompt here, because JavaScript actually doesn't have a command interface on it's own.
 
<syntaxhighlight lang="javascript">
(function() {
document.write(`
<table>
<tr><th>argument(s)<br>space separated</th><th>gives</th></tr>
<tr><td>$j</td><td><i>j</i><sup>th</sup> lucky number</tr>
<tr><td>$j lucky</td><td><i>j</i><sup>th</sup> lucky number</tr>
<tr><td>$j evenLucky</td><td><i>j</i><sup>th</sup> even lucky number</tr>
<tr><td>&nbsp;</td><td>&nbsp;</td></tr>
<tr><td>$j $k</td><td><i>J</i><sup>th</sup> through <i>k</i><sup>th</sup> (inclusive) lucky numbers</td></tr>
<tr><td>$j $k lucky</td><td><i>J</i><sup>th</sup> through <i>k</i><sup>th</sup> (inclusive) lucky numbers</td></tr>
<tr><td>$j $k evenLucky</td><td><i>J</i><sup>th</sup> through <i>k</i><sup>th</sup> (inclusive) even lucky numbers</td></tr>
<tr><td>&nbsp;</td><td>&nbsp;</td></tr>
<tr><td>-$j</td><td>all lucky numbers in the range 1 .. |<i>j</i>|*</tr>
<tr><td>$j -$k</td><td>all lucky numbers in the range <i>j</i> .. |<i>k</i>|*</tr>
<tr><td>$j -$k lucky</td><td>all lucky numbers in the range <i>j</i> .. |<i>k</i>|*</tr>
<tr><td>$j -$k evenLucky</td><td>all even lucky numbers in the range <i>j</i> .. |<i>k</i>|*</tr>
</table>
<p>* where |<i>x</i>| is the absolute value of <i>x</i></p>
`);
let args = prompt('Enter a luckyNumbers argument string:').split(' '),
numbers = [],
strings = [],
obj = {}, // object to pass to our luckyNumbers function
msg = '';
for (let x = 0; x < args.length; x++) {
if (isNaN(parseInt(args[x]))) strings.push(args[x].toUpperCase());
else numbers.push(parseInt(args[x]));
}
// check strings
switch (true) {
case (strings.length > 1):
msg += 'Too many arguments.';
break;
case (strings.length == 0): break;
case (strings[0] != 'LUCKY' && strings[0] != 'EVENLUCKY'):
msg += `Unknown argument: ${strings[0]}.`;
break;
case (strings[0] == 'EVENLUCKY'): obj.even = true;
}
// check numbers
switch (true) {
case (numbers.length == 0):
msg += 'Missing number argument.';
break;
case (numbers.length > 2):
msg += 'Too many number arguments.';
break;
case (numbers.length == 1):
if (numbers[0] > 0) obj.nth = numbers[0];
else obj.through = Math.abs(numbers[0]);
break;
case (numbers.length == 2):
// both negative
if (numbers[0] < 0 && numbers[1] < 0) {
msg += 'Missing positive argument';
break;
}
// negative + positive
numbers.sort();
if (numbers[0] < 0 && numbers[1] > 0) {
obj.range = [numbers[1], Math.abs(numbers[0])];
break;
}
// both positive
if (numbers[0] > 0 && numbers[1] > 0) {
obj.through = [numbers[0]-1, numbers[1]];
}
}
if (msg.length > 0)
document.write(`<p>${msg}<br>Reload to try again</p>`);
else {
let res;
try { res = luckyNumbers(obj); }
catch (err) {
document.write(`<p>${err}<br>Reload to try again</p>`);
}
finally {
if (typeof res == 'object' ) res = res.join(', ');
document.write(`
<p>
<b>Input: </b>${args.join(' ')}<br>
<b>Result: </b>${res}
</p>
<p>Reload to continue</p>
`)
}
}
})();
</syntaxhighlight>
Using the task demonstrations again, the I/O part looks like:
{{out}}<pre>
Input: 1 20
Result: 1, 3, 7, 9, 13, 15, 21, 25, 31, 33, 37, 43, 49, 51, 63, 67, 69, 73, 75, 79
 
Input: -20 evenLucky
Result: 2, 4, 6, 10, 12, 18, 20, 22, 26, 34, 36, 42, 44, 50, 52, 54, 58, 68, 70, 76
 
Input: 6000 -6100
Result: 6009, 6019, 6031, 6049, 6055, 6061, 6079, 6093
 
Input: -6100 evenlucky 6000
Result: 6018, 6020, 6022, 6026, 6036, 6038, 6050, 6058, 6074, 6090, 6092
 
Input: 10000
Result: 115591
 
Input: Evenlucky 10000
Result: 111842
</pre>
 
=={{header|jq}}==
'''Works with jq and jaq, the C and Rust implementions of jq'''
 
Also works with gojq, the Go implementation, but gojq requires
a very large amount of memory to complete the last task (finding the 10,000th even lucky number).
 
The following program has been written to run using the C, Go and Rust
implementations of jq and is therefore not as succinct as it would
otherwise have been. Also, the mechanisms for passing in command-line
parameters differ amongst the three implementations, so the program presented here expects the
parameters to be presented on a single line as input. Thus a possible
invocation would be:
 
<pre>
JQ -Rrcf program.jq <<< '1 20'
</pre>
 
The disadvantage of the sieve-based approach used below is the
difficulty of choosing a sieve size that is on the one hand sufficient
but on the other frugal. No claim is made for the algorithm used to
determine the sieve size here other than it is reasonable for problems
sufficiently similar to the ones solved here. However, if the sieve size
turns out to be insufficient, an error condition will be raised.
<syntaxhighlight lang=jq>
# Preliminaries:
# This def can be omitted if using jq or gojq:
def range($a;$b;$c): $a | while(. < $b; .+$c);
 
# jaq does not support string interpolation so, for brevity:
def tos: tostring;
 
# The following def should be modified if an alternative mechanism for
# reading the parameter arguments is used:
def args: {args: [splits(" *")]};
 
def argsSize: .args|length;
 
# Input: an array
# Emit the items in the array until the condition on the items is met.
# The item for which the condition is first met is NOT emitted.
def emit_until(cond):
length as $length
| if $length == 0 or (.[0]|cond) then empty
else . as $in
| {i:0, ok: true}
| while(.ok;
.i+=1
| .ok = .i < $length and ($in[.i] | cond | not) )
| $in[.i]
end;
 
# input: a number or a string
# If the input is or can be converted to a number using tonumber, then return the number
# else return false.
def isnumber:
if type == "number" then .
elif type == "string" then tonumber? // false
else false
end;
 
# input: anything
# output: a boolean
def isinteger:
if type == "number" then . == floor
else isnumber as $x
| if $x then $x | (. == floor) else false end
end;
 
# mark and collapse
def removeEvery($n):
reduce range($n-1; length; $n) as $i (.; .[$i]=null)
| map(select(.));
 
# Input: {lucky}
def filterLucky:
(.lucky|length) as $length
| .n = 2
| until( .n >= $length;
.lucky[.n-1] as $m
| if $m then .lucky |= removeEvery($m) | .n += 1
else .n = $length
end )
| del(.n);
 
def printSingle($j):
if $j >= 1 + (.lucky|length) then "Argument is too big" | error
else
(if .odd then "Lucky number #" else "Lucky even number #" end)
+ ($j|tos) + " = " + (.lucky[$j-1]|tos)
end;
 
# like jq's range($j-1; $k)
def printRange($j; $k):
if $k >= (.lucky|length) then "Argument " + ($k|tos) + " is too big" | error
elif .odd
then "Lucky numbers " + ($j|tos) + " to " + ($k|tos) + " inclusive are:",
.lucky[$j-1:$k]
else "Lucky even numbers " + ($j|tos) + " to " + ($k|tos) + " inclusive are:",
.lucky[$j-1:$k]
end;
 
def printBetween($j; $k):
.lucky[-1] as $max
| if $j > $max or $k > $max
then "At least one argument is too big" | error
else
(if .odd
then "Lucky numbers between " + ($j|tos) + " and " + ($k|tos) + " are:"
else "Lucky even numbers between " + ($j|tos) + " and " + ($k|tos) + " are:"
end),
[.lucky | emit_until(. > $k) | select(. >= $j)]
end;
 
def odd:
if argsSize < 3 then true
else (.args[2] | tostring | ascii_downcase)
| if . == "lucky" or . == "odd" then true
elif . == "evenlucky" or . == "even" then false
else "Third argument " + (.args[2]|tos) + " is invalid" | error
end
end;
 
# Input: {odd}
def init($n):
if .odd
then .lucky = reduce range(0;$n) as $i (null; . + [$i*2 + 1])
else .lucky = reduce range(0;$n) as $i (null; . + [$i*2 + 2])
end;
 
# Emit {args, j, k, odd, single} or raise an error
def gatherArgs:
args
| if argsSize | (. < 1 or . > 3)
then "There must be between 1 and 3 command line arguments" | error
else .j = (.args[0] | isnumber )
| .k = (.args[1] | isnumber )
| .single = (argsSize == 1 or .args[1] == ",")
| .odd = odd
| if (.j | isinteger | not) or (.j < 1)
then "First argument " + (.args[0]|tos) + " must be a positive integer" | error
elif argsSize >= 2 and .args[1] != "," and (.k | isinteger | not)
then "Second argument " + (.args[1]|tos) + " must be an integer or ," | error
else .
end
| if .k and .k >= 0 and .j > .k
then "Second argument cannot be less than first" | error
else .
end
end ;
def start:
gatherArgs
| (if .k then (if .k > 0 then .k else null end) else .j end) as $size
| (if $size then ($size | . * (tostring|length|.*.)) else -.k end) as $size
| init($size)
| filterLucky
| if .single then printSingle(.j)
elif .k > 0 then printRange(.j; .k)
else .k |= - .
| if (.j > .k)
then "The second argument cannot be less in absolute value than first" | error
else printBetween(.j; .k)
end
end;
 
start
</syntaxhighlight>
{{output}}
<pre>
Lucky numbers 1 to 20 inclusive are:
[1,3,7,9,13,15,21,25,31,33,37,43,49,51,63,67,69,73,75,79]
 
Lucky even numbers 1 to 20 inclusive are:
[2,4,6,10,12,18,20,22,26,34,36,42,44,50,52,54,58,68,70,76]
 
Lucky numbers between 6000 and 6100 are:
[6009,6019,6031,6049,6055,6061,6079,6093]
 
Lucky even numbers between 6000 and 6100 are:
[6018,6020,6022,6026,6036,6038,6050,6058,6074,6090,6092]
 
Lucky number #1000 = 8809
 
Lucky even number #10000 = 111842
</pre>
 
=={{header|Julia}}==
This iterator for lucky numbers is semi-lazy: it completes one pass of the filter each iteration.
<syntaxhighlight lang="julia">using Base, StringDistances
 
struct Lucky
start::Int
nmax::Int
Lucky(iseven, nmax) = new(iseven ? 2 : 1, nmax)
end
 
struct LuckyState
nextindex::Int
sequence::Vector{Int}
end
 
Base.eltype(iter::Lucky) = Int
 
function Base.iterate(iter::Lucky, state = LuckyState(1, collect(iter.start:2:iter.nmax)))
if length(state.sequence) < state.nextindex
return nothing
elseif state.nextindex == 1
return (iter.start, LuckyState(2, state.sequence))
end
result = state.sequence[state.nextindex]
newsequence = Vector{Int}()
for (i, el) in enumerate(state.sequence)
if i % result != 0
push!(newsequence, el)
end
end
(result, LuckyState(state.nextindex + 1, newsequence))
end
 
function luckyindex(j, wanteven, k=0)
topindex = max(j, k) + 4
luck = Lucky(wanteven, topindex * 20)
iter_result = iterate(luck)
while iter_result != nothing
(elem, state) = iter_result
iter_result = iterate(luck, state)
if iter_result != nothing && iter_result[2].nextindex > topindex
return iter_result[2].sequence[k > j ? (j:k) : j]
end
end
throw("Index $j out of range for nmax of $(luck.nmax).")
end
function luckyrange(j, k, wanteven)
topvalue = max(j, k)
luck = Lucky(wanteven, topvalue + 1)
iter_result = iterate(luck)
(elem, state) = iter_result # save next to last result
while iter_result != nothing
(elem, state) = iter_result
iter_result = iterate(luck, state)
end
filter(x -> (j <= x <= k), state.sequence)
end
 
function helpdisplay(exitlevel=1)
println("\n", PROGRAM_FILE, " j [-][k] [lucky|evenLucky]")
println("\tj: index wanted or a starting point (index or value)",
"\n\tk: optional ending point (index), \n\t-k: optional ending point (value)\n")
helpstring =
""" | Argument(s) | What is printed |
|--------------------------------------------------------------------------|
| j | jth lucky number (required argument) |
| j , lucky | jth lucky number |
| j , evenLucky | jth even lucky number |
| j k | jth through kth (inclusive) lucky numbers |
| j k lucky | jth through kth (inclusive) lucky numbers |
| j k evenLucky | jth through kth (inclusive) even lucky numbers |
| j -k | all lucky numbers in the value range [m, |k|] |
| j -k lucky | all lucky numbers in the value range [m, |k|] |
| j -k evenLucky | all even lucky numbers in the value range [m, |k|] |
|--------------------------------------------------------------------------|\n\n"""
 
println(helpstring)
exit(exitlevel)
end
 
function parsecommandline()
comma = false
evenLucky = false
range = false
j = k = 0
if length(ARGS) < 1
helpdisplay()
end
for (pos, arg) in enumerate(ARGS)
if pos == 1
j = tryparse(Int, arg)
if j == nothing
println("The first argument must be a positive integer.\n")
helpdisplay()
end
elseif pos == 2 || (pos == 3 && comma)
k = tryparse(Int, arg)
if k == nothing
k = 0
if arg == ","
comma = true
continue
elseif arg == "lucky"
continue
elseif arg == "evenLucky"
evenLucky = true
elseif compare(Hamming(), arg, "lucky") > 0.4 || compare(Hamming(), arg, "evenLucky") > 0.4
println("Did you misspell \"lucky\" or \"evenLucky\"? Check capitalization.\n")
helpdisplay()
else
helpdisplay()
end
elseif k < 0
k = -k
range = true
end
elseif pos == 3 || pos == 4 && comma
if arg == ","
comma = true
continue
elseif arg == "lucky"
continue
elseif arg == "evenLucky"
evenLucky = true
elseif compare(Hamming(), arg, "lucky") > 0.1 || compare(Hamming(), arg, "evenLucky") > 0.1
println("Did you misspell "\lucky\" or "\evenLucky\"?\n\n")
helpdisplay()
else
helpdisplay()
end
elseif arg == "lucky"
continue
elseif arg == "evenLucky"
evenLucky = true
else
println("Too many arguments.\n")
helpdisplay()
end
end
(j, k, evenLucky, range)
end
 
function runopts()
(j, k, evenLucky, range) = parsecommandline()
if j < 1 || (k != 0 && j >= k)
throw("Lucky number integer parameters out of range: $(typeof(j)), $j, $(typeof(k)), $k")
end
if range
println(luckyrange(j, k, evenLucky))
else
println(luckyindex(j, evenLucky, k))
end
end
 
runopts()</syntaxhighlight> {{output}} <pre>
> julia luckymath.jl 1 20
[1, 3, 7, 9, 13, 15, 21, 25, 31, 33, 37, 43, 49, 51, 63, 67, 69, 73, 75, 79]
> julia luckymath.jl 1 20 evenLucky
[2, 4, 6, 10, 12, 18, 20, 22, 26, 34, 36, 42, 44, 50, 52, 54, 58, 68, 70, 76]
> julia luckymath.jl 6000 -6100
[6009, 6019, 6031, 6049, 6055, 6061, 6079, 6093]
> julia luckymath.jl 6000 -6100 evenLucky
[6018, 6020, 6022, 6026, 6036, 6038, 6050, 6058, 6074, 6090, 6092]
> julia luckymath.jl 10000
115591
> julia luckymath.jl 10000 evenLucky
111842
</pre>
 
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">// version 1.1.51
 
typealias IAE = IllegalArgumentException
 
val luckyOdd = MutableList(100000) { it * 2 + 1 }
val luckyEven = MutableList(100000) { it * 2 + 2 }
 
fun filterLuckyOdd() {
var n = 2
while (n < luckyOdd.size) {
val m = luckyOdd[n - 1]
val end = (luckyOdd.size / m) * m - 1
for (j in end downTo m - 1 step m) luckyOdd.removeAt(j)
n++
}
}
 
fun filterLuckyEven() {
multi MAIN (Int $first where * > 0, Int $last where * > 0, Luck $howlucky = 'lucky') {
var n = 2
say @::(lc $howlucky)[$first-1 .. $last - 1];
while (n < luckyEven.size) {
val m = luckyEven[n - 1]
val end = (luckyEven.size / m) * m - 1
for (j in end downTo m - 1 step m) luckyEven.removeAt(j)
n++
}
}
 
fun printSingle(j: Int, odd: Boolean) {
multi MAIN (Int $min where * > 0, Int $neg-max where * < 0, Luck $howlucky = 'lucky') {
if (odd) {
say grep * >= $min, (@::(lc $howlucky) ...^ * > abs $neg-max);
if (j >= luckyOdd.size) throw IAE("Argument is too big")
}</lang>
println("Lucky number $j = ${luckyOdd[j - 1]}")
}
else {
if (j >= luckyEven.size) throw IAE("Argument is too big")
println("Lucky even number $j = ${luckyEven[j - 1]}")
}
}
 
fun printRange(j: Int, k: Int, odd: Boolean) {
if (odd) {
if (k >= luckyOdd.size) throw IAE("Argument is too big")
println("Lucky numbers $j to $k are:\n${luckyOdd.drop(j - 1).take(k - j + 1)}")
}
else {
if (k >= luckyEven.size) throw IAE("Argument is too big")
println("Lucky even numbers $j to $k are:\n${luckyEven.drop(j - 1).take(k - j + 1)}")
}
}
 
fun printBetween(j: Int, k: Int, odd: Boolean) {
val range = mutableListOf<Int>()
if (odd) {
val max = luckyOdd[luckyOdd.lastIndex]
if (j > max || k > max) {
throw IAE("At least one argument is too big")
}
for (num in luckyOdd) {
if (num < j) continue
if (num > k) break
range.add(num)
}
println("Lucky numbers between $j and $k are:\n$range")
}
else {
val max = luckyEven[luckyEven.lastIndex]
if (j > max || k > max) {
throw IAE("At least one argument is too big")
}
for (num in luckyEven) {
if (num < j) continue
if (num > k) break
range.add(num)
}
println("Lucky even numbers between $j and $k are:\n$range")
}
}
 
fun main(args: Array<String>) {
if (args.size !in 1..3) throw IAE("There must be between 1 and 3 command line arguments")
filterLuckyOdd()
filterLuckyEven()
val j = args[0].toIntOrNull()
if (j == null || j < 1) throw IAE("First argument must be a positive integer")
if (args.size == 1) { printSingle(j, true); return }
 
if (args.size == 2) {
val k = args[1].toIntOrNull()
if (k == null) throw IAE("Second argument must be an integer")
if (k >= 0) {
if (j > k) throw IAE("Second argument can't be less than first")
printRange(j, k, true)
}
else {
val l = -k
if (j > l) throw IAE("The second argument can't be less in absolute value than first")
printBetween(j, l, true)
}
return
}
 
var odd =
if (args[2].toLowerCase() == "lucky") true
else if (args[2].toLowerCase() == "evenlucky") false
else throw IAE("Third argument is invalid")
 
if (args[1] == ",") {
printSingle(j, odd)
return
}
 
val k = args[1].toIntOrNull()
if (k == null) throw IAE("Second argument must be an integer or a comma")
 
if (k >= 0) {
if (j > k) throw IAE("Second argument can't be less than first")
printRange(j, k, odd)
}
else {
val l = -k
if (j > l) throw IAE("The second argument can't be less in absolute value than first")
printBetween(j, l, odd)
}
}</syntaxhighlight>
 
{{out}}
<pre>
$ java -jar lucky.jar 1 20
Lucky numbers 1 to 20 are:
[1, 3, 7, 9, 13, 15, 21, 25, 31, 33, 37, 43, 49, 51, 63, 67, 69, 73, 75, 79]
 
$ java -jar lucky.jar 1 20 evenLucky
Lucky even numbers 1 to 20 are:
[2, 4, 6, 10, 12, 18, 20, 22, 26, 34, 36, 42, 44, 50, 52, 54, 58, 68, 70, 76]
 
$ java -jar lucky.jar 6000 -6100
Lucky numbers between 6000 and 6100 are:
[6009, 6019, 6031, 6049, 6055, 6061, 6079, 6093]
 
$ java -jar lucky.jar 6000 -6100 evenLucky
Lucky even numbers between 6000 and 6100 are:
[6018, 6020, 6022, 6026, 6036, 6038, 6050, 6058, 6074, 6090, 6092]
 
$ java -jar lucky.jar 10000 , lucky
Lucky number 10000 = 115591
 
$ java -jar lucky.jar 10000 , evenLucky
Lucky even number 10000 = 111842
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Save the following code in the file script.wls and execute the wolframscript from the command line:
<syntaxhighlight lang="mathematica">ClearAll[GetLuckies, GetEvenLuckies]
GetLuckies[max_] := Module[{luckies, f, i},
luckies = Range[1, max, 2];
f[n_] := Block[{k = luckies[[n]]}, luckies = Delete[luckies, Table[{k}, {k, k, Length@luckies, k}]]];
i = 2;
While[i < Length[luckies],
f[i];
i++
];
luckies
]
GetEvenLuckies[max_] := Module[{lst, i, len},
lst = Range[2, max, 2];
i = 2;
While[i <= (len = Length@lst) && (k = lst[[i]]) <= len,
lst = Drop[lst, {k, len, k}];
i++
];
lst
]
GiveLucky[s_String] := GiveLucky[StringSplit[s, " "]]
GiveLucky[args_List] := Module[{argc = Length[args], j, k, type},
If[argc > 3,
Print["Too many arguments"]
,
If[argc == 0,
Print["Too few arguments"]
,
Switch[argc, 1,
j = Interpreter["Integer"][args[[1]]];
If[! FailureQ[j],
If[j > 0,
Print@GetLuckies[20 j][[j]]
,
Print["j should be positive"]
]
,
Print["one argument that is not an integer"]
]
, 2,
j = Interpreter["Integer"][args[[1]]];
k = Interpreter["Integer"][args[[2]]];
If[! FailureQ[j] \[And] ! FailureQ[k],
If[j > 0,
If[k > 0,
Print@GetLuckies[20 k][[j ;; k]]
,
Print@Select[GetLuckies[-k], GreaterEqualThan[j]]
]
,
Print["j should be positive"]
]
,
Print["one of the two arguments is not an integer\[Ellipsis]"]
]
, 3,
If[args[[2]] === ",",
j = Interpreter["Integer"][args[[1]]];
If[! FailureQ[j],
If[j > 0,
type = args[[3]];
If[MatchQ[type, "evenLucky" | "lucky"],
If[type === "evenLucky",
Print@GetEvenLuckies[20 j][[j]]
,
Print@GetLuckies[20 j][[j]]
]
,
Print["unknown type ", type]
]
,
Print["j should be positive"]
]
,
Print["j should be an integer"]
]
,
j = Interpreter["Integer"][args[[1]]];
k = Interpreter["Integer"][args[[2]]];
If[! FailureQ[j] \[And] ! FailureQ[k],
If[j > 0,
type = args[[3]];
If[k > 0,
If[MatchQ[type, "evenLucky" | "lucky"],
If[type === "evenLucky",
Print@GetEvenLuckies[20 k][[j ;; k]]
,
Print@GetLuckies[20 k][[j ;; k]]
]
,
Print["unknown type ", type]
]
,
If[MatchQ[type, "evenLucky" | "lucky"],
If[type === "evenLucky",
Print@Select[GetEvenLuckies[-k], GreaterEqualThan[j]]
,
Print@Select[GetLuckies[-k], GreaterEqualThan[j]]
]
,
Print["unknown type ", type]
]
]
,
Print["j should be positive"]
]
,
Print["one of the two arguments is not an integer\[Ellipsis]"]
]
]
]
]
]
]
GiveLucky[Last@$ScriptCommandLine]</syntaxhighlight>
{{out}}
<pre>wolframscript -file script.wls -args "1 20 lucky"
{1, 3, 7, 9, 13, 15, 21, 25, 31, 33, 37, 43, 49, 51, 63, 67, 69, 73, 75, 79}
wolframscript -file script.wls -args "1 20 evenLucky"
{2, 4, 6, 10, 12, 18, 20, 22, 26, 34, 36, 42, 44, 50, 52, 54, 58, 68, 70, 76}
wolframscript -file script.wls -args "6000 -6100"
{6009, 6019, 6031, 6049, 6055, 6061, 6079, 6093}
wolframscript -file script.wls -args "6000 -6100 evenLucky"
{6018, 6020, 6022, 6026, 6036, 6038, 6050, 6058, 6074, 6090, 6092}
wolframscript -file script.wls -args "10000 , lucky"
115591
wolframscript -file script.wls -args "10000 , evenLucky"
111842</pre>
 
=={{header|Nim}}==
For the generation of lucky numbers, we use the second Python algorithm, modified to return a sequence.
<syntaxhighlight lang="nim">import os, strformat, strutils
 
type LuckyKind {.pure.} = enum Lucky = "lucky", EvenLucky = "evenlucky"
 
const NoValue = 0 # Indicates that no value have been specified.
 
 
####################################################################################################
# Lucky numbers generation.
 
func initLuckyNumbers(nelems: int; kind: LuckyKind): seq[int] =
## Initialize a list of lucky numbers.
result = newSeqOfCap[int](nelems)
for i in 0..<nelems:
var k = i
for j in countdown(result.high, 1):
k = k * result[j] div (result[j] - 1)
result.add 2 * k + 1 + ord(kind)
 
 
####################################################################################################
# Printing.
 
template name(kind: LuckyKind): string =
if kind == Lucky: "Lucky" else: "Even lucky"
 
proc printSingle(j: int; kind: LuckyKind) =
## Print the lucky number at a given index.
let luckySeq = initLuckyNumbers(j, kind)
echo &"{name(kind)} number at index {j} is {luckySeq[j - 1]}"
 
proc printRange(j, k: int; kind: LuckyKind) =
## print the luck numbers in a range of indexes.
let luckySeq = initLuckyNumbers(k, kind)
var list = &"{name(kind)} numbers at indexes {j} to {k} are: "
let start = list.len
for idx in (j - 1)..(k - 1):
list.addSep(", ", start)
list.add $luckySeq[idx]
echo list
 
proc printInRange(j, k: int; kind: LuckyKind) =
## Print the lucky numbers in a range of values.
let luckySeq = initLuckyNumbers(k, kind) # "k" is greater than needed.
var list = &"{name(kind)} numbers between {j} to {k} are: "
let start = list.len
for val in luckySeq:
if val > k: break
if val > j:
list.addSep(", ", start)
list.add $val
echo list
 
 
####################################################################################################
# Command line parsing.
 
proc parseCommandLine(): tuple[j, k: int; kind: LuckyKind] =
## Parse the command line.
 
# Internal exception to catch invalid argument value.
type InvalidArgumentError = object of ValueError
 
template raiseError(message, value = "") =
## Raise an InvalidArgumentError.
raise newException(InvalidArgumentError, message & value & '.')
 
 
result = (Novalue, Novalue, Lucky)
 
try:
 
if paramCount() notin 1..3: raiseError "Wrong number of arguments"
 
# First argument: "j" value.
let p1 = paramStr(1)
try:
result.j = parseInt(p1)
if result.j <= 0: raiseError "Expected a positive number, got: ", p1
except ValueError:
raiseError "Expected an integer, got: ", p1
 
# Second argument: "k" value or a comma.
if paramCount() > 1:
let p2 = paramStr(2)
if p2 == ",":
# Must be followed by the kind of lucky number.
if paramCount() != 3: raiseError "Missing kind argument"
else:
try:
result.k = parseInt(p2)
if result.k == 0: raiseError "Expected a non null number, got: ", p2
except ValueError:
raiseError "Expected an integer, got: ", p2
 
# Third argument: number kind.
if paramCount() == 3:
let p3 = paramStr(3)
try:
result.kind = parseEnum[LuckyKind](p3.toLowerAscii())
except ValueError:
raiseError "Wrong kind: ", p3
 
except InvalidArgumentError:
quit getCurrentExceptionMsg()
 
 
#———————————————————————————————————————————————————————————————————————————————————————————————————
# Main program.
 
let (j, k, kind) = parseCommandLine()
 
if k == NoValue:
# Print jth value.
printSingle(j, kind)
 
elif k > 0:
# Print jth to kth values.
printRange(j, k, kind)
 
else:
# Print values in range j..(-k).
printInRange(j, -k, kind)</syntaxhighlight>
 
{{out}}
<pre>./lucky_numbers 1 20
Lucky numbers at indexes 1 to 20 are: 1, 3, 7, 9, 13, 15, 21, 25, 31, 33, 37, 43, 49, 51, 63, 67, 69, 73, 75, 79
 
./lucky_numbers 1 20 evenlucky
Even lucky numbers at indexes 1 to 20 are: 2, 4, 6, 10, 12, 18, 20, 22, 26, 34, 36, 42, 44, 50, 52, 54, 58, 68, 70, 76
 
./lucky_numbers 6000 -6100
Lucky numbers between 6000 to 6100 are: 6009, 6019, 6031, 6049, 6055, 6061, 6079, 6093
 
./lucky_numbers 6000 -6100 evenlucky
Even lucky numbers between 6000 to 6100 are: 6018, 6020, 6022, 6026, 6036, 6038, 6050, 6058, 6074, 6090, 6092
 
./lucky_numbers 10000 , lucky
Lucky number at index 10000 is 115591
 
./lucky_numbers 10000 , evenlucky
Even lucky number at index 10000 is 111842</pre>
 
=={{header|Perl}}==
The module <code>Perl6::GatherTake</code> emulates the Raku gather/take syntax, and allows us to access values from what acts (mostly) like a lazy list.
{{trans|Raku}}
<syntaxhighlight lang="perl">use Perl6::GatherTake;
 
sub luck {
my($a,$b) = @_;
 
gather {
my $i = $b;
my(@taken,@rotor,$j);
 
take 0; # 0th index is a placeholder
push @taken, take $a;
 
while () {
for ($j = 0; $j < @rotor; $j++) {
--$rotor[$j] or last;
}
if ($j < @rotor) {
$rotor[$j] = $taken[$j+1];
}
else {
take $i;
push @taken, $i;
push @rotor, $i - @taken;
}
$i += 2;
}
}
}
 
# fiddle with user input
$j = shift || usage();
$k = shift || ',';
$l = shift || 'lucky';
usage() unless $k =~ /,|-?\d+/;
usage() unless $l =~ /^(even)?lucky$/i;
sub usage { print "Args must be: j [,|k|-k] [lucky|evenlucky]\n" and exit }
 
# seed the iterator
my $lucky = $l =~ /^l/i ? luck(1,3) : luck(2,4);
 
# access values from 'lazy' list
if ($k eq ',') {
print $lucky->[$j]
} elsif ($k > $j) {
print $lucky->[$_] . ' ' for $j..$k
} elsif ($k < 0) {
while () { last if abs($k) < $lucky->[$i++] } # must first extend the array
print join ' ', grep { $_ >= $j and $_ <= abs($k) } @$lucky
}
 
print "\n"</syntaxhighlight>
{{out}}
<pre>$ ./lucky
Args must be: j [,|k|-k] [lucky|evenlucky]
Usage:
./lucky <num>
./lucky <num> , [<howlucky>]
./lucky <first> <last> [<howlucky>]
./lucky <min> <neg-max> [<howlucky>]
$ ./lucky 20 , lucky
79
Line 301 ⟶ 2,284:
$ ./lucky 10000
115591
$ ./lucky 10000 , EVENLUCKY
111842</pre>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">luckyMax</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">120000</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">lucky</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">filterLucky</span><span style="color: #0000FF;">()</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">n</span><span style="color: #0000FF;"><=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lucky</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">m</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">lucky</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">],</span>
<span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">m</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lucky</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">k</span><span style="color: #0000FF;">,</span><span style="color: #000000;">m</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">l</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #000000;">lucky</span><span style="color: #0000FF;">[</span><span style="color: #000000;">l</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">lucky</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">>=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lucky</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">lucky</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">lucky</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">l</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">helptxt</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
argument(s) | what is displayed
=======================================
j | jth lucky number
j [,] lucky | jth lucky number
j [,] evenLucky | jth even lucky number
j k | jth through kth (inclusive) lucky numbers
j k lucky | jth through kth (inclusive) lucky numbers
j k evenLucky | jth through kth (inclusive) even lucky numbers
j -k | all lucky numbers in the range j to k
j -k lucky | all lucky numbers in the range j to k
j -k evenLucky | all even lucky numbers in the range j to k
"""</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">fatal</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">msg</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">msg</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">helptxt</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">abort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">process</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">cl</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">--
-- Allow eg "lucky j , evenLucky" to be == "lucky j evenLucky"
--</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cl</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">3</span> <span style="color: #008080;">and</span> <span style="color: #000000;">cl</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">","</span> <span style="color: #008080;">then</span> <span style="color: #000000;">cl</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">k</span>
<span style="color: #004080;">bool</span> <span style="color: #000000;">single</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">range</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">oddluck</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cl</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">cli</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">cl</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">cli</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]<=</span><span style="color: #008000;">'9'</span> <span style="color: #008080;">then</span> <span style="color: #000080;font-style:italic;">-- (includes '-')</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">scanf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cl</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">fatal</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"unrecognised "</span><span style="color: #0000FF;">&</span><span style="color: #000000;">cli</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">></span><span style="color: #000000;">2</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">fatal</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"too many numbers"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;"><</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">fatal</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"first argument must be a positive integer"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">j</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span>
<span style="color: #008080;">else</span>
<span style="color: #000000;">single</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;"><</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">range</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">n</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;"><</span><span style="color: #000000;">j</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">fatal</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"second argument cannot be less than first"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">else</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cli</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"lucky"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"evenLucky"</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">fatal</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"unrecognised "</span><span style="color: #0000FF;">&</span><span style="color: #000000;">cli</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">!=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cl</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">fatal</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cli</span><span style="color: #0000FF;">&</span><span style="color: #008000;">" must be last parameter"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">oddluck</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">l</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">lucky</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">luckyMax</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">-</span><span style="color: #000000;">oddluck</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">filterLucky</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Output when args are %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cl</span><span style="color: #0000FF;">)})</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">evenstr</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">oddluck</span><span style="color: #0000FF;">?</span><span style="color: #008000;">""</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"even "</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">single</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">></span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lucky</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">fatal</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"the argument, %d, is too big"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Lucky %snumber %d = %d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">evenstr</span><span style="color: #0000FF;">,</span><span style="color: #000000;">j</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">lucky</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]})</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">range</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">></span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lucky</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">fatal</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"the argument, %d, is too big"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Lucky %snumbers %d to %d are: %v\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">evenstr</span><span style="color: #0000FF;">,</span><span style="color: #000000;">j</span><span style="color: #0000FF;">,</span><span style="color: #000000;">k</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lucky</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">..</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]})</span>
<span style="color: #008080;">else</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">></span><span style="color: #000000;">lucky</span><span style="color: #0000FF;">[$]</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">fatal</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"start of range is too big"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">></span><span style="color: #000000;">lucky</span><span style="color: #0000FF;">[$]</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">fatal</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"end of range is too big"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">m</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">abs</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">binary_search</span><span style="color: #0000FF;">(</span><span style="color: #000000;">j</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lucky</span><span style="color: #0000FF;">)),</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">binary_search</span><span style="color: #0000FF;">(</span><span style="color: #000000;">k</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lucky</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;"><</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Lucky %snumbers between %d and %d are: %v\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">evenstr</span><span style="color: #0000FF;">,</span><span style="color: #000000;">j</span><span style="color: #0000FF;">,</span><span style="color: #000000;">k</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lucky</span><span style="color: #0000FF;">[</span><span style="color: #000000;">m</span><span style="color: #0000FF;">..</span><span style="color: #000000;">n</span><span style="color: #0000FF;">]})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">cl</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">command_line</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cl</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">2</span> <span style="color: #008080;">then</span>
<span style="color: #000080;font-style:italic;">-- fatal("at least one argument must be supplied") -- (if preferred)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">tests</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"1 20"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"1 20 evenLucky"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"20 lucky"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"20 evenLucky"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"6000 -6100"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"6000 -6100 evenLucky"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"10000 lucky"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"10000 evenLucky"</span><span style="color: #0000FF;">}</span>
<span style="color: #000080;font-style:italic;">-- (done this way to exercise the real command line handling...)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">cl</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">cl</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span> <span style="color: #000080;font-style:italic;">-- (compiled)</span>
<span style="color: #000000;">cl</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">cl</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">elsif</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()=</span><span style="color: #004600;">WINDOWS</span> <span style="color: #008080;">then</span> <span style="color: #000080;font-style:italic;">-- (and interpreted)</span>
<span style="color: #000000;">cl</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">substitute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cl</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span><span style="color: #008000;">"pw"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"p"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (pw.exe -&gt; p.exe)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cl</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">,</span><span style="color: #000000;">cl</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span> <span style="color: #008080;">then</span> <span style="color: #000000;">cl</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"\"%s\""</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">cl</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]})</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
<span style="color: #000080;font-style:italic;">-- (...except when we can't do that, of course)</span>
<span style="color: #000000;">process</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">t</span><span style="color: #0000FF;">]))</span>
<span style="color: #008080;">else</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">cmd</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cl</span><span style="color: #0000FF;">),</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">t</span><span style="color: #0000FF;">]))</span>
<span style="color: #000080;font-style:italic;">-- printf(1,"running %s\n",{cmd})</span>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">system_exec</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cmd</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"tests complete\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">else</span>
<span style="color: #000000;">cl</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">cl</span><span style="color: #0000FF;">[</span><span style="color: #000000;">3</span><span style="color: #0000FF;">..$]</span> <span style="color: #000080;font-style:italic;">-- ({1,2} are {interperter,source} or {exe,exe})</span>
<span style="color: #000000;">process</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cl</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
{{out}}
You may need to run this from a [windows] console to get them all on the same screen, and use p.exe rather than pw.exe
<pre>
Output when args are 1 20
Lucky numbers 1 to 20 are: {1,3,7,9,13,15,21,25,31,33,37,43,49,51,63,67,69,73,75,79}
Output when args are 1 20 evenLucky
Lucky even numbers 1 to 20 are: {2,4,6,10,12,18,20,22,26,34,36,42,44,50,52,54,58,68,70,76}
Output when args are 20 lucky
Lucky number 20 = 79
Output when args are 20 evenLucky
Lucky even number 20 = 76
Output when args are 6000 -6100
Lucky numbers between 6000 and 6100 are: {6009,6019,6031,6049,6055,6061,6079,6093}
Output when args are 6000 -6100 evenLucky
Lucky even numbers between 6000 and 6100 are: {6018,6020,6022,6026,6036,6038,6050,6058,6074,6090,6092}
Output when args are 10000 lucky
Lucky number 10000 = 115591
Output when args are 10000 evenLucky
Lucky even number 10000 = 111842
tests complete
</pre>
 
=={{header|PicoLisp}}==
<syntaxhighlight lang="picolisp">(off *Even)
 
(de nn (Lst N)
(seek
'((L)
(when (car L) (=0 (dec 'N))) )
Lst ) )
 
(de lucky (B)
(let Lst (range (if *Even 2 1) B 2)
(for A (cdr Lst)
(for (L (nn Lst A) L (nn (cdr L) A))
(set L) ) )
(filter bool Lst) ) )
 
(argv . *Argv) # without validations
(when (= "evenLucky" (last *Argv)) (on *Even))
(setq *Lst (lucky 7000))
(let (A (format (car *Argv)) B (format (cadr *Argv)))
(println
(if (lt0 B)
(filter '((N) (<= A N (abs B))) *Lst)
(head B (nth *Lst A)) ) ) )</syntaxhighlight>
{{out}}
<pre>$ pil ./lucky.l 1 20
(1 3 7 9 13 15 21 25 31 33 37 43 49 51 63 67 69 73 75 79)
$ pil ./lucky.l 1 20 evenLucky
(2 4 6 10 12 18 20 22 26 34 36 42 44 50 52 54 58 68 70 76)
$ pil ./lucky.l 6000 -6100
(6009 6019 6031 6049 6055 6061 6079 6093)
$ pil ./lucky.l 6000 -6100 evenLucky
(6018 6020 6022 6026 6036 6038 6050 6058 6074 6090 6092)</pre>
 
=={{header|Python}}==
The generator
<langsyntaxhighlight lang="python">from __future__ import print_function
 
def lgen(even=False, nmax=1000000):
Line 319 ⟶ 2,516:
# drain
for i in lst[n:]:
yield i</langsyntaxhighlight>
 
The argument handler
<langsyntaxhighlight lang="python">from itertools import islice
import sys, re
 
Line 421 ⟶ 2,618:
 
if __name__ == '__main__':
arghandler(' '.join(sys.argv[1:]))</langsyntaxhighlight>
 
{{out}}
Line 436 ⟶ 2,633:
# Output when arguments are: 10000 , evenLucky
Single 10000'th even lucky number: 111842</pre>
===Python: streaming lazy generator===
The following streaming version of function lgen returns odd or even lucky numbers until reaching system limits. Instead of creating a bounded table and deleting elements, it uses the insight that after each iteration the <b>remaining</b> numbers are shuffled left, modifying their indices in a regular way. Reversing this process tracks the k'th lucky number in the final list back to its position in the initial list of integers, and hence determines its value without any need to build the table. The only storage requirement is for the list of numbers found so far.
<br>Based on the algorithm at https://oeis.org/A000959/a000959.txt.
<syntaxhighlight lang="python">from itertools import count
def lgen(even=False):
lucky = []
if not even:
yield 1
for k in count(1):
for l in reversed(lucky):
k = (k*l)//(l-1)
lucky.append(2*k+1 + even)
yield 2*k+1 + even
</syntaxhighlight>
 
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" line>sub luck(\a,\b) {
gather {
my @taken = take a;
my @rotor;
my $i = b;
 
loop {
loop (my $j = 0; $j < @rotor; $j++) {
--@rotor[$j] or last;
}
if $j < @rotor {
@rotor[$j] = @taken[$j+1];
}
else {
push @taken, take $i;
push @rotor, $i - @taken;
}
$i += 2;
}
}
}
 
constant @lucky = luck(1,3);
constant @evenlucky = luck(2,4);
 
subset Luck where m:i/^ 'even'? 'lucky' $/;
 
multi MAIN (Int $num where * > 0) {
say @lucky[$num-1];
}
 
multi MAIN (Int $num where * > 0, ',', Luck $howlucky = 'lucky') {
say @::(lc $howlucky)[$num-1];
}
 
multi MAIN (Int $first where * > 0, Int $last where * > 0, Luck $howlucky = 'lucky') {
say @::(lc $howlucky)[$first-1 .. $last - 1];
}
 
multi MAIN (Int $min where * > 0, Int $neg-max where * < 0, Luck $howlucky = 'lucky') {
say grep * >= $min, (@::(lc $howlucky) ...^ * > abs $neg-max);
}</syntaxhighlight>
{{out}}
<pre>$ ./lucky
Usage:
./lucky <num>
./lucky <num> , [<howlucky>]
./lucky <first> <last> [<howlucky>]
./lucky <min> <neg-max> [<howlucky>]
$ ./lucky 20 , lucky
79
$ ./lucky 20 , evenlucky
76
$ ./lucky 1 20
1 3 7 9 13 15 21 25 31 33 37 43 49 51 63 67 69 73 75 79
$ ./lucky 1 20 evenlucky
2 4 6 10 12 18 20 22 26 34 36 42 44 50 52 54 58 68 70 76
$ ./lucky 6000 -6100
6009 6019 6031 6049 6055 6061 6079 6093
$ ./lucky 6000 -6100 evenLucky
6018 6020 6022 6026 6036 6038 6050 6058 6074 6090 6092
$ ./lucky 10000
115591
$ ./lucky 10000 , EVENLUCKY
111842</pre>
 
=={{header|REXX}}==
This REXX version does extra error checking for the arguments.
<langsyntaxhighlight REXXlang="rexx">/*REXX program displays lucky or evenLucky integers (numbers or a number range).*/
parse arg bot top func _ . /*obtain required & optional arguments.*/
if func=='' then func= 'lucky' /*Not specified? Then use the default.*/
s= left('s', bot\==top & top\==",") /*plural results (or maybe not plural).*/
say func 'number's":" bot top '───►' $lucky(bot, top, func, _)
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
$lucky: arg x,y,f,?; if y=='' | y=="," then y= x /*obtain some arguments; set Y.*/
#= 0; $=; ny=y<0 ny= y<0 /*set variable NOY: value range*/
if f=='' then f= 'LUCKY'; lucky= (f=="LUCKY") /*assume LUCKY if omitted. */
if f\=='LUCKY' & f\=='EVENLUCKY' then return 'function not valid: ' f
if arg()>3 & ?\='' then return "too many arguments entered: " ?
Line 456 ⟶ 2,735:
if \datatype(y,'W') then return "2nd argument isn't an integer: " y
if x>ay then return "2nd argument is less than 1st arg."
ay=abs(y); yL=ay; if y>0 then yL= y*10+y+y + y + y /*adjust the upper Y limit. */
/* [↓] build LUCKY | EVENLUCKY*/
do j=1 until j>=yL /*construct list pos. integers.*/
Line 464 ⟶ 2,743:
else nop /*balance the IF-THEN logic.*/
else if j //8==0 then iterate /*prune next if mod 8 ≡ zero.*/
#= # +1 1 /*bump the counter of numbers. */
$= $ j /*append integer to the $ list.*/
end /*j*/
q=0
do p=3 until q==''; q= word($, p) /*start to prune integer list.*/
if q># then leave /*if integer is too large, stop*/
do j=#%q*q by -q to q /*elide every Qth integer. */
$= delword($, j, 1) /*delete a particular number. */
#= # -1 /*decrease the integer count. */
end /*j*/ /*delete from the right side. */
end /*p*/
@.=
do k=1; parse var $ q $; if q=='' then leave; @.k=q; end /* @.k*/= q
end /*k*/
@.0=k-1
do m=1 for # /*restrict the found integers. */
Line 482 ⟶ 2,762:
end /*m*/ /* [↑] a list of #s or a range*/
_=
do b=1 for @.0; _= _ @.b; end /*b*/ /*construct a list of integers.*/
return space(_) end /*remove superfluous blanks. b*/</lang>
return space(_) /*remove superfluous blanks. */</syntaxhighlight>
'''output''' &nbsp; when the input is: &nbsp; <tt> 1 &nbsp; 20 &nbsp; lucky </tt>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 1 &nbsp; 20 &nbsp; lucky </tt>}}
<pre>
lucky numbers: 1 20 ───► 1 3 7 9 13 15 21 25 31 33 37 43 49 51 63 67 69 73 75 79
</pre>
 
'''output''' &nbsp; when the input is: &nbsp; <tt> 1 &nbsp; 20 &nbsp; evenLucky </tt>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 1 &nbsp; 20 &nbsp; evenlucky </tt>}}
<pre>
evenLucky numbers: 1 20 ───► 2 4 6 10 12 18 20 22 26 34 36 42 44 50 52 54 58 68 70 76
</pre>
 
'''output''' &nbsp; when the input is: &nbsp; <tt> 6000 &nbsp; -6100 &nbsp; lucky</tt>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 6000 &nbsp; -6100 &nbsp; lucky </tt>}}
<pre>
lucky numbers: 6000 -6100 ───► 6009 6019 6031 6049 6055 6061 6079 6093
</pre>
 
'''output''' &nbsp; when the input is: &nbsp; <tt> 6000 &nbsp; -6100 &nbsp; venLucky</tt>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 6000 &nbsp; -6100 &nbsp; evenLucky </tt>}}
<pre>
evenLucky numbers: 6000 -6100 ───► 6018 6020 6022 6026 6036 6038 6050 6058 6074 6090 6092
</pre>
 
'''output''' &nbsp; when the input is: &nbsp; <tt> 10000 </tt>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 10000 </tt>}}
<pre>
lucky number: 10000 ───► 115591
</pre>
 
'''output''' &nbsp; when the input is: &nbsp; <tt> 10000 &nbsp; , &nbsp; evenLucky </tt>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 10000 &nbsp; , &nbsp; evenLucky </tt>}}
<pre>
evenLucky number: 10000 ───► 111842
</pre>
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
# Project : Lucky and even lucky numbers
 
lucky = list(50)
dellucky = []
for n = 1 to 50
lucky[n] = 2*n-1
next
see "the first 20 lucky numbers:" + nl
luckynumbers(lucky)
showarray(lucky)
see nl
 
lucky = list(50)
dellucky = []
for n = 1 to 50
lucky[n] = 2*n
next
see "the first 20 even lucky numbers:" + nl
luckynumbers(lucky)
showarray(lucky)
see nl
 
lucky = list(20000)
dellucky = []
for n = 1 to 10000
lucky[n] = 2*n-1
next
see "lucky numbers between 6,000 and 6,100:" + nl
luckynumbers2(lucky)
showarray2(lucky)
see nl
 
lucky = list(20000)
dellucky = []
for n = 1 to 10000
lucky[n] = 2*n
next
see "even lucky numbers between 6,000 and 6,100:" + nl
luckynumbers2(lucky)
showarray2(lucky)
see nl
 
func luckynumbers(lucky)
for n = 2 to len(lucky)
dellucky = []
for m = lucky[n] to len(lucky) step lucky[n]
add(dellucky, m)
next
for p = len(dellucky) to 1 step -1
del(lucky, dellucky[p])
next
next
 
func luckynumbers2(lucky)
for n = 2 to len(lucky)
dellucky = []
for m = lucky[n] to len(lucky) step lucky[n]
add(dellucky, m)
next
for p = len(dellucky) to 1 step -1
del(lucky, dellucky[p])
next
if lucky[n] >= 6100
exit
ok
next
 
func showarray(vect)
see "["
svect = ""
for n = 1 to 20
svect = svect + vect[n] + ", "
next
svect = left(svect, len(svect) - 2)
see svect
see "]" + nl
 
func showarray2(vect)
see "["
svect = ""
for n = 1 to len(vect)
if vect[n] >= 6000 and vect[n] <= 6100
svect = svect + vect[n] + ", "
ok
next
svect = left(svect, len(svect) - 2)
see svect
see "]" + nl
</syntaxhighlight>
Output:
<pre>
the first 20 lucky numbers:
[1, 3, 7, 9, 13, 15, 21, 25, 31, 33, 37, 43, 49, 51, 63, 67, 69, 73, 75, 79]
 
the first 20 even lucky numbers:
[2, 4, 6, 10, 12, 18, 20, 22, 26, 34, 36, 42, 44, 50, 52, 54, 58, 68, 70, 76]
 
lucky numbers between 6,000 and 6,100:
[6009, 6019, 6031, 6049, 6055, 6061, 6079, 6093]
 
even lucky numbers between 6,000 and 6,100:
[6018, 6020, 6022, 6026, 6036, 6038, 6050, 6058, 6074, 6090, 6092]
</pre>
 
=={{header|RPL}}==
{{works with|HP|48}}
The program returns even lucky numbers when one of the 2 arguments is negative, which is more idiomatic.
{| class="wikitable" ≪
! RPL code
! Comment
|-
|
1 CF DUP2 * 0 < ≪ 1 SF ≫ IFT
ABS SWAP ABS DUP2 ≤ ≪ #202h DOERR ≫ IFT
2 → b a n
≪ { } 1 1 FS? + b '''FOR''' j j + 2 '''STEP'''
'''DO'''
DUP n GET → m
≪ { } 1 3 PICK SIZE '''FOR''' j
OVER j DUP m + 2 - SUB + m '''STEP'''
≫ SWAP DROP 'n' INCR
'''UNTIL''' OVER SIZE > '''END'''
{ } 1 3 PICK SIZE '''FOR''' j
'''IF''' OVER j GET DUP a < '''THEN''' DROP '''ELSE''' + '''END'''
'''NEXT''' SWAP DROP
≫ ≫ '<span style="color:blue">LUCKY</span>' STO
|
<span style="color:blue">LUCKY</span> ''( a (-)b → { (even)lucky } )''
Set flag 1 if a xor b is negative
abort if b ≤ a with an error message
n = 2
generate list of odd/even numbers according to flag 1
loop
m = nth number of the list
for j = 1 to size(list) step m
keep numbers from j to j+m-2
clean stack ; n++
until n > size(list)
for j = 1 to size(list)
keep jth number only if ≥ a
clean stack
return list
|}
1 80 <span style="color:blue">LUCKY</span>
1 -80 <span style="color:blue">LUCKY</span>
6000 6100 <span style="color:blue">LUCKY</span>
6000 -6100 <span style="color:blue">LUCKY</span>
{{out}}
<pre>
4: { 1 3 7 9 13 15 21 25 31 33 37 43 49 51 63 67 69 73 75 79 }
3: { 2 4 6 10 12 18 20 22 26 34 36 42 44 50 52 54 58 68 70 76 }
2: { 6009 6019 6031 6049 6055 6061 6079 6093 }
1: { 6018 6020 6022 6026 6036 6038 6050 6058 6074 6090 6092 }
</pre>
A 6000-number sieve requiring more than 30 KB RAM, the last 2 demonstrations have been run on an emulator instead of a genuine calculator.
 
 
=={{header|Ruby}}==
{{trans|Python}}
<langsyntaxhighlight lang="ruby">def generator(even=false, nmax=1000000)
start = even ? 2 : 1
Enumerator.new do |y|
Line 555 ⟶ 3,001:
if __FILE__ == $0
lucky(ARGV)
end</langsyntaxhighlight>
 
{{out}}
Line 581 ⟶ 3,027:
10000th even lucky number: 111842
</pre>
 
=={{header|Swift}}==
{{trans|Python}}
 
The lucky numbers sequence:
 
<syntaxhighlight lang="swift">struct LuckyNumbers : Sequence, IteratorProtocol {
let even: Bool
let through: Int
private var drainI = 0
private var n = 0
private var lst: [Int]
init(even: Bool = false, through: Int = 1_000_000) {
self.even = even
self.through = through
self.lst = Array(stride(from: even ? 2 : 1, through: through, by: 2))
}
mutating func next() -> Int? {
guard n != 0 else {
defer { n += 1 }
return lst[0]
}
while n < lst.count && lst[n] < lst.count {
let retVal = lst[n]
lst = lst.enumerated().filter({ ($0.offset + 1) % lst[n] != 0 }).map({ $0.element })
n += 1
return retVal
}
if drainI == 0 {
lst = Array(lst.dropFirst(n))
}
 
while drainI < lst.count {
defer { drainI += 1 }
return lst[drainI]
}
return nil
}
}</syntaxhighlight>
 
The main file:
<syntaxhighlight lang="swift">let args = Array(CommandLine.arguments.dropFirst())
 
guard let sj = args.first, let j = Int(sj), j > 0, j <= 10_000 else {
fatalError("Incorrect j")
}
 
func evenString(_ even: Bool) -> String {
return even ? "even" : ""
}
 
func jLuckyNumber(_ j: Int, even: Bool) {
print("The \(j)th \(evenString(even)) lucky number is \(Array(LuckyNumbers(even: even))[j-1..<j].first!)")
}
 
func luckyNumbersKth(j: Int, k: Int, even: Bool) {
print("List of \(j) ... \(k) \(evenString(even)) lucky numbers: ", terminator: "")
for (offset, luck) in LuckyNumbers(even: even).lazy.enumerated() {
guard offset + 1 <= k else { break }
if offset + 1 >= j {
print(luck, terminator: ", ")
}
}
print()
}
 
func luckyNumbersRange(j: Int, k: Int, even: Bool) {
print("List of \(evenString(even)) lucky numbers in the range \(j) ... \(-k): ", terminator: "")
for lucky in LuckyNumbers(even: even).lazy {
guard lucky <= -k else { break }
if lucky >= j {
print(lucky, terminator: ", ")
}
}
print()
}
 
switch args.count {
case 1:
jLuckyNumber(j, even: false)
case 2:
switch Int(args.last!) {
case let k? where k > 0 && k <= 10_000 && k > j:
luckyNumbersKth(j: j, k: k, even: false)
case let k? where k < 0 && -k > j:
luckyNumbersRange(j: j, k: k, even: false)
case _:
fatalError("Bad args")
}
case 3:
switch (Int(args[1]), args.last!) {
case (nil, "lucky"):
jLuckyNumber(j, even: false)
case (nil, "evenLucky"):
jLuckyNumber(j, even: true)
case let (k?, "lucky") where k > 0 && k <= 10_000 && k > j:
luckyNumbersKth(j: j, k: k, even: false)
case let (k?, "evenLucky") where k > 0 && k <= 10_000 && k > j:
luckyNumbersKth(j: j, k: k, even: true)
case let (k?, "lucky") where k < 0 && -k > j:
luckyNumbersRange(j: j, k: k, even: false)
case let (k?, "evenLucky") where k < 0 && -k > j:
luckyNumbersRange(j: j, k: k, even: true)
case _:
fatalError("Bad args")
}
case _:
fatalError()
}</syntaxhighlight>
 
{{out}}
<pre>$ ./main 1 20 lucky
List of 1 ... 20 lucky numbers: 1, 3, 7, 9, 13, 15, 21, 25, 31, 33, 37, 43, 49, 51, 63, 67, 69, 73, 75, 79,
$ ./main 1 20 evenLucky
List of 1 ... 20 even lucky numbers: 2, 4, 6, 10, 12, 18, 20, 22, 26, 34, 36, 42, 44, 50, 52, 54, 58, 68, 70, 76,
$ ./main 6000 -6100 lucky
List of lucky numbers in the range 6000 ... 6100: 6009, 6019, 6031, 6049, 6055, 6061, 6079, 6093,
$ ./main 6000 -6100 evenLucky
List of even lucky numbers in the range 6000 ... 6100: 6018, 6020, 6022, 6026, 6036, 6038, 6050, 6058, 6074, 6090, 6092,
$ ./main 10000
The 10000th lucky number is 115591
$ ./main 10000 , evenLucky
The 10000th even lucky number is 111842</pre>
 
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
{{trans|Python}}
<langsyntaxhighlight lang="tcl">#!/usr/bin/env tclsh8.6
package require Tcl 8.6
 
Line 653 ⟶ 3,238:
} else {
puts "$from'th to $to'th $evenOdd numbers: [join [collectIndices $l [incr from -1] [incr to -1]] ,]"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 668 ⟶ 3,253:
bash$ lucky.tcl 10000 , evenLucky
10000'th even lucky number = 111842
</pre>
 
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|Wren-iterate}}
{{libheader|Wren-str}}
<syntaxhighlight lang="wren">import "os" for Process
import "./iterate" for Stepped
import "./str" for Str
 
var luckyOdd = List.filled(1e5, 0)
var luckyEven = List.filled(1e5, 0)
 
var init = Fn.new {
for (i in 0...1e5) {
luckyOdd[i] = i*2 + 1
luckyEven[i] = i*2 + 2
}
}
 
var filterLuckyOdd = Fn.new {
var n = 2
while (n < luckyOdd.count) {
var m = luckyOdd[n-1]
var end = (luckyOdd.count/m).floor * m - 1
for (j in Stepped.descend(end..m-1, m)) luckyOdd.removeAt(j)
n = n + 1
}
}
 
var filterLuckyEven = Fn.new {
var n = 2
while (n < luckyEven.count) {
var m = luckyEven[n-1]
var end = (luckyEven.count/m).floor * m - 1
for (j in Stepped.descend(end..m-1, m)) luckyEven.removeAt(j)
n = n + 1
}
}
 
var printSingle = Fn.new { |j, odd|
if (odd) {
if (j >= luckyOdd.count) Fiber.abort("Argument is too big")
System.print("Lucky number %(j) = %(luckyOdd[j-1])")
} else {
if (j >= luckyEven.count) Fiber.abort("Argument is too big")
System.print("Lucky even number %(j) = %(luckyEven[j-1])")
}
}
 
var printRange = Fn.new { |j, k, odd|
if (odd) {
if (k >= luckyOdd.count) Fiber.abort("Argument is too big")
var rng = luckyOdd.skip(j-1).take(k-j+1).toList
System.print("Lucky numbers %(j) to %(k) are:\n %(rng)")
} else {
if (k >= luckyEven.count) Fiber.abort("Argument is too big")
var rng = luckyEven.skip(j-1).take(k-j+1).toList
System.print("Lucky even numbers %(j) to %(k) are:\n %(rng)")
}
}
 
var printBetween = Fn.new { |j, k, odd|
var rng = []
if (odd) {
var max = luckyOdd[-1]
if (j > max || k > max) {
Fiber.abort("At least one argument is too big")
}
for (num in luckyOdd) {
if (num >= j) {
if (num > k) break
rng.add(num)
}
}
System.print("Lucky numbers between %(j) and %(k) are:\n%(rng)")
} else {
var max = luckyEven[-1]
if (j > max || k > max) {
Fiber.abort("At least one argument is too big")
}
for (num in luckyEven) {
if (num >= j) {
if (num > k) break
rng.add(num)
}
}
System.print("Lucky even numbers between %(j) and %(k) are:\n%(rng)")
}
}
 
var args = Process.arguments
var argsSize = args.count
if (argsSize < 1 || argsSize > 3) Fiber.abort("There must be between 1 and 3 command line arguments")
init.call()
filterLuckyOdd.call()
filterLuckyEven.call()
var j = Num.fromString(args[0])
if (j.type != Num || !j.isInteger || j < 1) Fiber.abort("First argument must be a positive integer")
if (argsSize == 1) {
printSingle.call(j, true)
return
}
 
if (argsSize == 2) {
var k = Num.fromString(args[1])
if (k.type != Num || !k.isInteger) Fiber.abort("Second argument must be an integer")
if (k >= 0) {
if (j > k) Fiber.abort("Second argument can't be less than first")
printRange.call(j, k, true)
} else {
var l = -k
if (j > l) Fiber.abort("The second argument can't be less in absolute value than first")
printBetween.call(j, l, true)
}
return
}
 
args[2] = Str.lower(args[2])
var odd = (args[2] == "lucky") ? true :
(args[2] == "evenlucky") ? false : Fiber.abort("Third argument is invalid")
 
if (args[1] == ",") {
printSingle.call(j, odd)
return
}
 
var k = Num.fromString(args[1])
if (!((k.type == Num && k.isInteger) || (k.type == String && k == ","))) {
Fiber.abort("Second argument must be an integer or a comma")
}
if (k >= 0) {
if (j > k) Fiber.abort("Second argument can't be less than first")
printRange.call(j, k, odd)
} else {
var l = -k
if (j > l) Fiber.abort("The second argument can't be less in absolute value than first")
printBetween.call(j, l, odd)
}</syntaxhighlight>
 
{{out}}
<pre>
$ wren Lucky_and_even_lucky_numbers.wren 1 20
Lucky numbers 1 to 20 are:
[1, 3, 7, 9, 13, 15, 21, 25, 31, 33, 37, 43, 49, 51, 63, 67, 69, 73, 75, 79]
 
$ wren Lucky_and_even_lucky_numbers.wren 1 20 evenLucky
Lucky even numbers 1 to 20 are:
[2, 4, 6, 10, 12, 18, 20, 22, 26, 34, 36, 42, 44, 50, 52, 54, 58, 68, 70, 76]
 
$ wren Lucky_and_even_lucky_numbers.wren 6000 -6100
Lucky numbers between 6000 and 6100 are:
[6009, 6019, 6031, 6049, 6055, 6061, 6079, 6093]
 
$ wren Lucky_and_even_lucky_numbers.wren 6000 -6100 evenLucky
Lucky even numbers between 6000 and 6100 are:
[6018, 6020, 6022, 6026, 6036, 6038, 6050, 6058, 6074, 6090, 6092]
 
$ wren Lucky_and_even_lucky_numbers.wren 10000 , lucky
Lucky number 10000 = 115591
 
$ wren Lucky_and_even_lucky_numbers.wren 10000 , evenLucky
Lucky even number 10000 = 111842
</pre>
 
=={{header|zkl}}==
The lucky number generator works by chaining filters to a even or odd infinite sequence. So it acts like a sieve as each starting number percolates through the filters. It also means there are lots and lots of filters, which doesn't scale well but works for the examples.
<langsyntaxhighlight lang="zkl">fcn lgen(a){
ns,idx:=[a..*,2],2;
vm.yield(ns.next());
Line 681 ⟶ 3,429:
}
}
fcn skipper(n,skp,cnt){ z:=cnt.inc(); if(z%skp==0) Void.Skip else n }</langsyntaxhighlight>
The command line is a bit funky (by Unix standards) so we just hard code it and use exceptions (such as trying to convert "foo" to int) to show the options.
<langsyntaxhighlight lang="zkl">cmdLineArgs,j,k,start:=vm.arglist,Void,Void,1;
try{
j=cmdLineArgs[0].toInt();
Line 714 ⟶ 3,462:
}
}
}catch(TheEnd){ options() }</langsyntaxhighlight>
{{out}}
<pre>
1,150

edits