相关疑难解决方法(0)

F#中的Eratosthenes筛

我对纯粹功能性F#中的eratosthenes筛子的实施很感兴趣.我对实际筛子的实现感兴趣,而不是真正的筛子的天真功能实现,所以不是这样的:

let rec PseudoSieve list =
    match list with
    | hd::tl -> hd :: (PseudoSieve <| List.filter (fun x -> x % hd <> 0) tl)
    | [] -> []
Run Code Online (Sandbox Code Playgroud)

上面的第二个链接简要描述了一个需要使用多图的算法,据我所知,这在F#中是不可用的.给出的Haskell实现使用了一个支持insertWith方法的映射,我在F#功能映射中没有看到它.

有没有人知道将给定的Haskell映射代码转换为F#的方法,或者可能知道替代实现方法或筛选算法哪些有效且更适合功能实现或F#?

algorithm f# sieve-of-eratosthenes

35
推荐指数
5
解决办法
4799
查看次数

寻找素数的程序

我想找到介于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万
查看次数

Siekin of Atkin解释

我正在做一个项目,我需要一种有效的方法来计算素数.我使用了Eratosthenes筛子,但是我一直在寻找并发现Atkin筛子是一种更有效的方法.我发现很难找到这种方法的解释(我能够理解!).它是如何工作的?示例代码(最好是在C或python中)很棒.

编辑:感谢您的帮助,我唯一不理解的是x和y变量在伪代码中引用的内容.有人可以帮我解释一下吗?

primes sieve-of-eratosthenes sieve-of-atkin

21
推荐指数
2
解决办法
2万
查看次数

阿特金的筛子

我一直在努力学习生成素数的算法,我在维基百科上遇到了阿特金的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#: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
查看次数

Atkin的分段筛可能吗?

我知道可以实施Eratosthenes筛,以便在没有上限(分段筛)的情况下连续找到质数.

我的问题是,阿特金/伯恩斯坦的筛子能否以同样的方式实施?

相关问题:C#:如何制作Atkin增量扫描

然而,相关问题只有一个答案,即"所有筛子都不可能",这显然是不正确的.

algorithm sieve sieve-of-eratosthenes sieve-of-atkin

5
推荐指数
1
解决办法
946
查看次数

如何使用多线程C#实现Sieve Of Eratosthenes?

我正在尝试使用Mutithreading实现Sieve Of Eratosthenes.这是我的实现:

using System;
using System.Collections.Generic;
using System.Threading;

namespace Sieve_Of_Eratosthenes 
{
    class Controller 
        {
        public static int upperLimit = 1000000000;
        public static bool[] primeArray = new bool[upperLimit];

        static void Main(string[] args) 
        {
        DateTime startTime = DateTime.Now;

        Initialize initial1 = new Initialize(0, 249999999);
        Initialize initial2 = new Initialize(250000000, 499999999);
        Initialize initial3 = new Initialize(500000000, 749999999);
        Initialize initial4 = new Initialize(750000000, 999999999);

        initial1.thread.Join();
        initial2.thread.Join();
        initial3.thread.Join();
        initial4.thread.Join();

        int sqrtLimit = (int)Math.Sqrt(upperLimit);

        Sieve sieve1 = new Sieve(249999999);
        Sieve sieve2 = new Sieve(499999999);
        Sieve …
Run Code Online (Sandbox Code Playgroud)

c# multithreading

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

当在F#中生成Primes时,为什么"Erosthenes的筛子"在这个特定的实现中如此缓慢?

IE浏览器,

我在这做错了什么?是否必须使用列表,序列和数组以及限制的工作方式?

所以这是设置:我正在尝试生成一些素数.我看到有十亿个素数的十亿个文本文件.问题不是为什么......问题是这些人如何在这篇文章中使用python计算1,000,000毫秒以下的所有素数......以及我对以下F#代码做错了什么?

let sieve_primes2 top_number = 
    let numbers = [ for i in 2 .. top_number do yield i ]
    let sieve (n:int list) = 
        match n with
        | [x] -> x,[]
        | hd :: tl -> hd, List.choose(fun x -> if x%hd = 0 then None else Some(x)) tl
        | _ -> failwith "Pernicious list error."
    let rec sieve_prime (p:int list) (n:int list) =  
        match (sieve n) with
        | i,[] -> i::p
        | i,n'  -> sieve_prime …
Run Code Online (Sandbox Code Playgroud)

f#

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