Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
44 lines (35 loc) · 1.25 KB

File metadata and controls

44 lines (35 loc) · 1.25 KB
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
# -*- coding:utf-8 -*-
__author__ = 'gjw'
__time__ = '2018/1/5 0005 上午 10:48'
# 题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。
"""
程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:
(1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。
(2)如果n<>k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数你n,重复执行第一步。
(3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。
"""
def reduceNum(n):
if not isinstance(n, int) or n<=0:
print("您输入的数字有误")
exit(0)
else:
l = []
r = n
if n == 1:
print(1, end="")
while r not in [1]:
for i in range(2, int(n/2)+1):
if r%i == 0:
r /= i
l.append(i)
break
if r == n:
l = [1, r]
r = 1
l = sorted(l)
for i, v in enumerate(l):
if i == 0:
print(str(n)+" = " + str(v), end="")
else:
print(" * " + str(v), end="")
reduceNum(64)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.