我想找到介于0和长变量之间的素数,但我无法获得任何输出.
该计划是
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication16
{
class Program
{
void prime_num(long num)
{
bool isPrime = true;
for (int i = 0; i <= num; i++)
{
for (int j = 2; j <= num; j++)
{
if (i != j && i % j == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
Console.WriteLine ( "Prime:" + i );
}
isPrime = true;
}
}
static void Main(string[] args) …Run Code Online (Sandbox Code Playgroud) 我一直在努力学习生成素数的算法,我在维基百科上遇到了阿特金的Sieve.我理解算法的几乎所有部分,除了少数几个.以下是问题:
以下是维基百科的伪代码供参考:
// arbitrary search limit
limit ? 1000000
// initialize the sieve
for i in [5, limit]: is_prime(i) ? false
// put in candidate primes:
// integers which have an odd number of
// representations by certain quadratic forms
for (x, y) in [1, ?limit] × [1, ?limit]:
n ? 4x²+y²
if (n ? limit) and (n mod 12 = 1 or …Run Code Online (Sandbox Code Playgroud) 我实际上有一个问题的答案,但它没有并行化,所以我对改进算法的方法很感兴趣.无论如何,它对某些人来说可能是有用的.
int Until = 20000000;
BitArray PrimeBits = new BitArray(Until, true);
/*
* Sieve of Eratosthenes
* PrimeBits is a simple BitArray where all bit is an integer
* and we mark composite numbers as false
*/
PrimeBits.Set(0, false); // You don't actually need this, just
PrimeBits.Set(1, false); // remindig you that 2 is the smallest prime
for (int P = 2; P < (int)Math.Sqrt(Until) + 1; P++)
if (PrimeBits.Get(P))
// These are going to be the multiples of P …Run Code Online (Sandbox Code Playgroud) 我想知道是否有人在这里有一个很好的实施他们想要分享的阿特金筛选.
我正在尝试实现它,但不能完全包围它.这是我到目前为止所拥有的.
public class Atkin : IEnumerable<ulong>
{
private readonly List<ulong> primes;
private readonly ulong limit;
public Atkin(ulong limit)
{
this.limit = limit;
primes = new List<ulong>();
}
private void FindPrimes()
{
var isPrime = new bool[limit + 1];
var sqrt = Math.Sqrt(limit);
for (ulong x = 1; x <= sqrt; x++)
for (ulong y = 1; y <= sqrt; y++)
{
var n = 4*x*x + y*y;
if (n <= limit && (n % 12 == 1 || n …Run Code Online (Sandbox Code Playgroud) 我不知道这是否可能,但我只是想问一下。我的数学和算法技能有点让我失望:P
问题是我现在有这个类可以生成达到一定限制的素数:
public class Atkin : IEnumerable<ulong>
{
private readonly List<ulong> primes;
private readonly ulong limit;
public Atkin(ulong limit)
{
this.limit = limit;
primes = new List<ulong>();
}
private void FindPrimes()
{
var isPrime = new bool[limit + 1];
var sqrt = Math.Sqrt(limit);
for (ulong x = 1; x <= sqrt; x++)
for (ulong y = 1; y <= sqrt; y++)
{
var n = 4*x*x + y*y;
if (n <= limit && (n % 12 == 1 || n …Run Code Online (Sandbox Code Playgroud)