Created
December 4, 2020 01:04
-
-
Save ssisaias/3809cd7408e33a75c2c98c7cffa0720b to your computer and use it in GitHub Desktop.
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
var primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] ; | |
function binS(x){ | |
var n = primes.length; | |
var min = 0; | |
var max = n-1; | |
var found = false; | |
var lookupPos = 0; | |
while(min<=max){ | |
lookupPos = Math.floor((min+max)/2); | |
println(); | |
console.log(lookupPos + "::Pos - Val::" + primes[lookupPos]); | |
if (primes[lookupPos]===x){ | |
found = true; | |
break; | |
} | |
if(primes[lookupPos]<x){ | |
min = ++lookupPos; | |
} | |
if(primes[lookupPos]>x){ | |
max = --lookupPos; | |
} | |
} | |
if(found){ | |
console.log("found pos: " + lookupPos) | |
} | |
else{console.log(-1)} | |
} | |
// console.log("put the number to look for: "); | |
// var number = prompt('question', -1); | |
binS(3); | |
binS(5); | |
binS(7); | |
binS(41); | |
binS(99); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment