C# 4.0 如何计算所有小于2144的素数之和并打印结果。。?
这是我的代码,但总和没有正确累加,这是一个大数字 我需要使用BigInteger吗 如果是这样的话,那是因为我不知道如何使用BigInteger进行合计C# 4.0 如何计算所有小于2144的素数之和并打印结果。。?,c#-4.0,C# 4.0,这是我的代码,但总和没有正确累加,这是一个大数字 我需要使用BigInteger吗 如果是这样的话,那是因为我不知道如何使用BigInteger进行合计 namespace ConsolePrimeNumbers { public static class PrimeTool { public static bool IsPrime(int candidate) { // Test whether the paramete
namespace ConsolePrimeNumbers
{
public static class PrimeTool
{
public static bool IsPrime(int candidate)
{
// Test whether the parameter is a prime number.
if ((candidate & 1) == 0)
{
if (candidate == 2)
{
return true;
}
else
{
return false;
}
}
// Note:
// ... This version was changed to test the square.
// ... Original version tested against the square root.
// ... Also we exclude 1 at the very end.
for (int i = 3; (i * i) <= candidate; i += 2)
{
if ((candidate % i) == 0)
{
return false;
}
}
return candidate != 1;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("--- Primes between 0 and 214.......");
long total = 0;
for (int i = 0; i < 2144; i++)
{
bool prime = PrimeTool.IsPrime(i);
if (prime)
{
Console.Write("Prime: ");
Console.WriteLine(i);
total = total + i;
}
}
Console.Write("Total of All Prime: ");
Console.WriteLine(total);
}
}
}
namespace consolePrimeNumber
{
公共静态类工具
{
公共静态bool IsPrime(int候选)
{
//测试参数是否为素数。
如果((候选者&1)==0)
{
如果(候选==2)
{
返回true;
}
其他的
{
返回false;
}
}
//注:
//…此版本已更改为测试方形。
//…根据平方根测试原始版本。
//…最后我们排除了1个。
对于(int i=3;(i*i)您的iPrime函数是错误的。循环条件是(i*i)您所说的“未正确累加”是什么意思?您的总和适合长整数,不需要大整数。为什么您认为这不起作用?它打印什么?我对代码进行了更改…对于(int i=3;(i*2)我需要帮助把所有的素数相加,然后打印出总数……我是彼得,你能帮我解决这个问题吗。