Question

Using nested loops, write a function called primes(a,b) that takes in two positive integers a and...

Using nested loops, write a function called primes(a,b) that takes in two positive integers a and b (where a<b). Then simply RETURNS a string containing all the prime numbers between a and b (or if there are none, the string "No Primes"). You should check that a and b are valid inputs, that is, that a and b are integers such that a<b (otherwise, the function should print “No Primes”). Three sample calls of your function (in IDLE) should produce the following results (please be sure to format your output exactly as shown and please note that we are not printing them into terminal):

>>> primes(12,72)
'Primes: 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71.'
>>> primes(3,10)
'Primes: 3, 5, 7.'
>>> primes(10,4)
'No Primes'
>>> primes(8,10)
'No Primes'

Homework Answers

Answer #1

Explanation:

Here is the function primes which takes two integers a and b

And returns the string of prime numbers between a and b

Code:

def primes(a, b):
  
res = []
  
if(a>=b):
return 'No Primes'
  
for i in range(a, b+1):
  
is_prime = 1
  
for j in range(2, i):
if(i%j==0):
is_prime=0
break
  
if(is_prime==1):
res.append(i)
  
if(len(res)==0):
return 'No Primes'
else:
s = ''
  
for i in range(len(res)):
s = s + str(res[i])
if(i!=len(res)-1):
s = s+ ', '
else:
s = s+'.'
return s


print(primes(12, 72))
print(primes(3, 10))
print(primes(10, 4))
print(primes(8, 10))

Output:

PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!
PLEASE COMMENT IF YOU NEED ANY HELP!

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
MATLAB: Write a function called matrix_problem1 that takes a matrix A of positive integers as its...
MATLAB: Write a function called matrix_problem1 that takes a matrix A of positive integers as its sole input. If the assumption is wrong, the function returns an empty matrix. Otherwise, the function doubles every odd element of A and returns the resulting matrix. Notice that the output matrix will have all even elements. For example, the call B = matrix_problem([1 4; 5 2; 3 1], will make B equal to [2 4; 10 2; 6 2]. The function should work...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
In this assignment you will write a program that compares the relative strengths of two earthquakes,...
In this assignment you will write a program that compares the relative strengths of two earthquakes, given their magnitudes using the moment magnitude scale. Earthquakes The amount of energy released during an earthquake -- corresponding to the amount of shaking -- is measured using the "moment magnitude scale". We can compare the relative strength of two earthquakes given the magnitudes m1 and m2 using this formula: f=10^1.5(m1−m2) If m1>m2, the resulting value f tells us how many times stronger m1...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT