blob: ee5c6786c4be6bdf4baaeaefa7f9d18a0a75a759 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# Human in the loop fibonacci
from functools import cache
import sys
req = 0
cur = 0
def compute(query, check):
global cur, req
while True:
try:
result = int(input(f"Please compute {query}: "))
if check(result):
break
except ValueError:
pass
print("Hmm, that didn't seem right...")
cur += 1
print(f"Computation {cur / req:2.2%} complete.")
return result
@cache
def add(x, y):
return compute(f"{x} + {y}", lambda r : r - x == y)
@cache
def sub(x, y):
return compute(f"{x} - {y}", lambda r : r + y == x)
def fib(n):
if n <= 1:
return n
else:
return add(fib(sub(n, 1)), fib(sub(n, 2)))
try:
n = int(sys.argv[-1])
except ValueError:
n = 10
req = 3 * (n - 1)
result = fib(n)
print(f"fib({n}) = {result}")
|