Skip to main content

Posts

Showing posts with the label math

Python - Get the Next Prime

🐍  Have the program in Python to find prime numbers until the user chooses to stop asking for the next one. Solution import math def getprimes():     primes = [2]     num = 3          while True:         isprime = True         numsqrt = int(math.sqrt(num)+1)         for i in primes:             if i<=numsqrt:                 if (num%i==0):                     isprime = False                     break             else:                 break         if isprime:             primes.append(num)             yield num         num += 2 if __name__==...