return false
var arr: array = [1, 2, 3, 4, 6, 8, 12, 45, 67, 80, 90, 100] stdout.write(find_x(arr, 120))
Assuming that the code is just wrongly formatted and it looks like this:
proc find_x(list: openArray[int], x: int): bool =
var lo = list.low
var hi = list.high
while lo < hi:
var m = int((lo + (hi - lo)) / 2)
if x == list[m]:
return true
elif x < list[m]:
hi = m - 1
else:
lo = m
return false
You have a small error here:
var m = int((lo + (hi - lo)) / 2)
It should be:
var m = lo + int((hi+lo) / 2)
I would also use integer division instead of converting to int:
var m = lo + (hi + lo) div 2
P.S. You can format code nicely on the forum if you put it in triple backticks, like this:
```Nim
echo "Hello World!"
```
While loop should use:
while lo <= hi:
Or it would fail on lists of length 1.
And this change also needed:
else:
lo = m + 1
Otherwise search gets stuck on last two elements of list, when x > last element.echo "Thanks"