Blue Bridge Cup questions basic exercises Python implementation decomposition of the quality factor

Problem Description

Determined interval [a, b] for all integers prime factor decomposition.

Input Format

Input two integers a, b.

Output Format

Each output line of a number of decomposition, the form A1 = K A2 A3 ... (A1 <= A2 <A3 = ..., K is small to large) (see Specific examples)

Sample input

3 10

Sample Output

3=3
4=22
5=5
6=2
3
7=7
8=222
9=33
10=2
5

prompt

First screened out all prime numbers, and then break down.

Scale data and conventions

2<=a<=b<=10000

My code

def find(n): #写一个函数来直接输出n的表达式
    s = str(n) + '='
    i = 2
    while i <= n: # 当i<n的时候就说明i可能是n的因子
        if n%i == 0: #i是因子的时候就加到末尾
            s = s + str(i) + '*'
            n = n//i
            i -= 1 #再次判断i是不是n的因子
        i += 1 #遍历i
    return s[:-1] #最后一个乘号不要
m,n = map(eval,input().split())
for i in range(m,n+1): #逐个输出m到n的表达式
    print(find(i))
Published 15 original articles · won praise 5 · Views 596

Guess you like

Origin blog.csdn.net/weixin_44389971/article/details/104817266