Lapius

Lapius / prime-factorization.py

Last active 1 month ago

Like 0

Lapius revised this gist 1 month ago · e79353d

1 file changed, 27 insertions

prime-factorization.py (file created)
@@ -0,0 +1,27 @@
1 + while True:
2 + try:
3 + k = int(input("数値を入力してください: "))
4 + break
5 + except ValueError:
6 + print("エラー")
7 +
8 + symbol = 2
9 + output_parts = []
10 +
11 + while k > 1:
12 + count = 0
13 +
14 + while k % symbol == 0:
15 + count += 1
16 + k //= symbol
17 +
18 + if count > 0:
19 + if count > 1:
20 + output_parts.append(f"{symbol}^{count}")
21 + else:
22 + output_parts.append(f"{symbol}")
23 +
24 + symbol += 1
25 +
26 + result_str = "*".join(output_parts)
27 + print("素因数分解:", result_str)