Python 3.4 MyProgrammingLab 1. Define a function is_prime that receives an integ
ID: 672707 • Letter: P
Question
Python 3.4
MyProgrammingLab
1. Define a function is_prime that receives an integer argument and returns true if the argument is a prime number and otherwise returns false. (An integer is prime if it is greater than 1 and cannot be divided evenly [with no remainder] other than by itself and one. For example, 15 is not prime because it can be divided by 3 or 5 with no remainder. 13 is prime because only 1 and 13 divide it with no remainder.) This function may be written with a for loop, a while loop or using recursion.
2. Assume the availability of a function is_prime. Assume a variable n has been associated with positive integer. Write the statements needed to compute the sum of the first n prime numbers. The sum should be associated with the variable total. Note: is_prime takes an integer as a parameter and returns True if and only if that integer is prime.
3. Assume the availability of a function is_prime. Assume a variable n has been associated with positive integer. Write the statements needed to find out how many prime numbers (starting with 2 and going in increasing order with successively higher primes [2,3,5,7,11,13,...]) can be added before exceeding n. Associate this number with the variable k.
Explanation / Answer
1) working python code
def is_prime(num):
checker = 0
prime_check = 0
while checker<num:
checker += 1
if num % checker == 0:
prime_check += 1
if prime_check == 2:
return True
else:
return False
num=raw_input("enter the number")
is_prime(num)
2)
printing sum of prime numbers for between 1 and 100
num=1
sum=0
while num<100:
num += 1
if is_prime(num):
sum += num
print sum
3)
same logic can be used to find numbe of prime numbers as well.
num=1
count=0
while num<100:
num += 1
if is_prime(num):
count++
print sum
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.