2018-09-30 23:01:58 +01:00
---
id: 5a23c84252665b21eecc7e7b
2020-11-27 19:02:05 +01:00
title: Generator/Exponential
2018-09-30 23:01:58 +01:00
challengeType: 5
2019-08-05 09:17:33 -07:00
forumTopicId: 302275
2021-01-13 03:31:00 +01:00
dashedName: generatorexponential
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
2018-09-30 23:01:58 +01:00
A generator is an executable entity (like a function or procedure) that contains code that yields a sequence of values, one at a time, so that each time you call the generator, the next value in the sequence is provided.
2020-11-27 19:02:05 +01:00
2019-05-22 23:41:41 +09:00
Generators are often built on top of coroutines or objects so that the internal state of the object is handled "naturally".
2020-11-27 19:02:05 +01:00
2018-09-30 23:01:58 +01:00
Generators are often used in situations where a sequence is potentially infinite, and where it is possible to construct the next value of the sequence with only minimal state.
2020-11-27 19:02:05 +01:00
# --instructions--
2019-03-05 14:39:16 +09:00
Write a function that uses generators to generate squares and cubes. Create a new generator that filters all cubes from the generator of squares.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
The function should return the \\( n^{th} \\) value of the filtered generator.
For example for \\(n=7\\), the function should return 81 as the sequence would be 4, 9, 16, 25, 36, 49, 81. Here 64 is filtered out, as it is a cube.
# --hints--
`exponentialGenerator` should be a function.
```js
assert(typeof exponentialGenerator == 'function');
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
`exponentialGenerator()` should return a number.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(typeof exponentialGenerator(10) == 'number');
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`exponentialGenerator(10)` should return `144` .
2018-09-30 23:01:58 +01:00
```js
2020-11-27 19:02:05 +01:00
assert.equal(exponentialGenerator(10), 144);
```
2020-09-15 09:57:40 -07:00
2020-11-27 19:02:05 +01:00
`exponentialGenerator(12)` should return `196` .
```js
assert.equal(exponentialGenerator(12), 196);
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
`exponentialGenerator(14)` should return `256` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert.equal(exponentialGenerator(14), 256);
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`exponentialGenerator(20)` should return `484` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert.equal(exponentialGenerator(20), 484);
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`exponentialGenerator(25)` should return `784` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert.equal(exponentialGenerator(25), 784);
```
# --seed--
## --seed-contents--
```js
function exponentialGenerator(n) {
}
```
# --solutions--
2018-09-30 23:01:58 +01:00
```js
function exponentialGenerator(n){
function* PowersGenerator(m) {
2020-11-27 19:02:05 +01:00
var n=0;
while(1) {
yield Math.pow(n, m);
n += 1;
}
2018-09-30 23:01:58 +01:00
}
function* FilteredGenerator(g, f){
2020-11-27 19:02:05 +01:00
var value = g.next().value;
var filter = f.next().value;
while(1) {
if( value < filter ) {
yield value;
value = g.next().value;
} else if ( value > filter ) {
filter = f.next().value;
} else {
value = g.next().value;
filter = f.next().value;
}
}
2018-09-30 23:01:58 +01:00
}
var squares = PowersGenerator(2);
var cubes = PowersGenerator(3);
var filtered = FilteredGenerator(squares, cubes);
var curr=0;
for(var i=0;i< n ; i + + ) curr = filtered.next();
return curr.value;
}
```