function HotChocolatePool() { this.scoreboard = []; this.loopCount =0; } HotChocolatePool.prototype.create = function(score) { this.scoreboard.push(score); return score; } HotChocolatePool.prototype.combine = function(a, b) { var result = [], resultStr = "" +(a +b); for (var i =0, len = resultStr.length; i < len; ++i) { var child = this.create(parseInt(resultStr.charAt(i))); result.push(child); } return result; } HotChocolatePool.prototype.getAtIndex = function(i) { return this.scoreboard[i]; } function simPool(a, b, stopCondition) { var pool = new HotChocolatePool(), a = pool.create(a), aIndex = 0, b = pool.create(b), bIndex = 1; while (stopCondition(pool)) { pool.combine(a, b); a = pool.getAtIndex(aIndex = ((aIndex +a +1) % pool.scoreboard.length)) b = pool.getAtIndex(bIndex = ((bIndex +b +1) % pool.scoreboard.length)); ++pool.loopCount; } return pool; } function ex1(count) { var pool = simPool(3, 7, p => p.scoreboard.length < 10 +count), result = pool.scoreboard.slice(count, count +10); console.log(result); } function ex2(arr) { var pool = simPool(3, 7, p => { var sb = p.scoreboard; if (sb.length < arr.length) return true; for (var i =Math.max(0, sb.length -arr.length -5), lenI = sb.length -arr.length; i < lenI; ++i) { var found = true; for (var j =0, lenJ =arr.length; j < lenJ && found; ++j) if (arr[j] != sb[i +j]) found = false; if (found) { console.log("Found at index ", i); return false; } } return true; }); } ex1(430971); ex2([4, 3, 0, 9, 7, 1]);