生成具有上限的素数 [英] Generate prime numbers with upper limit
问题描述
我正在尝试生成一个上限内的素数列表,例如。 100:所以它应该从0生成100以下的所有素数。
我遵循Sieve of Eratosthenes算法,但不能在代码中实现它。多次尝试。
我要做的是:
I am trying to generate a list of prime numbers within a upper limit , eg. 100: so it should generate all the prime numbers below 100 from 0.
I was following Sieve of Eratosthenes algorithm , but cant implement it in code. Tried many times.
What i am trying to do is:
假设所有数字的列表从2到N.
设p最初等于2.第一个素数。
从列表中删除小于或等于N的所有倍数。
找到p后面的下一个数字,该数字尚未在列表中划掉。这是下一个素数。
对新p重复步骤3和4,除非p2大于N.
列表中没有划掉的数字是下面的素数limit N。
Assume list of all numbers from 2 to N.
Let p initially equal to 2. The first prime.
Strike all multiples of p from the list less than or equal to N.
Find the next number after p, that’s not yet crossed out on the list. This is the next prime.
Repeat step 3 and 4 for the new p, unless p2 is greater than N.
Numbers not crossed out on the list are primes below the limit N.
我可以不使用数组做到这一点,或者我必须使用数组肯定。
还有任何其他方法来生成Prime数字(即使检测它们,我会在以后自己工作)。谢谢
Can i do this without using an array, or do i have to use an array for sure.
Also is there any other method to generate Prime numbers( even to detect them , i would work on later myself). Thanks
推荐答案
算法:
Algorithm:
创建从2到N的所有数字的列表。
[...]
Create list of all numbers from 2 to N.
[...]
问题:
Question:
我可以在不使用数组的情况下执行此操作,还是必须使用数组。
Can i do this without using an array, or do i have to use an array for sure.
答案: 不,你不能。
[update]
当然,使用阵列可以找到0到100之间的素数。对于每个候选人 n
只测试 2
和 n $之间是否有数字c $ c> (整数)平方根,除以它
[/ update]
Answer: "No, you cannot".
[update]
Finding the prime numbers between 0 and 100 wihtout using an array is possible, of course. For each candidate n
just test if there is a number between 2
and n
(integer) square root, dividing it
[/update]
我们不要在这里做作业。你说你已经尝试了很多次,但是没有在这里发布你的代码,以便我们帮助解决一个特定的问题。
说了这个链接以获得一些建议
http://stackoverflow.com/questions/14211628/listing-prime-numbers-up-to-2-billion-using-sieves-method-in-c [ ^ ]
Google提供的这些结果还有很多其他建议。我已将完整链接保留在视图中,以便您可以查看搜索条件是什么
https://www.google.co.uk/#q=sieve+of+eratosthenes+prime+数字+上涨+至+ 100 + C& safe =有效 [ ^ ]
We don't do homework here. You say you have tried many times but have not posted your code here for us to help with a specific problem.
Having said that look at this link for some suggestions
http://stackoverflow.com/questions/14211628/listing-prime-numbers-up-to-2-billion-using-sieves-method-in-c[^]
And there are many many more suggestions with these results from google. I've left the full link in view so you can see what the search criteria were
https://www.google.co.uk/#q=sieve+of+eratosthenes+prime+numbers+up+to+100+C&safe=active[^]
这篇关于生成具有上限的素数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!