用筛子查找素数?使用1000个大小的一维数组,编写一个查找所有素数的程序? [英] Find Primes with a sieve? Using 1000 sized One-Dimensional Array ,write a program that finds all primes ?

查看:90
本文介绍了用筛子查找素数?使用1000个大小的一维数组,编写一个查找所有素数的程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

仔细阅读并在下面提供C#代码

使用1000个大小的一维数组,编写一个查找所有素数的程序.

方向:
2是素数,
从4开始,擦除大于2的2的所有倍数
擦除4,6,8,10,12,14,16,....等.
找到2的下一个数字,该数字不会被删除,
类似地,扫描整个阵列数组中剩余的应该是质数.

在此先感谢

S.Naveen ...

Hi all,

Read it carefully and give the C# code for the below

Using 1000 sized One-Dimensional Array ,write a program that finds all primes.

Directions :
2 is a prime ,
Starting with 4,erase all multiples of 2 larger than 2
Erase 4,6,8,10,12,14,16,....etc.
Find next number to 2 ,that is not erased,
Similarly Scan entire array & remaining in the array should be Prime Numbers.

Thanks in advance

S.Naveen...

推荐答案

只要找到一个列出所有1000以下的质数的网站,然后将它们贴在数组中,素数列表.
Just find a web site that lists all prime numbers under 1000, stick ''em in an array, and there''s your list of prime numbers.


报价:仔细阅读"-好的,做到了.

Quote:在下面提供C#代码"-我认为不是.

如果您没有做自己的工作的技能,那么我建议您上课或读书.如果您太懒了,那么我建议您雇用一个人,工作页面在那里.
Quote: "Read it carefully" - OK, did that.

Quote: "give the C# code for the below" - I don''t think so.

If you do not have the skills to do your own work then I suggest you take a class or read a book. If you are just too lazy then I suggest you employ someone, the jobs page is over there.


long limit,sqrLimit,i,j,n;
            long[] A = new long[1001];

            limit = 1000;
            sqrLimit = Convert.ToInt64(Math.Sqrt(1000));

            for (i = 0; i <= limit; i++)
                A[i] = 0;

                for (i = 2; i <= sqrLimit + 1; i++)
                    for (j = i + i; j <= limit; j = j + i)
                        A[j] = 1;

            n = 0;
            for (i = 2; i <= limit; i++)
                if (A[i] == 0)
                {
                    A[n] = i;
                    n++;
                }


这篇关于用筛子查找素数?使用1000个大小的一维数组,编写一个查找所有素数的程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆