Ad

as all the numbers are formed of combinations of prime numbers the best efficient method to check if a number is a prime or not is by checking if it is a product of any of the prime numbers such avoding unviable checks by other numbers

Code
Diff
  • def pri(n,p):#a simple function to loop over your primes 
        for i in p:
            if(n%i==0):return False
        return True 
    def Number_of_Primes(n):
        prm,x=[2,3],5 #start with your intial prime numbers
        while(len(prm)<n): #loop over all odd numbers until your reach your wanted number of primes
            #check if this number is divisible by any of the former prime numbers
            if(pri(x,prm)):prm.append(x)
            x=x+2
        return prm
    
    • def pri(n,p):#a simple function to loop over your primes
    • for i in p:
    • if(n%i==0):return False
    • return True
    • def Number_of_Primes(n):
    • primes=[]
    • x=2
    • if n<0 or type(n)!=int:
    • return "Invalid Input"
    • else:
    • while len(primes)<n:
    • if x==2 or x==3 or x==5 or x==7 or x%2!=0 and x%3!=0 and x%5!=0 and x%7!=0:
    • primes.append(x)
    • x+=1
    • return primes
    • prm,x=[2,3],5 #start with your intial prime numbers
    • while(len(prm)<n): #loop over all odd numbers until your reach your wanted number of primes
    • #check if this number is divisible by any of the former prime numbers
    • if(pri(x,prm)):prm.append(x)
    • x=x+2
    • return prm