Lucky and even lucky numbers: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|Phix}}: made and marked p2js compatible)
m (syntax highlighting fixup automation)
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,433:
=={{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:
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:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.51
 
typealias IAE = IllegalArgumentException
Line 1,724:
printBetween(j, l, odd)
}
}</langsyntaxhighlight>
 
{{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:
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[GetLuckies, GetEvenLuckies]
GetLuckies[max_] := Module[{luckies, f, i},
luckies = Range[1, max, 2];
Line 1,867:
]
]
GiveLucky[Last@$ScriptCommandLine]</langsyntaxhighlight>
{{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.
<langsyntaxhighlight Nimlang="nim">import os, strformat, strutils
 
type LuckyKind {.pure.} = enum Lucky = "lucky", EvenLucky = "evenlucky"
Line 2,006:
else:
# Print values in range j..(-k).
printInRange(j, -k, kind)</langsyntaxhighlight>
 
{{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}}
<langsyntaxhighlight lang="perl">use Perl6::GatherTake;
 
sub luck {
Line 2,080:
}
 
print "\n"</langsyntaxhighlight>
{{out}}
<pre>$ ./lucky
Line 2,102:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="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>
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>
<!--</langsyntaxhighlight>-->
{{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}}==
<langsyntaxhighlight PicoLisplang="picolisp">(off *Even)
 
(de nn (Lst N)
Line 2,304:
(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,317:
=={{header|Python}}==
The generator
<langsyntaxhighlight lang="python">from __future__ import print_function
 
def lgen(even=False, nmax=1000000):
Line 2,330:
# drain
for i in lst[n:]:
yield i</langsyntaxhighlight>
 
The argument handler
<langsyntaxhighlight lang="python">from itertools import islice
import sys, re
 
Line 2,432:
 
if __name__ == '__main__':
arghandler(' '.join(sys.argv[1:]))</langsyntaxhighlight>
 
{{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.
<langsyntaxhighlight lang="python">from itertools import count
def lgen(even=False):
lucky = []
Line 2,460:
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,505:
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,532:
=={{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,578:
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,610:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Lucky and even lucky numbers
 
Line 2,699:
see svect
see "]" + nl
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,717:
=={{header|Ruby}}==
{{trans|Python}}
<langsyntaxhighlight lang="ruby">def generator(even=false, nmax=1000000)
start = even ? 2 : 1
Enumerator.new do |y|
Line 2,761:
if __FILE__ == $0
lucky(ARGV)
end</langsyntaxhighlight>
 
{{out}}
Line 2,793:
The lucky numbers sequence:
 
<langsyntaxhighlight lang="swift">struct LuckyNumbers : Sequence, IteratorProtocol {
let even: Bool
let through: Int
Line 2,835:
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,911:
case _:
fatalError()
}</langsyntaxhighlight>
 
{{out}}
Line 2,930:
{{works with|Tcl|8.6}}
{{trans|Python}}
<langsyntaxhighlight lang="tcl">#!/usr/bin/env tclsh8.6
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]] ,]"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,019:
{{libheader|Wren-trait}}
{{libheader|Wren-str}}
<langsyntaxhighlight lang="ecmascript">import "os" for Process
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)
}</langsyntaxhighlight>
 
{{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.
<langsyntaxhighlight lang="zkl">fcn lgen(a){
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 }</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,222:
}
}
}catch(TheEnd){ options() }</langsyntaxhighlight>
{{out}}
<pre>
10,327

edits