Last active
January 15, 2019 10:51
-
-
Save rpf5573/6155304c12ede82ae89f73e83d5d6f0d to your computer and use it in GitHub Desktop.
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 transform(input) { | |
var result = [0]; | |
for ( var i = 0; i < input; i++ ) { | |
up(result); | |
} | |
return result.reverse(); | |
} | |
function up(array) { | |
array[0]++; | |
for ( var i = 0; i < array.length; i++ ) { | |
if ( array[i] == 2 ) { | |
array[i] = 0; | |
if ( array[i+1] ) { | |
array[i+1]++; | |
}else { | |
array[i+1] = 1; | |
} | |
} else { | |
return; | |
} | |
} | |
} | |
transform(8); // [1, 0, 0, 0]; | |
transform(20); // [1, 0, 1, 0, 0]; | |
transform(41); // [1, 0, 1, 0, 0, 1]; | |
transform(65); // [1, 0, 0, 0, 0, 0, 1]; | |
// done in 0.078 seconds |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment