python求素数重复
时间: 2024-06-10 12:03:30 浏览: 66
在 Python 中,求素数并检查数字中重复的素因子可以使用一些算法和数据结构来实现。以下是一个简单的示例,展示了如何找出一个范围内所有数字的素因子及其重复次数:
```python
def prime_factors(n):
factors = {}
i = 2
while i * i <= n:
if n % i:
i += 1
else:
n //= i
factors[i] = factors.get(i, 0) + 1
if n > 1:
factors[n] = factors.get(n, 0) + 1
return factors
def find_repeated_primes(start, end):
unique_factors = set()
repeated_factors = {}
for num in range(start, end + 1):
factors = prime_factors(num)
for factor, count in factors.items():
unique_factors.add(factor)
if factor in repeated_factors:
repeated_factors[factor] += count
else:
repeated_factors[factor] = count
return repeated_factors, unique_factors
# 示例使用
start = 10
end = 50
repeated_factors, unique_factors = find_repeated_primes(start, end)
print("重复的素因子:", repeated_factors)
print("独特的素因子:", unique_factors)
阅读全文