相关疑难解决方法(0)

寻找素数的程序

我想找到介于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)

.net c# primes sieve-of-eratosthenes

31
推荐指数
8
解决办法
12万
查看次数

阿特金的筛子

我一直在努力学习生成素数的算法,我在维基百科上遇到了阿特金的Sieve.我理解算法的几乎所有部分,除了少数几个.以下是问题:

  1. 下面的三个二次方程如何形成?4x ^ 2 + y ^ 2,3x ^ 2 + y ^ 2和3x ^ 2-y2
  2. 维基百科中的算法讨论模数为60但我不明白在下面的psudocode中使用的方式/位置.
  3. 如何找到这些提醒1,5,7和11?

以下是维基百科的伪代码供参考:

// 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)

language-agnostic algorithm math primes sieve-of-atkin

18
推荐指数
2
解决办法
3560
查看次数

计算C#素数的最快方法?

我实际上有一个问题的答案,但它没有并行化,所以我对改进算法的方法很感兴趣.无论如何,它对某些人来说可能是有用的.

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)

.net c# algorithm performance bitarray

10
推荐指数
1
解决办法
8456
查看次数

C#:Atkin筛选的实施

我想知道是否有人在这里有一个很好的实施他们想要分享的阿特金筛选.

我正在尝试实现它,但不能完全包围它.这是我到目前为止所拥有的.

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)

c# algorithm primes sieve-of-atkin

7
推荐指数
2
解决办法
4895
查看次数

C#:如何使阿特金筛增量

我不知道这是否可能,但我只是想问一下。我的数学和算法技能有点让我失望: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)

c# algorithm primes sieve sieve-of-atkin

3
推荐指数
1
解决办法
3276
查看次数