Most efficient code for the first 10000 prime numbers?

前端 未结 30 1400
日久生厌
日久生厌 2020-11-29 19:09

I want to print the first 10000 prime numbers. Can anyone give me the most efficient code for this? Clarifications:

  1. It does not matter if your code is ineffici
相关标签:
30条回答
  • 2020-11-29 19:28

    Here the code that I made :


    enter code here
    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    
    int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT*/   
    
    unsigned long int n;
    
    int prime(unsigned long int);
    
    scanf("%ld",&n);
    
    unsigned long int val;
    
    for(unsigned long int i=0;i<n;i++)
    {
        int flag=0;
    
        scanf("%ld",&val);
    
        flag=prime(val);
    
        if(flag==1)
            printf("yes\n");
    
        else
            printf("no\n");
    }
    
    return 0;
    
    }
    
    int prime(unsigned long int n)
    {
    
    if(n==2) return 1;
    
    else if (n == 1||n%2==0)  return 0;
    
    for (unsigned long int i=3; i<=sqrt(n); i+=2)
        if (n%i == 0)
            return 0;
    
    return 1;
    }
    
    0 讨论(0)
  • 2020-11-29 19:28

    Using the Array.prototype.find() method in Javascript. 2214.486 ms

    function isPrime (number) {
    
      function prime(element) {
        let start = 2;
        while (start <= Math.sqrt(element)) {
          if (element % start++ < 1) {
            return false;
          }
        }
        return element > 1;
      }
    
      return [number].find(prime)
    
    }
    
    function logPrimes (n) {
    
      let count = 0
      let nth = n
    
      let i = 0
      while (count < nth) {
        if (isPrime(i)) {
          count++
          console.log('i', i) //NOTE: If this line is ommited time to find 10,000th prime is 121.157ms
          if (count === nth) {
            console.log('while i', i)
            console.log('count', count)
          }
        }
        i++
      }
    
    }
    
    console.time(logPrimes)
    
    logPrimes(10000)
    
    console.timeEnd(logPrimes) // 2214.486ms
    
    0 讨论(0)
  • 2020-11-29 19:29

    This isn't strictly against the hardcoding restriction, but comes terribly close. Why not programatically download this list and print it out, instead?

    http://primes.utm.edu/lists/small/10000.txt

    0 讨论(0)
  • 2020-11-29 19:29

    GateKiller, how about adding a break to that if in the foreach loop? That would speed up things a lot because if like 6 is divisible by 2 you don't need to check with 3 and 5. (I'd vote your solution up anyway if I had enough reputation :-) ...)

    ArrayList primeNumbers = new ArrayList();
    
    for(int i = 2; primeNumbers.Count < 10000; i++) {
        bool divisible = false;
    
        foreach(int number in primeNumbers) {
            if(i % number == 0) {
                divisible = true;
                break;
            }
        }
    
        if(divisible == false) {
            primeNumbers.Add(i);
            Console.Write(i + " ");
        }
    }
    
    0 讨论(0)
  • 2020-11-29 19:29

    In Python

    import gmpy
    p=1
    for i in range(10000):
        p=gmpy.next_prime(p)
        print p 
    
    0 讨论(0)
  • 2020-11-29 19:30

    I have adapted code found on the CodeProject to create the following:

    ArrayList primeNumbers = new ArrayList();
    
    for(int i = 2; primeNumbers.Count < 10000; i++) {
        bool divisible = false;
    
        foreach(int number in primeNumbers) {
            if(i % number == 0) {
                divisible = true;
            }
        }
    
        if(divisible == false) {
            primeNumbers.Add(i);
            Console.Write(i + " ");
        }
    }
    

    Testing this on my ASP.NET Server took the rountine about 1 minute to run.

    0 讨论(0)
提交回复
热议问题