Skip to content

Instantly share code, notes, and snippets.

@FaberVitale
Last active June 21, 2018 18:12
Show Gist options
  • Save FaberVitale/5bc208e056e44cb64a96358020b4524c to your computer and use it in GitHub Desktop.
Save FaberVitale/5bc208e056e44cb64a96358020b4524c to your computer and use it in GitHub Desktop.
;(function() {
"use strict";
const hashIteration = seq => seq.join(",");
const nextIteration = seq =>
seq.map((num, index, array) =>
Math.abs(num - array[(index + 1) % array.length])
);
function* ducciSequence(initialValue = []) {
let prev = initialValue;
let hash = hashIteration(initialValue);
const iterations = new Set([hash.replace(/\d+/g, "0")]);
yield prev;
while (!iterations.has(hash)) {
iterations.add(hash);
yield (prev = nextIteration(prev));
hash = hashIteration(prev);
}
}
if (
typeof module !== "undefined" &&
module != null &&
module.exports != null
) {
module.exports = ducciSequence;
} else if (typeof window !== "undefined") {
window.ducciSequence = ducciSequence;
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment