--- id: 5ea281203167d2b0bdefca00 title: Ludic numbers challengeType: 5 isHidden: false --- ## Description
Ludic numbers are related to prime numbers as they are generated by a sieve quite like the 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. 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 ... (Loop)
## Instructions
Write a function that returns all the ludic numbers less than or equal to the given number.
## Tests
```yml tests: - text: ludic should be a function. testString: assert(typeof ludic === 'function', 'ludic should be a function.'); - text: ludic(2) should return a array. testString: assert(Array.isArray(ludic(2))); - text: ludic(2) should return [1, 2]. testString: assert.deepEqual(ludic(2), [1, 2]); - text: ludic(3) should return [1, 2, 3]. testString: assert.deepEqual(ludic(3), [1, 2, 3]); - text: ludic(5) should return [1, 2, 3, 5]. testString: assert.deepEqual(ludic(5), [1, 2, 3, 5]); - text: ludic(20) should return [1, 2, 3, 5, 7, 11, 13, 17]. testString: assert.deepEqual(ludic(20), [1, 2, 3, 5, 7, 11, 13, 17]); - text: ludic(26) should return [1, 2, 3, 5, 7, 11, 13, 17, 23, 25]. testString: assert.deepEqual(ludic(26), [1, 2, 3, 5, 7, 11, 13, 17, 23, 25]); ```
## Challenge Seed
```js function ludic(n) { } ```
## Solution
```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; } ```