Lucky and even lucky numbers: Difference between revisions

added RPL
(added RPL)
 
(7 intermediate revisions by 4 users not shown)
Line 92:
{{trans|Nim}}
 
<langsyntaxhighlight lang="11l">-V NoValue = 0
 
F initLuckyNumbers(nelems, evenlucky, limit = -1)
Line 176:
print()
E
process_args(:argv[1..])</langsyntaxhighlight>
 
{{out}}
Line 205:
=={{header|C}}==
{{trans|C++}}
<langsyntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
Line 431:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>LuckyNumbers.exe -j=1 -k=20
Line 453:
=={{header|C++}}==
{{trans|Go}}
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <iostream>
#include <iterator>
Line 629:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>&gt;LuckyNumbers.exe -j=1 -k=20
Line 650:
 
=={{header|D}}==
<langsyntaxhighlight Dlang="d">import std.algorithm;
import std.concurrency;
import std.conv;
Line 735:
lucky(evenLucky).drop(j-1).take(1).writeln;
}
}</langsyntaxhighlight>
 
{{out}}
Line 758:
=={{header|F_Sharp|F#}}==
===The functions===
<langsyntaxhighlight 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>
</lang>
 
===The task===
Line 783:
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 975:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,005:
 
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 Text.Regex.Posix
Line 1,085:
_ -> do
putStrLn "Invalid input, wrong number of arguments"
putStrLn "Type --help"</langsyntaxhighlight>
{{out}}
<pre>$ luckyNumbers 1 20
Line 1,106:
Implementation:
 
<langsyntaxhighlight Jlang="j">luckySeq=:3 :0
1 luckySeq y
:
Line 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 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|Java}}==
<syntaxhighlight lang="java">
<lang Java>
import java.util.ArrayList;
import java.util.Collections;
Line 1,222:
 
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,247:
=={{header|JavaScript}}==
First: the function.
<syntaxhighlight lang="javascript">
<lang javaScript>
function luckyNumbers(opts={}) {
/**************************************************************************\
Line 1,303:
// showing the 10,000th even lucky number (extra credit)
console.log( luckyNumbers({even: true, nth: 10000}) );
</syntaxhighlight>
</lang>
{{out}}<pre>
> Array(276) [ 1, 3, 7, 9, 13, 15, 21, 25, 31, 33, … ]
Line 1,320:
We'll use an HTML-prompt here, because JavaScript actually doesn't have a command interface on it's own.
 
<langsyntaxhighlight lang="javascript">
(function() {
document.write(`
Line 1,409:
}
})();
</syntaxhighlight>
</lang>
Using the task demonstrations again, the I/O part looks like:
{{out}}<pre>
Line 1,429:
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.
<langsyntaxhighlight lang="julia">using Base, StringDistances
 
struct Lucky
Line 1,586 ⟶ 1,772:
end
 
runopts()</langsyntaxhighlight> {{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]
Line 1,602 ⟶ 1,788:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.51
 
typealias IAE = IllegalArgumentException
Line 1,724 ⟶ 1,910:
printBetween(j, l, odd)
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,753 ⟶ 1,939:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Save the following code in the file script.wls and execute the wolframscript from the command line:
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[GetLuckies, GetEvenLuckies]
GetLuckies[max_] := Module[{luckies, f, i},
luckies = Range[1, max, 2];
Line 1,867 ⟶ 2,053:
]
]
GiveLucky[Last@$ScriptCommandLine]</langsyntaxhighlight>
{{out}}
<pre>wolframscript -file script.wls -args "1 20 lucky"
Line 1,884 ⟶ 2,070:
=={{header|Nim}}==
For the generation of lucky numbers, we use the second Python algorithm, modified to return a sequence.
<langsyntaxhighlight Nimlang="nim">import os, strformat, strutils
 
type LuckyKind {.pure.} = enum Lucky = "lucky", EvenLucky = "evenlucky"
Line 2,006 ⟶ 2,192:
else:
# Print values in range j..(-k).
printInRange(j, -k, kind)</langsyntaxhighlight>
 
{{out}}
Line 2,030 ⟶ 2,216:
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}}
<langsyntaxhighlight lang="perl">use Perl6::GatherTake;
 
sub luck {
Line 2,080 ⟶ 2,266:
}
 
print "\n"</langsyntaxhighlight>
{{out}}
<pre>$ ./lucky
Line 2,102 ⟶ 2,288:
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>constant luckyMax = 120000
<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>
sequence lucky
<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>
procedure filterLucky()
<span style="color: #000000;">filterLucky</span><span style="color: #0000FF;">()</span>
integer n = 2
<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>
while n<=length(lucky) do
<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>
integer m = lucky[n],
<span style="color: #008080;">if</span> <span style="color: #000000;">single</span> <span style="color: #008080;">then</span>
l = m-1
<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>
for k=m+1 to length(lucky) do
<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>
if mod(k,m)!=0 then
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
l += 1
<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>
lucky[l] = lucky[k]
<span style="color: #008080;">elsif</span> <span style="color: #000000;">range</span> <span style="color: #008080;">then</span>
end if
<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>
end for
<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>
if l>=length(lucky) then exit end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
lucky = lucky[1..l]
<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>
n += 1
<span style="color: #008080;">else</span>
end while
<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>
end procedure
<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>
constant helptxt = """
<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>
argument(s) | what is displayed
<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>
j | jth lucky number
<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>
j [,] lucky | jth lucky number
<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>
j [,] evenLucky | jth even lucky number
<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>
j k | jth through kth (inclusive) lucky numbers
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
j k lucky | jth through kth (inclusive) lucky numbers
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
j k evenLucky | jth through kth (inclusive) even lucky numbers
j -k | all lucky numbers in the range j to k
<span style="color: #008080;">procedure</span> <span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
j -k lucky | all lucky numbers in the range j to k
<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>
j -k evenLucky | all even lucky numbers in the range j to k
<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>
procedure fatal(string msg)
<span style="color: #008000;">"1 20 evenLucky"</span><span style="color: #0000FF;">,</span>
puts(1,msg)
<span style="color: #008000;">"20 lucky"</span><span style="color: #0000FF;">,</span>
puts(1,helptxt)
<span style="color: #008000;">"20 evenLucky"</span><span style="color: #0000FF;">,</span>
{} = wait_key()
<span style="color: #008000;">"6000 -6100"</span><span style="color: #0000FF;">,</span>
abort(0)
<span style="color: #008000;">"6000 -6100 evenLucky"</span><span style="color: #0000FF;">,</span>
end procedure
<span style="color: #008000;">"10000 lucky"</span><span style="color: #0000FF;">,</span>
 
<span style="color: #008000;">"10000 evenLucky"</span><span style="color: #0000FF;">}</span>
procedure main()
<span style="color: #000080;font-style:italic;">-- (done this way to exercise the real command line handling...)</span>
sequence cl = command_line()
<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>
integer j,k,l,m,n
<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>
bool single = true, range = true, odd = true
<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>
if length(cl)=2 then
<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>
-- fatal("at least one argument must be supplied") -- (if preferred)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
sequence tests = {"1 20",
<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>
"1 20 evenLucky",
<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>
"20 lucky",
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
"20 evenLucky",
<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>
"6000 -6100",
<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>
"6000 -6100 evenLucky",
<span style="color: #000080;font-style:italic;">-- (...except when we can't do that, "10000of lucky",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>
"10000 evenLucky"}
<span style="color: #008080;">else</span>
-- (done this way to exercise the real command line handling)
<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>
if cl[1]=cl[2] then -- (compiled)
<span style="color: #000080;font-style:italic;">-- printf(1,"running %s\n",{cmd})</span>
cl = cl[1..1]
<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>
elsif platform()=WINDOWS then -- (and interpreted)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
cl[1] = substitute(cl[1],"pw","p") -- (pw.exe -> p.exe)
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end if
<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>
for i=1 to length(cl) do
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
if find(' ',cl[i]) then cl[i] = sprintf("\"%s\"",{cl[i]}) end if
<span style="color: #008080;">else</span>
end for
<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>
for t=1 to length(tests) do
<span style="color: #000000;">process</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cl</span><span style="color: #0000FF;">)</span>
string cmd = join(append(cl,tests[t]))
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
-- printf(1,"running %s\n",{cmd})
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
{} = system_exec(cmd)
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
end for
<!--</syntaxhighlight>-->
puts(1, "tests complete\n")
{} = wait_key()
else
cl = cl[3..$] -- ({1,2} are {interperter,source} or {exe,exe})
 
--
-- Allow eg "lucky j , evenLucky" to be == "lucky j evenLucky"
--
if length(cl)=3 and cl[2]="," then cl[2..2] = {} end if
 
for i=1 to length(cl) do
string cli = cl[i]
if cli[1]<='9' then -- (includes '-')
sequence d = scanf(cl[i],"%d")
if length(d)!=1 then
fatal("unrecognised "&cli)
end if
if i>2 then
fatal("too many numbers")
end if
n = d[1][1]
if i=1 then
if n<1 then
fatal("first argument must be a positive integer")
end if
j = n
else
single = false
if n<0 then
range = false
n = -n
end if
if n<j then
fatal("second argument cannot be less than first")
end if
k = n
end if
else
l = find(cli,{"lucky","evenLucky"})
if l=0 then
fatal("unrecognised "&cli)
end if
if i!=length(cl) then
fatal(cli&" must be last parameter")
end if
odd = (l=1)
end if
end for
lucky = tagset(luckyMax,2-odd,2)
filterLucky()
printf(1,"Output when args are %s\n",{join(cl)})
string even = iff(odd?"":"even ")
if single then
if j>length(lucky) then
fatal(sprintf("the argument, %d, is too big", j))
end if
printf(1,"Lucky %snumber %d = %d\n",{even,j, lucky[j]})
elsif range then
if k>length(lucky) then
fatal(sprintf("the argument, %d, is too big", k))
end if
printf(1,"Lucky %snumbers %d to %d are: %s\n",{even,j,k,sprint(lucky[j..k])})
else
if j>lucky[$] then
fatal("start of range is too big")
elsif k>lucky[$] then
fatal("end of range is too big")
end if
m = abs(binary_search(j,lucky))
n = binary_search(k,lucky)
if n<0 then n = -n-1 end if
printf(1,"Lucky %snumbers between %d and %d are: %s\n", {even,j,k,sprint(lucky[m..n])})
end if
end if
end procedure
main()</lang>
{{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
Line 2,271 ⟶ 2,468:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(off *Even)
 
(de nn (Lst N)
Line 2,293 ⟶ 2,490:
(if (lt0 B)
(filter '((N) (<= A N (abs B))) *Lst)
(head B (nth *Lst A)) ) ) )</langsyntaxhighlight>
{{out}}
<pre>$ pil ./lucky.l 1 20
Line 2,306 ⟶ 2,503:
=={{header|Python}}==
The generator
<langsyntaxhighlight lang="python">from __future__ import print_function
 
def lgen(even=False, nmax=1000000):
Line 2,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 2,421 ⟶ 2,618:
 
if __name__ == '__main__':
arghandler(' '.join(sys.argv[1:]))</langsyntaxhighlight>
 
{{out}}
Line 2,439 ⟶ 2,636:
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.
<langsyntaxhighlight lang="python">from itertools import count
def lgen(even=False):
lucky = []
Line 2,449 ⟶ 2,646:
lucky.append(2*k+1 + even)
yield 2*k+1 + even
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>sub luck(\a,\b) {
gather {
my @taken = take a;
Line 2,494 ⟶ 2,691:
multi MAIN (Int $min where * > 0, Int $neg-max where * < 0, Luck $howlucky = 'lucky') {
say grep * >= $min, (@::(lc $howlucky) ...^ * > abs $neg-max);
}</langsyntaxhighlight>
{{out}}
<pre>$ ./lucky
Line 2,521 ⟶ 2,718:
=={{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.*/
Line 2,567 ⟶ 2,764:
do b=1 for @.0; _= _ @.b /*construct a list of integers.*/
end /*b*/
return space(_) /*remove superfluous blanks. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 1 &nbsp; 20 &nbsp; lucky </tt>}}
<pre>
Line 2,599 ⟶ 2,796:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Lucky and even lucky numbers
 
Line 2,688 ⟶ 2,885:
see svect
see "]" + nl
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,703 ⟶ 2,900:
[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 2,750 ⟶ 3,001:
if __FILE__ == $0
lucky(ARGV)
end</langsyntaxhighlight>
 
{{out}}
Line 2,782 ⟶ 3,033:
The lucky numbers sequence:
 
<langsyntaxhighlight lang="swift">struct LuckyNumbers : Sequence, IteratorProtocol {
let even: Bool
let through: Int
Line 2,824 ⟶ 3,075:
return nil
}
}</langsyntaxhighlight>
 
The main file:
<langsyntaxhighlight lang="swift">let args = Array(CommandLine.arguments.dropFirst())
 
guard let sj = args.first, let j = Int(sj), j > 0, j <= 10_000 else {
Line 2,900 ⟶ 3,151:
case _:
fatalError()
}</langsyntaxhighlight>
 
{{out}}
Line 2,919 ⟶ 3,170:
{{works with|Tcl|8.6}}
{{trans|Python}}
<langsyntaxhighlight lang="tcl">#!/usr/bin/env tclsh8.6
package require Tcl 8.6
 
Line 2,987 ⟶ 3,238:
} else {
puts "$from'th to $to'th $evenOdd numbers: [join [collectIndices $l [incr from -1] [incr to -1]] ,]"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,006 ⟶ 3,257:
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|Wren-traititerate}}
{{libheader|Wren-str}}
<langsyntaxhighlight ecmascriptlang="wren">import "os" for Process
import "./traititerate" for Stepped
import "./str" for Str
 
var luckyOdd = List.filled(1e5, 0)
Line 3,140 ⟶ 3,391:
if (j > l) Fiber.abort("The second argument can't be less in absolute value than first")
printBetween.call(j, l, odd)
}</langsyntaxhighlight>
 
{{out}}
<pre>
$ wren luckyLucky_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 luckyLucky_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 luckyLucky_and_even_lucky_numbers.wren 6000 -6100
Lucky numbers between 6000 and 6100 are:
[6009, 6019, 6031, 6049, 6055, 6061, 6079, 6093]
 
$ wren luckyLucky_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 luckyLucky_and_even_lucky_numbers.wren 10000 , lucky
Lucky number 10000 = 115591
 
$ wren luckyLucky_and_even_lucky_numbers.wren 10000 , evenLucky
Lucky even number 10000 = 111842
</pre>
Line 3,169 ⟶ 3,420:
=={{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 3,178 ⟶ 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 3,211 ⟶ 3,462:
}
}
}catch(TheEnd){ options() }</langsyntaxhighlight>
{{out}}
<pre>
1,150

edits