```python
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n-1)
def calculate_series(n):
sum = 0
flag = 1
for i in range(1, n+1):
flag *= -1
product = 1
for j in range(1, 2*i):
product *= j
sum += flag * (1/product)
return sum
print("计算1/1! - 1/3! + 1/5! - 1/7! ... 的结果是:", calculate_series(5))
def find_perfect_numbers(limit):
for num in range(1, limit+1):
divisors_sum = 0
for i in range(1, num):
if num % i == 0:
divisors_sum += i
if divisors_sum == num:
print(num, "是一个完数")
find_perfect_numbers(1000)
def integer_partition(n, i, out):
if n == 0:
print(out)
return
elif n < 0:
return
while i <= n:
integer_partition(n-i, i, out + [i])
i += 1
def combination_helper(n, r, out, start, result):
if r == 0:
result.append(out)
return
for i in range(start, n+1):
combination_helper(n, r-1, out+[i], i+1, result)
def find_combinations(n, r):
result = []
combination_helper(n, r, [], 1, result)
return result
print("1, 2, 3中取2个数的组合是:", find_combinations(3, 2))
```
通过上面的代码,我们实现了四个问题的求解:
1. 通过循环和递归计算了1/1! - 1/3! + 1/5! - 1/7! ... 的结果。
2. 找出了1000以内所有的完数。
3. 实现了对正整数n的划分,输出了所有可能的分划方式。
4. 用循环和递归方式找出了n个自然数1,2,3,……,n中r个数的组合。