Prime Numbers On Python

Prime Numbers On Python



Hello Every one welcome...
Today i show you How to calculate prime number upto N th number
Or How many prime numbers upto N th number

Here make simple code
If you like support us
and also you find easier compare to it
Post your code

Okay Friends.,..


CODE 1:

# Prime numbers
a = int(input('Enter needed prime number range: '))
prime = []
for i in range(2,a+1):
    for j in range(2,i+1):
        if i % j == 0:
            if i == j:
                prime.append(i)
                break
            else:
                break

print(prime)
print(len(prime)) 


------


CODE 2:

# This another method using generators

a = int(input('Enter needed prime number range: '))
def genr(a):
    for i in range(2,a+1):
        for j in range(2,i+1):
            if i % j == 0:
                if i == j:
                    yield i
                    break
                else:
                    break

kar = genr(a)
count = 0
for i in kar:
    print(i,end=' ')
    count += 1

print(f'\nTotal number of prime numbers {count}')


-----

Note: When using generators functions run time is minimum compared to Code1 .






By
PyKAR


Comments

Popular posts from this blog

Hardy - Ramanujan Number (Magic Number) Program in Python

FLAMES CODE ON PYTHON

Count Swap for Sorting in Python