Python displays all of the prime numbers from 1 through 100

Question:

I’m trying to print the all of the prime numbers from 1 through 100 by using Boolean function.

Below is my code that is working.

for n in range(1,101):
status = True
if n < 2:
    status = False
else:
    for i in range(2,n):
        if n % i == 0:
            status = False

    if status:
        print(n, '', sep=',', end='')

But when I put the code in the function and run module, there’s nothing print on the shell.
What did I do wrong?

is_prime():
    for n in range(1,101):
        status = True
        if n < 2:
            status = False
        else:
            for i in range(2,n):
                if n % i == 0:
                    status = False
        return status

if is_prime():    
    print(n, '', sep=',', end='')

Below is the output of the program.
How do I prevent the last comma from printing?
2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,

Asked By: user2210599

||

Answers:

try this

def is_prime(n):
    status = True
    if n < 2:
        status = False
    else:
        for i in range(2,n):
            if n % i == 0:
                status = False
    return status

for n in range(1,101):
    if is_prime(n):
        if n==97:
            print n
        else:
            print n,",",

output is
2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 , 41 , 43 , 47 , 53 , 59 , 61 , 67 , 71 , 73 , 79 , 83 , 89 , 97

Answered By: Mahdi-bagvand

How about this it accomplishes the same thing but instead asks the user for the input:

num1 = input("Input a number: ")
num2 = input("Input another number: ")


for x in range(num1,num2):
    prime = True
    for i in range(2,x):
        if (x%i==0):
            prime = False
    if prime == True:
       print x

print "Done......"

And if you want to just solve for the numbers you input your self then take out this part:

num1 = input("Input a number: ")
num2 = input("Input another number: ")

And change the range from num1,num2 too 1 and 100 for example:

for x in range(1,100):
Answered By: user2589636

I think your best solution would be to append an array, which fixes the comma issue and is faster/more optimized. And then if needed you could strip out the values, store them to file, whathaveyou. Good luck!

def get_primes(start, end):
    out = list()
    if start <= 1:
        start = 2

    sieve = [True] * (end + 1)
    for p in range(start, end + 1):
        if (sieve[p]):
            out.append(p)
            for i in range(p, end + 1, p):
                sieve[i] = False
    return out

print(get_primes(1, 100))

Output:

[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

Please see my comment and others on stack overflow #11619942 “print series of prime numbers in python

Answered By: jacktrader

Simple way to display range of prime Number #

  # Program to display prime number till n nubers

def prime(number):
    for num in range(2,number):
        status = True
        for i in range(2,num):
            if num % i == 0:
                status = False
        if status:
            print(num)


prime(101)

print "Program Ends here"
Answered By: Kuldeep Choughule
n=int(input())
for i in range(1,int(n)):
    for j in range(2,(i+1)):
        if i%j==0:
            if i==j:
                print(i)
            break

This is the short way …

Answered By: iku

A couple of different ways to do it

primes = [x for x in range(2,100) if(all(x % j for j in range(2, x)))]

primes = []
for x in range(2, 101):
    if(False not in (x % j for j in range(2, x))):
        primes.append(x)

primes = []
for x in range(2, 101):
    flag = True
    for j in range(2, x):
        if(not x % j):
            flag = False
    if(flag):
        primes.append(x)

print(primes)

Answered By: Wheat

Very simple way to do it as follows:

def prime(n):
    p = True
    for i in range(2,n):
        if (n%i == 0):
            p = False
    return p

for j in range(2,201):
    if prime(j):
        print (j)
Answered By: Aditya Kumar Singh

Here’s an example where the number is only checked against prime numbers since all non prime numbers are divisible by a prime number (link to related question in math stackexchange)

prime_nums = []

for num in range(2,101):
    isPrime = True
    for prime in prime_nums:
        isPrime = not (num % prime == 0)
        if prime * 2 >= num or not isPrime:
            break
    if isPrime:
        prime_nums.append(num)
print(prime_nums)
Answered By: senel34
def ex1():
    count = 3

    while count < 101:
        isPrime = True

        for i in range(2, int(math.sqrt(count))+1):
            if count % i == 0:
                isPrime = False
        if isPrime:
            print(count, end=' ')
        count += 1
ex1()
Answered By: Xinxin Zhang
num = 101
primes = [True] * num
primes[:2] = [False, False]
for i in range(4, num, 2):
    primes[i] = False
pn = []
for i in range(3, num, 2):
    if primes[i]:
        for j in range(2 * i, num, i):
            primes[j] = False
        pn.append(i)
print(pn)
Answered By: Samad Samadov

Hope this helps

for a in range(1, 101):
    prime = True
    for b in range(2, a):
        if a % b == 0:
            prime = False
        if prime == True:
            print(a)
            break
Answered By: Benjamin Franco
prime_list = []
for number in range(2, 100 + 1):
    is_prime = True
    for i in range(2, number):
        if number % i == 0:
            is_prime = False
            break

        if is_prime == True:
            prime_list.append(number)
print(prime_list)
Answered By: dosytres
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.