RosettaCodeData/Task/Binary-search/Maxima/binary-search.maxima

21 lines
463 B
Plaintext

find(L, n) := block([i: 1, j: length(L), k, p],
if n < L[i] or n > L[j] then 0 else (
while j - i > 0 do (
k: quotient(i + j, 2),
p: L[k],
if n < p then j: k - 1 elseif n > p then i: k + 1 else i: j: k
),
if n = L[i] then i else 0
)
)$
".."(a, b) := if a < b then makelist(i, i, a, b) else makelist(i, i, a, b, -1)$
infix("..")$
a: sublist(1 .. 1000, primep)$
find(a, 27);
0
find(a, 421);
82