-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibonacci.py
More file actions
84 lines (82 loc) · 1.92 KB
/
Fibonacci.py
File metadata and controls
84 lines (82 loc) · 1.92 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 19 15:51:11 2020
@author: zsl
"""
"""
(1)给定正整数 n,确定 n 是否是它的所
有因子之和。比如 n=6 时,它的所有因子分别为 1,2,3,由于
6=1+2+3,所以 6 满足条件.
要求:
输入:正整数 n ;
输出:若 n 是它的所有因子之和,输出 YES ,否则,输出 NO
请给出至少两种方法,并 分析 对比它们的时间复杂度
"""
n = eval(input('输入正整数n:'))
#方法一
print('方法一')
factors = []
for i in range(n-1):
# print(i+1)
j = i+1
if n%j == 0:
factors.append(j)
if sum(factors)==n:
print('YES')
else:
print('NO')
print('时间复杂度是:o(n)')
#方法二
print('方法二')
sums = 0
for i in range(n-1):
j = i+1
if n%j == 0:
sums = sums + j
if sums > n:
break
if sums==n:
print('YES')
else:
print('NO')
print('时间复杂度是:o(n)')
"""
(2)编写程序计算斐波那契数列之和:1,2,2,3,5,8,13,21,34
要求:
使用递归方法实现以上算法;
对比递归算法的实现过程,从时间复杂度和空间复杂度上 考虑
是否可以改进递归算法
"""
#递归法
import numpy as np
import time
time_start=time.time()
def F(n):
if n==1:
return 1
elif n==2:
return 1
else:
return F(n-1)+F(n-2)
def sum_F(n):
tmp = []
for i in range(n):
j=i+1
tmp.append(F(j))
return np.array(tmp).sum()
m = eval(input('Input N:'))
print(m,"的斐波那契数列之和:",sum_F(m))
time_end=time.time()
print('递归花费时间',time_end-time_start)
#非递归法
time_start=time.time()
n = int(input("Input N: "))
a = 1
b = 1
sum = 0
for i in range(n):
sum += a
a, b = b, a + b
print(n,"的斐波那契数列之和:", sum)
time_end=time.time()
print('非递归花费时间:',time_end-time_start)