1 Replies - 3173 Views - Last Post: 16 January 2015 - 09:55 PM

#1 Brewer   User is offline

  • Awesome
  • member icon

Reputation: 182
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Find All Primes Between X and Y

Posted 06 October 2010 - 08:26 AM

Description: This snippet contains a function that will find all prime numbers between two integers, x and y, which are input by the user.
x = input("X: ")
y = input("Y: ")

def isPrime(n):
    if n < 2: return False
    for i in range(2,int(n * 0.5) + 1):
        if n % i == 0: return False
    return True

for i in range(int(x),int(y) + 1):
    if isPrime(i): print(i)

Is This A Good Question/Topic? 0
  • +

Replies To: Find All Primes Between X and Y

#2 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11632
  • View blog
  • Posts: 19,762
  • Joined: 19-March 11

Re: Find All Primes Between X and Y

Posted 16 January 2015 - 09:55 PM

Note that the upper bound for the loop only needs to check up to sqrt(n). Proof: if i * j = n, then at least one of i,j <= sqrt(n)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1