Python displays all primes from 1 to 100

I am trying to print all primes from 1 to 100 using a boolean function.

Below my code works.

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 launch module, nothing is printed in the shell. What have I done 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 result of the program. How to prevent the printing of the last comma?
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,

+5
source share
9 answers

try it

 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

+4
source

How about this, it does the same thing, but instead asks the user to enter:

 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 just want to decide which numbers you enter yourself, then take out this part:

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

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

 for x in range(1,100): 
+2
source

I think the best solution would be to add an array that fixes the comma problem and is faster / optimized. And then, if necessary, you can cross out the values, save them to a 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 about stack overflow # 11619942 print a series of primes in python "

+2
source

A simple way to display a range of primes #

  # 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
source
 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 a short cut ...

+1
source

A couple of different ways to do this.

 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 (strokes)

0
source

A very simple way to do this is 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) 
0
source

Here is an example where a number is checked only for prime numbers, since all non-prime numbers are divided by a prime number ( link to a related question in stackexchange math)

 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) 
0
source
 for Number in range (1, 101): count = 0 for i in range(2, (Number//2 + 1)): if(Number % i == 0): count = count + 1 break if (count == 0 and Number != 1): print(" %d" %Number, end = ' ') 
0
source

All Articles