gpt4 book ai didi

c# - “素数求和”花费的时间太长

转载 作者:行者123 更新时间:2023-12-04 03:43:40 24 4
gpt4 key购买 nike

<分区>

我不知道为什么这对于大数来说要花很长时间 我正在尝试解决欧拉计划 (https://projecteuler.net/problem=10) 中的问题 10。有人可以帮帮我吗?

它找到第一个质数并与它的所有因子相交,然后移动到下一个质数,依此类推。

long sum=0;
int howmanyChecked = 1;
int target = 1000000;
int index = -1;
List<int> numbers = new List<int>(target);
List<bool> Isprime = new List<bool>(target);
for(int i=2;i<=target;i++)
{
numbers.Add(i);
Isprime.Add(true);
}
while (1 > 0)
{
index = Isprime.IndexOf(true, index + 1);

int Selected = numbers[index];
howmanyChecked++;


sum += Selected;
//Console.WriteLine($"selected prime number is {Selected}");
//int startfrom =numbers.IndexOf(Selected * Selected);
if (Selected >= target / 2)
{
Console.WriteLine("ss");
for(int i=index+1;i<target-1;i++)

{
if(Isprime[i]==true)
{
Console.WriteLine(numbers[i].ToString());
sum += numbers[i];
}
}
Console.WriteLine($"the sum of all prime nubers below {target} is {sum} tap to continue");
Console.ReadLine();
break;


}
else
{
for (int i = Selected; i * Selected <= target; i++)
{
int k = numbers.IndexOf(i * Selected);
if (k == -1)
break;
if (Isprime[k] == true)
{
Isprime[numbers.IndexOf(i * Selected)] = false;
howmanyChecked++;
//Console.WriteLine($"Checked number is {Selected * i} and we have counted {howmanyChecked} numbers");
}



}
}
if (howmanyChecked == target || index==target)
break;

}


Console.ReadLine();

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com