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"

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"

``

Learn more about our help with Assignments: Python

## Comments

## Leave a comment