查找素数

/// <summary>
/// 筛选素数
/// </summary>
public void GenPrimes()
{
    for (int outer = 2; outer <= arr.GetUpperBound(0); outer++)
    {
        for (int inner = outer + 1; inner <= arr.GetUpperBound(0); inner++)
        {
            if (arr[inner] == 1)
            {
                if (inner % outer == 0)
                {
                    arr[inner] = 0;
                }
            }
        }
    }
}

public void ShowPrimes()
{
    for (int i = 2; i < arr.GetUpperBound(0); i++)
    {
        if (arr[i] == 1)
        {
            Console.Write(i + " ");
        }
    }
}
int size = 100;
var cArray = new CArray(size);
for (int i = 0; i <= size - 1; i++)
{
    cArray.Insert(1);
}

cArray.GenPrimes();
Console.WriteLine("显示1~100的素数:");
cArray.ShowPrimes();

 

posted @ 2021-07-06 16:43  技术宅的化身  阅读(46)  评论(0编辑  收藏  举报