2020-05-05 15:22:32 +05:30
---
id: 5ea281203167d2b0bdefca00
title: Ludic numbers
challengeType: 5
2020-06-08 20:07:41 +05:30
forumTopicId: 385282
2021-01-13 03:31:00 +01:00
dashedName: ludic-numbers
2020-05-05 15:22:32 +05:30
---
2020-11-27 19:02:05 +01:00
# --description--
[Ludic numbers ](https://oeis.org/wiki/Ludic_numbers ) are related to prime numbers as they are generated by a sieve quite like the [Sieve of Eratosthenes ](https://rosettacode.org/wiki/Sieve_of_Eratosthenes ) is used to generate prime numbers.
The first ludic number is 1.
To generate succeeding ludic numbers create an array of increasing integers starting from 2.
< code style = 'margin-left: 2em;' > < span style = 'color:blue;font-weight:bold' > 2< / span > 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 ...< / code >
2020-05-05 15:22:32 +05:30
(Loop)
2020-11-27 19:02:05 +01:00
2020-05-05 15:22:32 +05:30
< ul >
2020-11-27 19:02:05 +01:00
< li > Take the first member of the resultant array as the next ludic number < span style = 'color:blue;font-weight:bold' > 2< / span > .< / li >
2020-05-05 15:22:32 +05:30
< li > Remove every < strong > 2< sup > nd< / sup > < / strong > indexed item from the array (including the first).< / li >
2020-11-27 19:02:05 +01:00
< code style = 'margin-left: 2em;' > < span style = 'color:blue;font-weight:bold;' > < s > 2< / s > < / span > 3 < s > 4< / s > 5 < s > 6< / s > 7 < s > 8< / s > 9 < s > 10< / s > 11 < s > 12< / s > 13 < s > 14< / s > 15 < s > 16< / s > 17 < s > 18< / s > 19 < s > 20< / s > 21 < s > 22< / s > 23 < s > 24< / s > 25 < s > 26< / s > ...< / code >
2020-05-05 15:22:32 +05:30
< / ul >
2020-11-27 19:02:05 +01:00
2020-05-05 15:22:32 +05:30
< ul >
< li > (Unrolling a few loops...)< / li >
2020-11-27 19:02:05 +01:00
< li > Take the first member of the resultant array as the next ludic number < span style = 'color:blue;font-weight:bold' > 3< / span > .< / li >
2020-05-05 15:22:32 +05:30
< li > Remove every < strong > 3< sup > rd< / sup > < / strong > indexed item from the array (including the first).< / li >
2020-11-27 19:02:05 +01:00
< code style = 'margin-left: 2em;' > < span style = 'color:blue;font-weight:bold' > < s > 3< / s > < / span > 5 7 < s > 9< / s > 11 13 < s > 15< / s > 17 19 < s > 21< / s > 23 25 < s > 27< / s > 29 31 < s > 33< / s > 35 37 < s > 39< / s > 41 43 < s > 45< / s > 47 49 < s > 51< / s > ...< / code >
2020-05-05 15:22:32 +05:30
< / ul >
2020-11-27 19:02:05 +01:00
2020-05-05 15:22:32 +05:30
< ul >
2020-11-27 19:02:05 +01:00
< li > Take the first member of the resultant array as the next ludic number < span style = 'color:blue;font-weight:bold' > 5< / span > .< / li >
2020-05-05 15:22:32 +05:30
< li > Remove every < strong > 5< sup > th< / sup > < / strong > indexed item from the array (including the first).< / li >
2020-11-27 19:02:05 +01:00
< code style = 'margin-left: 2em;' > < span style = 'color:blue;font-weight:bold' > < s > 5< / s > < / span > 7 11 13 17 < s > 19< / s > 23 25 29 31 < s > 35< / s > 37 41 43 47 < s > 49< / s > 53 55 59 61 < s > 65< / s > 67 71 73 77 ...< / code >
2020-05-05 15:22:32 +05:30
< / ul >
2020-11-27 19:02:05 +01:00
2020-05-05 15:22:32 +05:30
< ul >
2020-11-27 19:02:05 +01:00
< li > Take the first member of the resultant array as the next ludic number < span style = 'color:blue;font-weight:bold' > 7< / span > .< / li >
2020-05-05 15:22:32 +05:30
< li > Remove every < strong > 7< sup > th< / sup > < / strong > indexed item from the array (including the first).< / li >
2020-11-27 19:02:05 +01:00
< code style = 'margin-left: 2em;' > < span style = 'color:blue;font-weight:bold' > < s > 7< / s > < / span > 11 13 17 23 25 29 < s > 31< / s > 37 41 43 47 53 55 < s > 59< / s > 61 67 71 73 77 83 < s > 85< / s > 89 91 97 ...< / code >
2020-05-05 15:22:32 +05:30
< / ul >
2020-11-27 19:02:05 +01:00
2020-05-05 15:22:32 +05:30
< ul >
< li > < big > < b > ... < / b > < / big > < / li >
2020-11-27 19:02:05 +01:00
< li > Take the first member of the current array as the next ludic number < span style = 'color:blue;font-weight:bold' > L< / span > .< / li >
2020-05-05 15:22:32 +05:30
< li > Remove every < strong > L< sup > th< / sup > < / strong > indexed item from the array (including the first).< / li >
< li > < big > < b > ... < / b > < / big > < / li >
< / ul >
2020-11-27 19:02:05 +01:00
# --instructions--
2020-05-05 15:22:32 +05:30
Write a function that returns all the ludic numbers less than or equal to the given number.
2020-11-27 19:02:05 +01:00
# --hints--
`ludic` should be a function.
```js
assert(typeof ludic === 'function', '< code > ludic< / code > should be a function.');
2020-05-05 15:22:32 +05:30
```
2020-11-27 19:02:05 +01:00
`ludic(2)` should return a array.
2020-05-05 15:22:32 +05:30
2020-11-27 19:02:05 +01:00
```js
assert(Array.isArray(ludic(2)));
```
2020-05-05 15:22:32 +05:30
2020-11-27 19:02:05 +01:00
`ludic(2)` should return `[1, 2]` .
2020-05-05 15:22:32 +05:30
2020-11-27 19:02:05 +01:00
```js
assert.deepEqual(ludic(2), [1, 2]);
```
`ludic(3)` should return `[1, 2, 3]` .
2020-05-05 15:22:32 +05:30
```js
2020-11-27 19:02:05 +01:00
assert.deepEqual(ludic(3), [1, 2, 3]);
```
2020-06-08 20:07:41 +05:30
2020-11-27 19:02:05 +01:00
`ludic(5)` should return `[1, 2, 3, 5]` .
```js
assert.deepEqual(ludic(5), [1, 2, 3, 5]);
```
`ludic(20)` should return `[1, 2, 3, 5, 7, 11, 13, 17]` .
```js
assert.deepEqual(ludic(20), [1, 2, 3, 5, 7, 11, 13, 17]);
```
`ludic(26)` should return `[1, 2, 3, 5, 7, 11, 13, 17, 23, 25]` .
```js
assert.deepEqual(ludic(26), [1, 2, 3, 5, 7, 11, 13, 17, 23, 25]);
2020-05-05 15:22:32 +05:30
```
2020-11-27 19:02:05 +01:00
# --seed--
2020-05-05 15:22:32 +05:30
2020-11-27 19:02:05 +01:00
## --seed-contents--
2020-05-05 15:22:32 +05:30
2020-11-27 19:02:05 +01:00
```js
function ludic(n) {
2020-05-05 15:22:32 +05:30
2020-11-27 19:02:05 +01:00
}
```
# --solutions--
2020-05-05 15:22:32 +05:30
```js
function ludic(n) {
const makeArr = (s, e) => new Array(e + 1 - s).fill(s).map((e, i) => e + i);
const filterAtInc = (arr, n) => arr.filter((e, i) => (i + 1) % n);
const makeLudic = (arr, result) => {
const iter = arr.shift();
result.push(iter);
return arr.length ? makeLudic(filterAtInc(arr, iter), result) : result;
};
const ludicResult = makeLudic(makeArr(2, n), [1]);
return ludicResult;
}
```