Last active
May 5, 2023 17:00
-
-
Save avestura/1934840e551dbf6de3d5c465dd61d958 to your computer and use it in GitHub Desktop.
Get a "V" and "P" representation of dragon fractal
This file contains hidden or 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
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