One question per day - the number of prime factors (the real question of the 13th Provincial Competition of the Lanqiao Cup in 2022)

How to decompose a positive integer into prime factors: a daily question - decompose a positive integer into prime factors_Ben Xiaogu's Blog-CSDN Blog

Title description: Given a positive integer n, how many prime numbers are divisors of n.

Input format: The first line of input contains an integer n.

Output format: output an integer, representing the number of prime divisors of n.

Sample input:

396

Sample output:

3

hint:

396 has 2, 3, 11 three prime divisors.

import math


def is_prime(factor_list):
    global list
    list = []
    for num in factor_list:
        if num == 1:
            pass
        elif num == 2:
            # 2是质数,可以直接输出
            list.append(num)
        else:
            for i in range(2, num):
                if num % i == 0:
                    # 如果能被2 ~ n-1的数整除,则不是质数
                    break
            else:
                list.append(num)


def get_factor(n):
    global flist
    flist = []
    for i in range(1, n):
        if n % i == 0:
            flist.append(i)


n = int(input())
get_factor(n)
is_prime(flist)
num = len(list)
print(n, "的素因数个数:", num)

 

 

Guess you like

Origin blog.csdn.net/weixin_51995147/article/details/126882248