Most efficient code for the first 10000 prime numbers?

前端 未结 30 1404
日久生厌
日久生厌 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:26

    I have written this using python, as I just started learning it, and it works perfectly fine. The 10,000th prime generate by this code as same as mentioned in http://primes.utm.edu/lists/small/10000.txt. To check if n is prime or not, divide n by the numbers from 2 to sqrt(n). If any of this range of number perfectly divides n then it's not prime.

    import math
    print ("You want prime till which number??")
    a = input()
    a = int(a)
    x = 0
    x = int(x)
    count = 1
    print("2 is prime number")
    for c in range(3,a+1):
        b = math.sqrt(c)
        b = int(b)
        x = 0
        for b in range(2,b+1):
            e  = c % b
            e = int(e)
            if (e == 0):
                x = x+1
        if (x == 0):
            print("%d is prime number" % c)
            count = count + 1
    print("Total number of prime till %d is %d" % (a,count))
    

提交回复
热议问题