FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
vcs/_4.python/common/python03_algorithm.py at master · bunshue/vcs · GitHub
bunshue
/
vcs
Public
Notifications
You must be signed in to change notification settings
Fork
3
Star
3
Code
Issues
0
Pull requests
4
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
vcs
/
_4.python
/
common
/
python03_algorithm.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
127 lines (82 loc) · 2.79 KB
Breadcrumbs
vcs
/
_4.python
/
common
/
python03_algorithm.py
Copy path
File metadata and controls
127 lines (82 loc) · 2.79 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
"""
各種演算法
"""
print
(
"------------------------------------------------------------"
)
# 60個
def
bubbleSort
(
list
):
needNextPass
=
True
k
=
1
while
k
<
len
(
list
)
and
needNextPass
:
# List may be sorted and next pass not needed
needNextPass
=
False
for
i
in
range
(
len
(
list
)
-
k
):
if
list
[
i
]
>
list
[
i
+
1
]:
# swap list[i] with list[i + 1]
temp
=
list
[
i
]
list
[
i
]
=
list
[
i
+
1
]
list
[
i
+
1
]
=
temp
needNextPass
=
True
# Next pass still needed
list
=
[
2
,
3
,
2
,
5
,
6
,
1
,
-
2
,
3
,
14
,
12
]
bubbleSort
(
list
)
for
v
in
list
:
print
(
v
)
print
(
"------------------------------------------------------------"
)
# 60個
def
Fibonacci
(
n
):
# 写出从第0项到第n项的Fibonacci系列
a
,
b
,
i
=
0
,
1
,
0
while
i
<=
n
:
print
(
a
,
end
=
" "
)
a
,
b
,
i
=
b
,
a
+
b
,
i
+
1
print
()
print
(
Fibonacci
(
10
))
print
(
"------------------------------------------------------------"
)
# 60個
# n = int(input("請輸入大於 1 的整數:"))
n
=
1723
if
n
==
2
:
print
(
"2 是質數!"
)
else
:
for
i
in
range
(
2
,
n
):
if
n
%
i
==
0
:
print
(
"%d 不是質數!"
%
n
)
break
else
:
print
(
"%d 是質數!"
%
n
)
print
(
"------------------------------------------------------------"
)
# 60個
def
gcd
(
n1
,
n2
):
g
=
1
# 最初化最大公約數
n
=
2
# 從2開始檢測
while
n
<=
n1
and
n
<=
n2
:
if
n1
%
n
==
0
and
n2
%
n
==
0
:
g
=
n
# 新最大公約數
n
+=
1
return
g
n1
,
n2
=
1233
,
2477
print
(
"最大公約數是 : "
,
gcd
(
n1
,
n2
))
print
(
"------------------------------------------------------------"
)
# 60個
def
gcd
(
a
,
b
):
if
a
<
b
:
a
,
b
=
b
,
a
while
b
!=
0
:
tmp
=
a
%
b
a
=
b
b
=
tmp
return
a
a
,
b
=
1233
,
2477
print
(
"最大公約數是 : "
,
gcd
(
a
,
b
))
print
(
"------------------------------------------------------------"
)
# 60個
def
gcd
(
a
,
b
):
return
a
if
b
==
0
else
gcd
(
b
,
a
%
b
)
a
,
b
=
1233
,
2477
print
(
"最大公約數是 : "
,
gcd
(
a
,
b
))
print
(
"------------------------------------------------------------"
)
# 60個
def
gcd
(
a
,
b
):
return
a
if
b
==
0
else
gcd
(
b
,
a
%
b
)
def
lcm
(
a
,
b
):
return
a
*
b
//
gcd
(
a
,
b
)
a
,
b
=
1233
,
2477
print
(
"最大公約數是 : "
,
gcd
(
a
,
b
))
print
(
"最小公倍數是 : "
,
lcm
(
a
,
b
))
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"作業完成"
)
print
(
"------------------------------------------------------------"
)
# 60個
Back
|
FazBrowse Home
|
New Git URL