Question

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'

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!**

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...

Please do it in Python
Write the simplest program that will demonstrate iteration vs
recursion using the following guidelines -
Write two primary helper functions - one iterative
(IsArrayPrimeIter) and one recursive (IsArrayPrimeRecur) - each of
which
Take the array and its size as input params and return a
bool.
Print out a message "Entering <function_name>" as the
first statement of each function.
Perform the code to test whether every element of the array is
a Prime number.
Print out...

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, 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

Get Answers For Free

Most questions answered within 1 hours.

ADVERTISEMENT

asked 11 minutes ago

asked 20 minutes ago

asked 29 minutes ago

asked 42 minutes ago

asked 1 hour ago

asked 1 hour ago

asked 1 hour ago

asked 1 hour ago

asked 3 hours ago

asked 3 hours ago

asked 3 hours ago

asked 3 hours ago