Skip to content

Instantly share code, notes, and snippets.

@avestura
Last active May 5, 2023 17:00
Show Gist options
  • Save avestura/1934840e551dbf6de3d5c465dd61d958 to your computer and use it in GitHub Desktop.
Save avestura/1934840e551dbf6de3d5c465dd61d958 to your computer and use it in GitHub Desktop.
Get a "V" and "P" representation of dragon fractal
const getDragonBit = (n) => {
while ((n & 1) == 0) {
n = n >> 1;
}
return 1 - ((n >> 1) & 1)
}
const getDragon = n => {
let str = ""
for (i = 1; i < Math.pow(2, n); i++) {
str = (getDragonBit(i) == 1 ? "V" : "P") + str
}
return str
}
const n = 4;
console.log(getDragon(n)) // PPVPPVVVPPVVPVV
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment