Project Euler 357
Project Euler 357
题目
Prime generating integers
Consider the divisors of \(30: 1,2,3,5,6,10,15,30\). It can be seen that for every divisor \(d\) of \(30\), \(d+30/d\) is prime.
Find the sum of all positive integers \(n\) not exceeding \(100 000 000\) such that for every divisor \(d\) of \(n\), \(d+n/d\) is prime.
解决方案
如果一个数\(n\)满足题目要求,那么\(n\)一定满足以下条件。
- \(n+1\)一定是一个质数(因为每个数都有因子\(1\),有\(n+1=\dfrac{n}{1}+1\))
- \(n\)是一个无平方因子数,如果存在一个质因子\(p\),使得\(p^2\mid n\),那么\(p+\left(\dfrac{n}{p}\right)=p\cdot\left(1+\dfrac{n}{p^2}\right)\),那么此时的\(p+\dfrac{n}{p}\)不是质数。
对于这些满足条件的数,直接枚举它们的因数,判断答案合法性即可。
代码
1 |
|