Skip to content

Instantly share code, notes, and snippets.

@rpf5573
Last active January 15, 2019 10:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rpf5573/6155304c12ede82ae89f73e83d5d6f0d to your computer and use it in GitHub Desktop.
Save rpf5573/6155304c12ede82ae89f73e83d5d6f0d to your computer and use it in GitHub Desktop.
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