2024年4月5日 星期五

用Python求最大公因收和最小公倍數

 

1.找因數

1
2
3
4
5
6
a=int(input('請輸入大於零的整數:'))
if(a>0):
    for i in range(1,a+1):
        if(a%i==0):
            print(i,end=" ")
    print("")

執行結果:
請輸入大於零的整數:12
1 2 3 4 6 12 

2.利用集合生成式找因數

1
2
3
a=int(input('請輸入大於零的整數:'))
a_set={n for n in range(1,a+1) if a>0 and a%n==0}
print(a_set)

執行結果:
請輸入大於零的整數:9
{1, 3, 9}

3.找出兩數的公因數

1
2
3
4
5
a=int(input('請輸入大於零的整數A:'))
a_set={n for n in range(1,a+1) if a>0 and a%n==0}
b=int(input('請輸入大於的整數B:'))
b_set={n for n in range(1,b+1) if b>0 and b%n==0}
print(a_set & b_set)

執行結果:
請輸入大於零的整數A:12
請輸入大於的零整數B:9
{1, 3}

4.找出兩數的最大公因數

1
2
3
4
5
a=int(input('請輸入大於零的整數A:'))
a_set={n for n in range(1,a+1) if a>0 and a%n==0}
b=int(input('請輸入大於的整數B:'))
b_set={n for n in range(1,b+1) if b>0 and b%n==0}
print(max(a_set & b_set))

執行結果:
請輸入大於零的整數A:12
請輸入大於零的整數B:9
3

5.找出兩數的最小公倍數

1
2
3
4
5
a=int(input('請輸入大於零的整數A:'))
a_set={n for n in range(1,a+1) if a>0 and a%n==0}
b=int(input('請輸入大於的整數B:'))
b_set={n for n in range(1,b+1) if b>0 and b%n==0}
print(a*b//max(a_set & b_set))

執行結果:
請輸入大於零的整數A:12
請輸入大於零的整數B:9
36

6.一次輸入兩個數,找出兩數的最小公倍數

1
2
3
4
5
a,b=int(input('請輸入大於的整數(A, B):'))
print(a,b)
a_set={n for n in range(1,a+1) if a>0 and a%n==0}
b_set={n for n in range(1,b+1) if b>0 and b%n==0}
print(a*b//max(a_set & b_set))

執行結果:
請輸入大於零的整數(A, B):9, 12
Traceback (most recent call last):
  File "C:/Users/cheng-min/AppData/Local/Programs/Python/Python311/test.py", line 1, in <module>
    a,b=int(input('請輸入大於零的整數(A, B):'))
ValueError: invalid literal for int() with base 10: '9, 12'

將int()改成eval()函式:

1
2
3
4
5
a,b=eval(input('請輸入大於零的整數(A, B):'))
print(a,b)
a_set={n for n in range(1,a+1) if a>0 and a%n==0}
b_set={n for n in range(1,b+1) if b>0 and b%n==0}
print(a*b//max(a_set & b_set))

執行結果:
請輸入大於零的整數(A, B):9, 12
9 12
36

7.用自定函式來求最大公因收和最小公倍數

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
def gcd(a, b):
    if b==0:
        return a
    else:
        return gcd(b, a%b)

def lcm(a, b):
    return a*b//gcd(a, b)


a,b=eval(input('請輸入大於零的整數(A, B):'))
print("gcd(",a,",",b,")=", gcd(a,b))
print("lcm(",a,",",b,")=", lcm(a,b))

執行結果:
請輸入大於零的整數(A, B):9, 12
gcd( 9 , 12 )= 3
lcm( 9 , 12 )= 36

8.利用math套件求最大公因收和最小公倍數

1
2
3
4
import math
a,b=eval(input('請輸入大於零的整數(A, B):'))
print("gcd(",a,",",b,")=", math.gcd(a,b))
print("lcm(",a,",",b,")=", math.lcm(a,b))

執行結果:
請輸入大於零的整數(A, B):9,12
gcd( 9 , 12 )= 3
lcm( 9 , 12 )= 36

9.利用sympy套件求最大公因收和最小公倍數

1
2
3
4
from sympy import *
a,b=eval(input('請輸入大於的整數(A, B):'))
print("gcd(",a,",",b,")=", gcd(a,b))
print("lcm(",a,",",b,")=", lcm(a,b))

執行結果:
Traceback (most recent call last):
  File "C:/Users/cheng-min/AppData/Local/Programs/Python/Python311/test.py", line 1, in <module>
    from sympy import *
ModuleNotFoundError: No module named 'sympy'

安裝sympy套件

執行結果:
請輸入大於零的整數(A, B):9, 12
gcd( 9 , 12 )= 3
lcm( 9 , 12 )= 36

10.改用import寫法

1
2
3
4
import sympy
a,b=eval(input('請輸入大於零的整數(A, B):'))
print("gcd(",a,",",b,")=", gcd(a,b))
print("lcm(",a,",",b,")=", lcm(a,b))

執行結果:
Traceback (most recent call last):
  File "C:/Users/cheng-min/AppData/Local/Programs/Python/Python311/test.py", line 3, in <module>
    print("gcd(",a,",",b,")=", gcd(a,b))
NameError: name 'gcd' is not defined

修改程式:

1
2
3
4
import sympy
a,b=eval(input('請輸入大於零的整數(A, B):'))
print("gcd(",a,",",b,")=", sympy.gcd(a,b))
print("lcm(",a,",",b,")=", sympy.lcm(a,b))

執行結果:
請輸入大於零的整數(A, B):9,12
gcd( 9 , 12 )= 3
lcm( 9 , 12 )= 36

為什麼要介紹sympy套件,不妨可以參考夠Python,一行指令找質數

沒有留言:

張貼留言