如何生成正好有七个约数的数?
对于家庭作业,我需要逻辑来找到从 1 到 1000 的一系列数字,其中恰好有 7 个约数。
(理想情况下,可以轻松修改代码以生成素数。)
For a homework assignment, I need the logic to find a series of numbers from 1 to 1000 which have exactly seven divisors.
(Ideally, the code could be easily modified to generate prime numbers.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
取一个质数
p
。计算p^6
。它唯一的除数是:1
、p
、p^2
、p^3
、...、p^6
。Take a prime number
p
. Calculatep^6
. Its only divisors will be:1
,p
,p^2
,p^3
, ...,p^6
.经过因式分解的数字
将有
除数(请参阅维基百科中的除数函数)。这说明
n
可能只有一个素因数,而这个素因数必须是6的次方。所以取任意素数的6次方。A number with the factorisation
will have
divisors (see divisor function in Wikipedia). This shows
n
may only have one prime factor, and this prime factor must be raised to the power of 6. So take the sixth power of an arbitrary prime number.逻辑是该数字既是完美的平方又是完美的立方。
您必须知道质因数形式为 N=N1^a * N2^b 的数字;
其中 N1 和 N2 是质数,具有 a*b 因数或约数。
因此,对于 7 个因数,数字必须采用 N=a^6 的形式,其中 a 是质数 。
例如 2^6 (64) 、 3^6 (729) 。
编辑:有了这个逻辑,快速生成数字会变得非常容易。您可以轻松生成完美的平方和完美的立方<1000。并检查两个列表中的常见数字。
The Logic would be that the number would be both perfect Square and Perfect cube.
You must be Knowing that a number which has prime factor form as N=N1^a * N2^b;
Where N1 and N2 are prime numbers has a*b factors or divisors.
Thus for 7 factors number must be of the form N=a^6 where a is a prime number .
for e.g 2^6 (64) , 3^6 (729).
EDIT: With this logic it would be quite easier to generate numbers quicly .You can easily generate perfect squares and perfect cube <1000.And check both the lists for common numbers.
您需要从
for n = 1 到 1000
的循环(for 循环),并在该循环内使用另一个循环for m = 1 to n
测试 ifn/ m = 整数(无余数)
如果是则递增 div 计数器。在第二个循环结束时,检查 div 计数器是否为 7(如果将数字写在屏幕上)。
编辑:对于素数,div 计数器必须为 2!
You need loop (for loop) from
for n = 1 to 1000
and inside this loop another loopfor m = 1 to n
inside this loop test ifn/m = integer (no remainder)
and if it is increment the div counter.At the end of second loop check if div counter is 7 if it is write the number on the screen.
EDIT: for prime numbers the div counter must be 2!