Skip to content

Instantly share code, notes, and snippets.

@ricmarinovic
Last active July 5, 2019 00:18
Show Gist options
  • Save ricmarinovic/913bb1ed1ca16d0a6b1c90a4e2997015 to your computer and use it in GitHub Desktop.
Save ricmarinovic/913bb1ed1ca16d0a6b1c90a4e2997015 to your computer and use it in GitHub Desktop.
guess number
defmodule Playground do
def guess(actual, a..b) do
do_guess(actual, a..b, div(b, 2))
end
def do_guess(actual, a.._b, number) when actual < number do
print_number(number)
do_guess(actual, a..number, div(a + number, 2))
end
def do_guess(actual, _a..b, number) when actual > number do
print_number(number)
do_guess(actual, number..b, div(number + b, 2))
end
def do_guess(number, _, number) do
print_number(number)
IO.puts("#{number}")
end
def print_number(number), do: IO.puts("Is it #{number}")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment