Lucky and even lucky numbers: Difference between revisions
m
syntax highlighting fixup automation
m (→{{header|Phix}}: made and marked p2js compatible) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 92:
{{trans|Nim}}
<
F initLuckyNumbers(nelems, evenlucky, limit = -1)
Line 176:
print()
E
process_args(:argv[1..])</
{{out}}
Line 205:
=={{header|C}}==
{{trans|C++}}
<
#include <stdio.h>
#include <stdlib.h>
Line 431:
return 0;
}</
{{out}}
<pre>LuckyNumbers.exe -j=1 -k=20
Line 453:
=={{header|C++}}==
{{trans|Go}}
<
#include <iostream>
#include <iterator>
Line 629:
return 0;
}</
{{out}}
<pre>>LuckyNumbers.exe -j=1 -k=20
Line 650:
=={{header|D}}==
<
import std.concurrency;
import std.conv;
Line 735:
lucky(evenLucky).drop(j-1).take(1).writeln;
}
}</
{{out}}
Line 758:
=={{header|F_Sharp|F#}}==
===The functions===
<
// 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===
Line 783:
=={{header|Go}}==
{{trans|Kotlin}}
<
import (
Line 975:
}
}
}</
{{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">
import System.Environment
import Text.Regex.Posix
Line 1,085:
_ -> do
putStrLn "Invalid input, wrong number of arguments"
putStrLn "Type --help"</
{{out}}
<pre>$ luckyNumbers 1 20
Line 1,106:
Implementation:
<
1 luckySeq y
:
Line 1,133:
)
thru=: <./ + i.@(+*)@-~</
Task:
<
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</
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">
import java.util.ArrayList;
import java.util.Collections;
Line 1,222:
}
</syntaxhighlight>
{{out}}
Line 1,247:
=={{header|JavaScript}}==
First: the function.
<syntaxhighlight 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>
{{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.
<
(function() {
document.write(`
Line 1,409:
}
})();
</syntaxhighlight>
Using the task demonstrations again, the I/O part looks like:
{{out}}<pre>
Line 1,433:
=={{header|Julia}}==
This iterator for lucky numbers is semi-lazy: it completes one pass of the filter each iteration.
<
struct Lucky
Line 1,586:
end
runopts()</
> 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:
=={{header|Kotlin}}==
<
typealias IAE = IllegalArgumentException
Line 1,724:
printBetween(j, l, odd)
}
}</
{{out}}
Line 1,753:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Save the following code in the file script.wls and execute the wolframscript from the command line:
<
GetLuckies[max_] := Module[{luckies, f, i},
luckies = Range[1, max, 2];
Line 1,867:
]
]
GiveLucky[Last@$ScriptCommandLine]</
{{out}}
<pre>wolframscript -file script.wls -args "1 20 lucky"
Line 1,884:
=={{header|Nim}}==
For the generation of lucky numbers, we use the second Python algorithm, modified to return a sequence.
<
type LuckyKind {.pure.} = enum Lucky = "lucky", EvenLucky = "evenlucky"
Line 2,006:
else:
# Print values in range j..(-k).
printInRange(j, -k, kind)</
{{out}}
Line 2,030:
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}}
<
sub luck {
Line 2,080:
}
print "\n"</
{{out}}
<pre>$ ./lucky
Line 2,102:
=={{header|Phix}}==
<!--<
<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>
Line 2,258:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</
{{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
Line 2,282:
=={{header|PicoLisp}}==
<
(de nn (Lst N)
Line 2,304:
(if (lt0 B)
(filter '((N) (<= A N (abs B))) *Lst)
(head B (nth *Lst A)) ) ) )</
{{out}}
<pre>$ pil ./lucky.l 1 20
Line 2,317:
=={{header|Python}}==
The generator
<
def lgen(even=False, nmax=1000000):
Line 2,330:
# drain
for i in lst[n:]:
yield i</
The argument handler
<
import sys, re
Line 2,432:
if __name__ == '__main__':
arghandler(' '.join(sys.argv[1:]))</
{{out}}
Line 2,450:
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.
<
def lgen(even=False):
lucky = []
Line 2,460:
lucky.append(2*k+1 + even)
yield 2*k+1 + even
</syntaxhighlight>
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku"
gather {
my @taken = take a;
Line 2,505:
multi MAIN (Int $min where * > 0, Int $neg-max where * < 0, Luck $howlucky = 'lucky') {
say grep * >= $min, (@::(lc $howlucky) ...^ * > abs $neg-max);
}</
{{out}}
<pre>$ ./lucky
Line 2,532:
=={{header|REXX}}==
This REXX version does extra error checking for the arguments.
<
parse arg bot top func _ . /*obtain required & optional arguments.*/
if func=='' then func= 'lucky' /*Not specified? Then use the default.*/
Line 2,578:
do b=1 for @.0; _= _ @.b /*construct a list of integers.*/
end /*b*/
return space(_) /*remove superfluous blanks. */</
{{out|output|text= when using the input of: <tt> 1 20 lucky </tt>}}
<pre>
Line 2,610:
=={{header|Ring}}==
<
# Project : Lucky and even lucky numbers
Line 2,699:
see svect
see "]" + nl
</syntaxhighlight>
Output:
<pre>
Line 2,717:
=={{header|Ruby}}==
{{trans|Python}}
<
start = even ? 2 : 1
Enumerator.new do |y|
Line 2,761:
if __FILE__ == $0
lucky(ARGV)
end</
{{out}}
Line 2,793:
The lucky numbers sequence:
<
let even: Bool
let through: Int
Line 2,835:
return nil
}
}</
The main file:
<
guard let sj = args.first, let j = Int(sj), j > 0, j <= 10_000 else {
Line 2,911:
case _:
fatalError()
}</
{{out}}
Line 2,930:
{{works with|Tcl|8.6}}
{{trans|Python}}
<
package require Tcl 8.6
Line 2,998:
} else {
puts "$from'th to $to'th $evenOdd numbers: [join [collectIndices $l [incr from -1] [incr to -1]] ,]"
}</
{{out}}
<pre>
Line 3,019:
{{libheader|Wren-trait}}
{{libheader|Wren-str}}
<
import "/trait" for Stepped
import "/str" for Str
Line 3,151:
if (j > l) Fiber.abort("The second argument can't be less in absolute value than first")
printBetween.call(j, l, odd)
}</
{{out}}
Line 3,180:
=={{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.
<
ns,idx:=[a..*,2],2;
vm.yield(ns.next());
Line 3,189:
}
}
fcn skipper(n,skp,cnt){ z:=cnt.inc(); if(z%skp==0) Void.Skip else n }</
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.
<
try{
j=cmdLineArgs[0].toInt();
Line 3,222:
}
}
}catch(TheEnd){ options() }</
{{out}}
<pre>
|