Answer to Question #50999 in Python for Will

Question #50999
Given a positive integer n , assign True to is_prime if n has no factors other than 1 and itself. (Remember, m is a factor of n if m divides n evenly.)
1
Expert's answer
2015-02-27T05:10:42-0500
#!/usr/bin/python
import math
# Question #50999, Programming, Python
# Given a positive integer n, assign True to is_prime
# if n has no factors other than 1 and itself.
# (Remember, m is a factor of n if m divides n evenly.)
n = input("Enter a number: ")
# The method of checking whether a number is prime or not.
# Divide n by all numbers from 2 to square_root(n).
# If n is not divisible by neither of those numbers, then n is a prime number
# Othervise n is not a prime number
# compute square root of n, remove fractional part to get an integer number
sqrtn = int(math.sqrt(n))
is_prime = True # set initial value for is_prime
for i in range(2,sqrtn+1):
if (n % i) == 0: # check if n is divisible by i
is_prime = False # change value of is_prime
break # exit loop
if is_prime:
print n, "is a prime number"
else:
print n, "is not a prime number"


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS