Jump to content

Lucky and even lucky numbers: Difference between revisions

JavaScript added
m (→‎{{header|REXX}}: added/changed whitespace and comments, used templates for the output sections.)
(JavaScript added)
Line 1,130:
EvenLuckyNumber(10000) = 111842
</pre>
 
=={{header|JavaScript}}==
First: the function.
<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 */
// blank
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}) );
</lang>
{{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.
 
<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 luckNumbers 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>
`)
}
}
})();
</lang>
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|Julia}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.