63 074
Assignments Done
99,1%
Successfully Done
In July 2018

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.)
Expert's answer
#!/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 first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions