Last active
June 21, 2018 18:12
-
-
Save FaberVitale/5bc208e056e44cb64a96358020b4524c to your computer and use it in GitHub Desktop.
Ducci Sequence https://en.wikipedia.org/wiki/Ducci_sequence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(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