列表

详情


NC25066. [USACO 2007 Mar L]Prime Factors

描述

Bessie is attending a new school where the new subject is 'prime factoring'. Bessie's homework has many integers that must be broken into prime factors.
As you know, a prime number is a number greater than 1 that is divisible only by 1 and itself). Prime factors of a number are the primes that, when multiplied together, yield that number. For example, the prime factors of 36 are: 2 2 3 * 3
Bessie is getting tired of performing this operation by hand and needs you to design a program that will read in a value of N (1 < N < 1,000,000,000) and print out, smallest to largest, one per line, all the prime factors of this number (including duplicates if they exist).

输入描述

Line 1: A single integer: N

输出描述

Line 1: All the prime factors of N, one per line, in ascending order

示例1

输入:

3423

输出:

3
7
163

说明:

This is the number to be broken down into its prime factors.
3 7 163 = 3423; 3, 7, and 163 are primes.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Python3 解法, 执行用时: 43ms, 内存消耗: 4580K, 提交时间: 2023-08-15 09:55:57

N = int(input())

# 分解质因数
def getFactors(n):
    r = []
    for k in range(2, int(n ** 0.5) + 1):
        while n != k:
            if n % k == 0:
                r.append(k)
                n /= k
            else:
                break
    r.append(int(n))
    return r

l = getFactors(N)
for n in l:
    print(n)

上一题