Skip to content

Instantly share code, notes, and snippets.

@pjivers
Last active September 1, 2017 00:53
Show Gist options
  • Save pjivers/36a49ed8be7c427330c0efaaf66a34b8 to your computer and use it in GitHub Desktop.
Save pjivers/36a49ed8be7c427330c0efaaf66a34b8 to your computer and use it in GitHub Desktop.
Find a given number
var toFind = 1e15;
var query = 1;
var increment = 1;
var i = 1
for ( ; ; i++) {
console.log('Query = ' + query);
if (query === toFind) {
break;
} else if (query > toFind) {
query -= increment / 2;
increment = 1;
}
query += increment;
increment *= 2;
}
console.log('Found ' + query);
console.log('Iterations = ' + i);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment