This idea for searching is from TI-Basic Developer, and is quite brilliant:
Let's suppose you have a value named x
and a list named L
.
:If max(1/(1+(abs(L-x))))=1
:Then
//value is in list
:Else
//value is not in list
:End
And that's it!
Here is how it works:
abs(L-x)
- Firstly, it subtracts the searched number from every value of a list and gets its absolute value.
max(1/(1+(abs(L-x))))
- After that, it searches for the largest element in it, adds it to 1 and divides 1 by it.
:If max(1/(1+(abs(L-x))))=1
- If it's 1, than the value is in the list. Why? Because
1 / 1 + 0
is 1 (a number minus itself is always 0) and 0 is the maximum possible value for 1 / 1 + x
(for positive numbers, of course). If the maximum is smaller than 1, it's certain that the searched value is not inside the list.