68 758
Assignments Done
100%
Successfully Done
In January 2019

# 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.)
#!/usr/bin/python
import math
# Question #50999, Programming, Python &nbsp;
# 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(&quot;Enter a number: &quot;)
# The method of checking whether a number is prime or not.&nbsp;
# 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&nbsp;
# 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 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# set initial value for is_prime
for i in range(2,sqrtn+1):
&nbsp; &nbsp; if (n % i) == 0: &nbsp; &nbsp; &nbsp; &nbsp; # check if n is divisible by i
&nbsp; &nbsp; &nbsp; &nbsp; is_prime = False &nbsp; &nbsp; # change value of is_prime&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; break &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# exit loop
if is_prime:
&nbsp; &nbsp; print n, &quot;is a prime number&quot;
else:
&nbsp; &nbsp; print n, &quot;is not a prime number&quot;
﻿

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!