forked from yidao620c/core-algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathm01_math.py
More file actions
100 lines (80 loc) · 2.12 KB
/
m01_math.py
File metadata and controls
100 lines (80 loc) · 2.12 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# 最大公约数,公倍数,素因子分解,闰年判断,找零钱,斐波那契数列
"""
基本的初等数学类
最大公约数,公倍数,素因子分解,闰年判断,找零钱,斐波那契数列
"""
__author__ = 'Xiong Neng'
# 闰年判断
def isLeapYear(year):
return (not year % 4 and year % 100) or (not year % 400)
# 找零钱
def mod(money):
cent = int(money * 100)
all_cent = {25: 0, 10: 0, 5: 0, 1: 0}
for k in all_cent:
all_cent[k], cent = divmod(cent, k)
return all_cent
# 最大公约数,辗转相除法
def maxCommonDivisor(m, n):
while True:
remainder = m % n
if not remainder:
return n
else:
m, n = n, remainder
# 最小公倍数
def minCommonMultiple(m, n):
return m * n / maxCommonDivisor(m, n)
# 素数的判断
def isprime(n):
result = True
for i in range(n / 2, 1, -1):
if n % i == 0:
result = False
break
return result
# 获取n的所有因子
def getfactors(n):
result = [n]
for i in range(n / 2, 0, -1):
if n % i == 0:
result.append(i)
return result
# 素因子分解
def decompose(n):
all_factors = getfactors(n)
all_factors.remove(1)
all_factors.remove(n)
prime_factors = [x for x in all_factors if isprime(x)]
prime_factors.sort(reverse=True)
result = []
remainder = n
for f in prime_factors:
while remainder >= f:
qut, rem = divmod(remainder, f)
if rem != 0:
break
else:
remainder = qut
result.append(f)
return result
# 获取前N个斐波那契数列
def fibonacci(n):
result = []
if n == 1:
result.append(1)
elif n >= 2:
result.append(1)
result.append(1)
for i in range(2, n):
result.append(result[-1] + result[-2])
return result
def main():
print(fibonacci(8))
mod(0.78)
print(maxCommonDivisor(24, 36))
print(minCommonMultiple(24, 36))
if __name__ == '__main__':
main()