使用以下代码,我能够找到圆形素数,但有一些意外的值。例如如果'13'是循环素数,那么'31'应该被消除。请建议一种消除此类事件的方法。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PrimeNumberAlgorithm
{
class Program
{
static void Main(string[] args)
{
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
int n = 100;
int check_limit = (Int32)Math.Sqrt(Convert.ToDouble(n));
int count_Circular = 0;
bool[] b_arr = new bool[n - 2];
sw.Start();
for (int i = 0; i < b_arr.Length; i++)
{
b_arr[i] = true;
}
int currentNum = 2;
for (int i = 0; i < check_limit; i++)
{
if (b_arr[i] == true)
{
for (int j =i+currentNum;j < b_arr.Length; j += currentNum)
{
b_arr[j] = false;
}
}
currentNum++;
}
for(int i = 0; i < n-2; i++)
{
if (b_arr[i])
{
int j = i + 2;
if (j != 2 && j != 5)
{
string numStr = j.ToString();
char[] chr_numStr = numStr.ToCharArray();
for (int k = 0; k < chr_numStr.Length; k++)
{
if (chr_numStr[k] % 2 == 0 || chr_numStr[k] == 5)
{
b_arr[i] = false;
break;
}
}
}
}
if (b_arr[i])
{
Console.WriteLine("The circular primes are:\n");
Console.WriteLine(i+2);
count_Circular++;
}
}
sw.Stop();
Console.WriteLine("The total number of circular primes:{0}",count_Circular);
Console.WriteLine("The time taken:{0} sec",sw.ElapsedMilliseconds/(1000));
}
}
}
答案 0 :(得分:1)
我认为你误解了这个问题。再读一遍:
提示:如果数字包含任何数字0,2,4,5,6,8那么它不能是圆形的,除非它是2或5。