blob: 1bea5c0d1c1ad5ef519b77328eaef42e82855ee9 (
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
|
#!/usr/bin/python3
# Challenge 013
#
# Challenge #2
# Write a script to demonstrate Mutually Recursive methods. Two methods are
# mutually recursive if the first method calls the second and the second calls
# first in turn. Using the mutually recursive methods, generate Hofstadter
# Female and Male sequences.
#
# F ( 0 ) = 1 ; M ( 0 ) = 0
# F ( n ) = n - M ( F ( n - 1 ) ) , n > 0
# M ( n ) = n - F ( M ( n - 1 ) ) , n > 0.
import sys
def F(n):
if n==0:
return 1
else:
return n-M(F(n-1))
def M(n):
if n==0:
return 0
else:
return n-F(M(n-1))
N = int(sys.argv[1])
print("F: "+", ".join([str(F(x)) for x in range(N)])+", ...")
print("M: "+", ".join([str(M(x)) for x in range(N)])+", ...")
|