Prime Sieve Method--spoj problem 2 prime Generator

Source: Internet
Author: User

Prime number is also known as prime numbers, except 1 and itself, cannot be divided into other natural numbers, in other words, the number except 1 and itself no longer have other factors; otherwise called composite. The smallest prime number is 2.

To determine whether an integer N is a prime number is simple, see if it can be divisible by an integer between 2 and sqrt (n).

def isprime (n):    if n%2==0:        return False    for i in Xrange (3,int (MATH.SQRT (n) +1), 2):        if n%i==0:            Return False    return True

But to find out all the prime numbers between 1 and N, one decision is clearly not a good idea. Since composite can be decomposed into a series of prime numbers, the composite between 1 and N is a multiple of a prime number between 1 and sqrt (n), excluding these composite, and the remainder being prime numbers:

Import Mathimport timeitdef findprime (n):    a=[true]* (n+1)    a[0]=false    a[1]=false for    i in Xrange (2,int (MATH.SQRT (n) +1)):        if a[i]:            k=i*i while            k<=n:                a[k]=false                k=k+i      if __name__== ' __ Main__ ':    T=timeit. Timer (' Findprime (2000000) ', ' from __main__ import findprime ')    print T.timeit (1)

The algorithm starts from 2 to determine whether it is prime and excludes multiples of prime numbers, and when 2 to I are judged, it is clear that i+1 is prime.

Spoj problem 2 Prime Generator requires finding prime numbers between N and M, of which 1 <= m <= n <= 1000000000, n-m<=100000.

In this case, a 1000000000-length sequence would be a waste of space, and a prime number between 1 and sqrt (n) would need to be found first, then the multiples of these primes between N and M would be excluded:

Import Mathdef findprime (n):    a=[true]* (n+1)    a[0]=false    a[1]=false for    i in Xrange (2,int (MATH.SQRT (n ) +1):        if a[i]:            k=i*i while            k<=n:                a[k]=false k=k+i for i in    xrange (2,n+1):        if A[i]:            yield idef findprimebyseed (n,m):    if n==1:        n=2    seed=findprime (int (math.sqrt (m)))    alist=[ 1]* (m-n+1) for    Prime in seed:           if prime<n:            k= (prime-n%prime)%prime        else:            k=2*prime-n While        k<=m-n:            alist[k]=false                        k+=prime for    i in Xrange (m-n+1):        if alist[i]:            Print I +n                if __name__== ' __main__ ':    line=int (Raw_input ()) for    I on Xrange (line):        n,m=raw_input (). Split ()        findprimebyseed (int (n), int (m))        print

  

  

Prime Sieve Method--spoj problem 2 prime Generator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.