June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
24
Task/Binary-search/PARI-GP/binary-search-2.pari
Normal file
24
Task/Binary-search/PARI-GP/binary-search-2.pari
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
binarysearch(v, x) = {
|
||||
local(
|
||||
minm = 1,
|
||||
maxm = length(v),
|
||||
guess = floor(maxm/2+minm/2)
|
||||
);
|
||||
|
||||
while(v[guess] != x,
|
||||
if(v[guess] < x, minm = guess + 1, maxm = guess - 1);
|
||||
if(minm > maxm,
|
||||
guess = 0;
|
||||
break
|
||||
);
|
||||
guess = floor(maxm/2+minm/2)
|
||||
);
|
||||
|
||||
return(guess);
|
||||
}
|
||||
|
||||
idx = binarysearch([1,4,9,16,25,36,49,64,81,100,121,144], 121);
|
||||
if(idx, \
|
||||
print("Item exists on index ", idx), \
|
||||
print("Item does not exist anywhere.") \
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue