Reputation: 83
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,
Upvotes: 6
Views: 94169
Reputation: 25
Source code prime numbers:
a=0
i=3
print(2)
while(i<100):
a=0
for x in range (2,i):
if(i%x ==0):
a=1
break
if(a==0)
print(i)
i=i+1
Upvotes: 0
Reputation: 1
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
Upvotes: 0
Reputation: 1
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)
Upvotes: 0
Reputation: 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()
Upvotes: -1
Reputation: 16
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)
Upvotes: 0
Reputation: 31
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)
Upvotes: 0
Reputation: 11
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)
Upvotes: 1
Reputation: 1064
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 ...
Upvotes: 1
Reputation: 39
# 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"
Upvotes: 1
Reputation: 1407
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
Upvotes: 6
Reputation: 689
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"
Upvotes: 2
Reputation: 31
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):
Upvotes: 3