blob: 6eea537ff966560fa8b470547722fb2753903bac (
plain)
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
|
#!/opt/local/bin/python
#
# See ../README.md
#
#
# Run as: python ch-1a.py
#
#
# Initialize the cache
#
cache = {0: 0, 1: 1}
max = 50
#
# Fusc sequence is defined as:
# ( n, 0 <= n <= 1
# fusc (n) = { fusc (n / 2), n > 1 && n even
# ( fusc ((n - 1) / 2) + fusc ((n + 1) / 2), n > 1 && n odd
#
def fusc (n):
if n not in cache:
if n % 2 == 1:
cache [n] = fusc ((n - 1) / 2) + fusc ((n + 1) / 2)
else:
cache [n] = fusc (n / 2)
return cache [n]
#
# Calculate the first results of the fucs series, and print them
#
for i in range (max):
if i > 0:
print (' ', end = "")
print (fusc (i), end = "")
print ("")
|