查找素数
/// <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();
本文来自博客园,作者:技术宅的化身,转载请注明原文链接:https://www.cnblogs.com/Zyj12/p/14977647.html