6

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,

user2210599
  • 83
  • 1
  • 2
  • 6
  • When the function exits, the variable names inside it will not be accessible from the outside. You should make a `list` of values and `return` it to the outer world – JBernardo Apr 12 '13 at 05:34
  • 1
    To solve the comma problem, use `','.join()` - that's the typical way to create comma-separated values. – Ilmo Euro Apr 12 '13 at 06:00
  • In one line: `','.join(str(x) for x in xrange(2,100) if all(x % n for n in xrange(2,x)))` – wim Apr 12 '13 at 06:06

11 Answers11

6

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

Tshilidzi Mudau
  • 7,373
  • 6
  • 36
  • 49
Mahdi-bagvand
  • 1,396
  • 19
  • 40
  • 1
    For future visitors, a better solution would be something like `','.join(str(x) for x in xrange(1, 101) if is_prime(x))` – DanielB Oct 29 '13 at 04:09
3

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):
2

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"

Community
  • 1
  • 1
jacktrader
  • 588
  • 5
  • 10
1

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"
1
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 ...

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
iku
  • 133
  • 2
  • 9
1

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)
Dino
  • 7,779
  • 12
  • 46
  • 85
0

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)

Wheat
  • 31
  • 6
0

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)
senel34
  • 16
  • 4
0
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)
  • 1
    Welcome to Stack Overflow! While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. –  Apr 27 '22 at 20:10
0

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
S.B
  • 13,077
  • 10
  • 22
  • 49
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Ethan Jun 10 '22 at 01:21
-1
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()
Mia
  • 2,466
  • 22
  • 38