Python program to print prime numbers - python

I have started learning python and I am stuck in this code
num=int(input("Enter the number"))
for i in range(2, num):
if num%i==0:
print("Not a prime number")
break
print("Prime Number")
When I enter the number which are not prime, then I am getting output as not a prime number but when I am entering the number that is prime I am getting the output as
Prime number
Prime number
Prime number
Prime number
Prime number

You should only print it once, for example like this:
num = int(input("Enter the number"))
for i in range(2, num):
if num % i == 0:
print("Not a prime number")
break
else:
print("Prime Number")

The loop should check for all numbers and only then print not prime for example like this.
num=int(input("Enter the number"))
for i in range(2, num):
if num%i == 0:
print("Not a prime number")
break
if i > num/2:
print("Prime Number")
break
I would suggest you debug by including print statements that would help a beginner.

check this out :
num=int(input("Enter the number"))
for i in range(2, num//2):
if (num % i) == 0:
print(num, "is not a prime number")
break
else:
print(num, "is a prime number")`

The last statement should be managed carefully in this code. Due to this it print in loop and take care of the tabs you shift.

Related

Python program which returns the properties of a number by choosing a random number in a given range? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last month.
Improve this question
I want to create a function in which system takes a random number from the given range and tell us about it's properties like odd-even number, prime-composite number, positive-negative number. It would be good if it's in Python3
I have although created function but somehow its showing some errors during compiling
import random
A = eval(input("Enter the Range from: "))
B = eval(input("Enter the Range to: "))
num = random.randint(A,B)
if num>=1:
print(num,"is positive number")
if num%2==0:
print(num,"is even number")
else:
print(num,"is odd number")
if num >= 1:
for i in range(2,num+1):
if num%2i==0:
print(num,"is a composite number")
break
else:
print(num,"is a prime number")
elif num==0 or num==1:
print(num,"is neither prime nor composite")
else:
print(num,"is a prime number")
elif num==0:
print(num,"is neither positive nor negative")
print(num,"is neither even nor odd")
print(num,"is neither prime nor composite")
else:
print(num,"is negative number")
try this:
import random
A = int(input("Enter the Range from: "))
B = int(input("Enter the Range to: "))
def number_properties(start, end):
# Generate a random number from the given range
number = random.randint(start, end)
# Check if the number is odd or even
if number % 2 == 0:
odd_even = "even"
else:
odd_even = "odd"
# Check if the number is prime or composite
if number < 2:
prime_composite = "neither prime nor composite"
elif number == 2:
prime_composite = "prime"
else:
for i in range(2, number):
if number % i == 0:
prime_composite = "composite"
break
else:
prime_composite = "prime"
# Check if the number is positive or negative
if number < 0:
positive_negative = "negative"
elif number == 0:
positive_negative = "neither positive nor negative"
else:
positive_negative = "positive"
# Print the properties of the number
print(f"The number {number} is {odd_even}, {prime_composite}, and {positive_negative}.")
number_properties(A, B)

How can I apply a condition that check if the integers are equal in the 2nd while loop?

I can't figure out how to execute a condition that check if the user inputs are equal before checking if it is a prime number. My goal is to ask a user to input two prime numbers. And in the 2nd while loop, I want to make sure that the 2nd number is not equal to the first number in order to be checked if it is a prime number
value_P=[]
value_Q=[]
def is_prime(num):
if num == 2:
return True
if num < 2 or num % 2 == 0:
return False
for n in range(3, int(num**0.5)+2, 2):
if num % n == 0:
return False
return True
while True:
try:
P = int(input("Enter a prime number(P): "))
if is_prime(P):
value_P.append(P)
print("P =", value_P)
break;
else:
print(value_P, "is not a prime number")
except ValueError:
print("Provide an integer value...")
continue
#2nd while loop
while True:
try:
Q = int(input("Enter a prime number(Q). Not the same as the number you entered above: "))
if is_prime(Q):
value_Q.append(Q)
print("Q =", Q)
break;
else:
print(value_Q, "is not a prime number")
except ValueError:
print("Provide an integer value...")
continue
You can check if the value of Q is inside the P list using an if statement in the 2nd while loop.
while True:
try:
Q = int(input("Enter a prime number(Q). Not the same as the number you entered above: "))
if is_prime(Q):
#here the code made sure it's prime, next statement checks if the "Q" input was already previously used as the "P" input. If it is, the loop breaks.
if Q in value_P:
break
else:
value_Q.append(Q)
print("Q =", Q)
break;
else:
print(value_Q, "is not a prime number")
except ValueError:
print("Provide an integer value...")
continue

Python - prime number algorithm doesn't work with higher numbers

I´m new at python and I have problem with my simple program.
I have found some simple algorithm that can tell if input number is prime number or not. Evrything works fine with input numbers like 2 or 13. The problem happens when I use higher number (and I need to use higher numbers only).
num = 3231817448941
if num > 1:
for i in range(2,num):
if (num % i) == 0:
print(num,"is not a prime number")
print(i,"times",num//i,"is",num)
break
else:
print(num, "is a prime number")
else:
print(num,"is not a prime number")
My number input is 3231817448941. Because this number is prime number it should print:
Output should be:
3231817448941 is a prime number
But after runing this program my concole is empty and nothing is printed.
Also when I use similar number with same length that is not prime number then it works.
As I said I'm new at python and I would appreciate any advice.
One change to speed up the process A LOT
num = 3231817448941
if num > 1:
for i in range(2,int(num**0.5)+1): # Dont have to check to n, just sqrt(n)
if (num % i) == 0:
print(num,"is not a prime number")
print(i,"times",num//i,"is",num)
break
else:
print(num, "is a prime number")
else:
print(num,"is not a prime number")
You can find more optimizations here

How to read input again in a for loop?

I'm a freshman of Python, and now meet a problem I can't understand. How to recall an input outside of a for loop?
Please some master give me an answer using the following example. Tks!
n=int(input('Please input a number: ')
for i in range(2,n):
if n%i==0:
print('It is not a prime number!', end=' ')
break
else:
print('%d is a prime number!' % n)
Just put another loop around your code. e.g.
while True:
n=int(input('Please input a number: '))
for i in range(2,n):
if n%i==0:
print('It is not a prime number!', end=' ')
else:
print('%d is a prime number!' % n)
break
Is this what you are looking for?
while True:
n=int(input('Please input a number: ')
flag = True
for i in range(2,n):
if n%i==0:
flag = False
break
if flag == True:
print('%d is a prime number!' % n)
break
else:
print('It is not a prime number!', end=' ')

Writing a program to do my math homework for me but its not working

One part of my program displays n amount of prime numbers depending on the user input but no matter what i input it only prints "1"
def listPrimeNumbers():
print("List Prime Numbers")
print("------------------")
print("Enter how many prime numbers you want displayed")
print("Type in '0' to go back to the Main Menu")
print("\n"*10)
amountOfNumbers = int(input("Amount of Numbers --> "))
print("\n"*10)
for i in range(1, amountOfNumbers):
prime = True
for i in range(2,i):
if (num%i==0):
prime = False
if prime:
print(i)
print("\n"*10)
print("Type '0' to try again and '1' to go to the main menu")
print("\n"*10)
choice = int(input("Choice ---> "))
if choice == 0:
print("\n"*100)
listPrimeNumbers()
elif choice == 1:
print("\n"*100)
main()
Change your 'for' loop to this:
for num in range(1, amountOfNumbers):
prime = True
for i in range(2,num):
if (num%i==0):
prime = False
if prime:
print(num)
num wasn't defined anywhere in your code, I think this is what you meant.
How are you calling your code? As is, I couldn't run it. I managed to run it by adding a call to function listPrimeNumbers() at the bottom.
Anyway, assuming the code you've posted is your entire code, this is the version that worked for me:
import sys
def listPrimeNumbers():
print("List Prime Numbers")
print("------------------")
print("Enter how many prime numbers you want displayed")
print("Type in '0' to go back to the Main Menu")
print("\n"*10)
amountOfNumbers = int(input("Amount of Numbers --> "))
print("\n"*10)
for num in range(1, amountOfNumbers):
prime = True
for i in range(2,num):
if (num%i==0):
prime = False
if prime:
print(num)
print("\n"*10)
print("Type '0' to try again and '1' to go to the main menu")
print("\n"*10)
choice = int(input("Choice ---> "))
if choice == 0:
print("\n"*100)
listPrimeNumbers()
elif choice == 1:
print("\n"*100)
print 'Bye'
sys.exit(0)
listPrimeNumbers()
If I understand correctly the question, you have a logic flaw in the code since, even with the correction Alex suggested (which works), you just print all the numbers that are prime up to the value the user input and not, as seems to be the question, the quantity the user imput.
For example, given your question, if the user input 10, I understant that you should print the first 10 prime numbers (1,2,3,5,7,11,13,17,19,23) and not the primes up to 10 (1,2,3,5,7)
If my assumption is right, the code should be something like:
import math
def listPrimeNumbers(n):
l = int(math.sqrt(n))+1
if n == 1:
return True
for x in range(2, l):
if (n%x==0):
return False
return True
print("List Prime Numbers")
print("------------------")
print("Enter how many prime numbers you want displayed")
print("Type in '0' to go back to the Main Menu")
print("\n"*10)
amountOfNumbers = int(input("Amount of Numbers --> "))
counter = 0
n = 0
while (counter < amountOfNumbers):
n += 1
if listPrimeNumbers(n) == True:
counter += 1
print(n)
I omitted the part to reiterate the process, so you need to re-run the program to give another try

Categories