Question

Question 1 Assume a function called count_primes is pre-definied. If consumes two int arguments start and...

Question 1

Assume a function called count_primes is pre-definied. If consumes two int arguments start and stop that returns the number of prime integers between start (inclusive) and stop (exclusive). Note: The start value must be less than or equal to the stop value. Enter 3 assertEqual statements to test the count_primes function.

Question 2

Define a function called count_primes that consumes two int arguments start and stop that returns the number of prime integers between start (inclusive) and stop (exclusive). You may assume that start will be less than or equal to stop. You may use the pre-defined function called is_prime which consumes a positive number and returns True if the number is prime and False if the number is not prime.

Python language!

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def is_prime(n):
    if n > 1:
        for i in range(2, n):
            if (n % i) == 0:
                return False
    return n > 1


def count_primes(start, stop):
    count = 0
    for i in range(start, stop):
        if is_prime(i):
            count += 1
    return count


assert count_primes(1, 5) == 2
assert count_primes(2, 6) == 3
assert count_primes(2, 2) == 0
Add a comment
Know the answer?
Add Answer to:
Question 1 Assume a function called count_primes is pre-definied. If consumes two int arguments start and...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • CODE IN PYTHON 17. Use the Design Recipe to define a function called  black_jack  which consumes to 2...

    CODE IN PYTHON 17. Use the Design Recipe to define a function called  black_jack  which consumes to 2 int values greater than 0. Return whichever value is nearest to 21 without going over. Return 0 if they both go over. Include a docstring! Write three assertEqual statements to test your function.

  • A method called linearSearch(), which takes as parameters an array of int followed by three values...

    A method called linearSearch(), which takes as parameters an array of int followed by three values of type int, and returns a value of type int. The first int parameter represents a key, the second int parameter represents a starting position, and the third int parameter represents an end position. If the key occurs in the array between the start position (inclusive) and the end position (exclusive), the method returns the position of the first occurrence of the key in...

  • Write a Python function isPrime(number) that determines if the integer argument number is prime or not....

    Write a Python function isPrime(number) that determines if the integer argument number is prime or not. The function will return a boolean True or False. Next, write a function HowManyPrimes(P), that takes an integer P as argument and returns the number of prime numbers whose value is less than P. And then write a function HighestPrime(K) that takes integer K as an argument and returns the highest prime that is less than or equal to K. USE THE WHILE LOOP...

  • Python 1 Write a function called sum_odd that takes two parameters, then calculates and returns the...

    Python 1 Write a function called sum_odd that takes two parameters, then calculates and returns the sum of the odd numbers between the two given integers. The sum should include the two given integers, if they are odd. You can assume the arguments will always be positive integers, and the first smaller than or equal to the second. 3 To get full credit on this problem, you must define at least 1 function, use at least 1 loop, and use...

  • PYTHON 3 - please show format Question 2. ) Use the Design Recipe to write a...

    PYTHON 3 - please show format Question 2. ) Use the Design Recipe to write a function yesOrNo which has no parameters. When called, it gets input from the user until the user types either 'yes' or 'no', at which point the function should return True if the user typed 'yes' and False if the user typed 'no'. Any other entries by the user are ignored and another value must be input. For example: Test Input Result print(yesOrNo()) hello blank...

  • I am using the program Python Define a function named volume_ideal_gas. It consumes 3 arguments pressure,...

    I am using the program Python Define a function named volume_ideal_gas. It consumes 3 arguments pressure, num_moles and temperature and returns the volume, measured in Liters, of the ideal gas according to the Ideal Gas Law. You may assume that the pressure argument is measured in atm and temperature argument is measured in Kelvin. PV = nRT Where: P is pressure measured in atm V is volume measured in L n is the number of moles R = .0821 atm⋅L...

  • Write a Python function, called counting, that takes two arguments (a string and an integer), and...

    Write a Python function, called counting, that takes two arguments (a string and an integer), and returns the number of digits in the string argument that are not the same as the integer argument. Include a main function that inputs the two values (string and integer) and outputs the result, with appropriate labelling. You are not permitted to use the Python string methods (such as count(), etc.). Sample input/output: Please enter a string of digits: 34598205 Please enter a 1-digit...

  • Python Question: Define the function high_score that consumes a list of integers (representing scores in a...

    Python Question: Define the function high_score that consumes a list of integers (representing scores in a game) and produces an integer representing the highest score in the list. Ignore scores less than 100, and stop processing values if you encounter -999. If the list is empty, return the value None instead. It is up to you to decompose this function (or not) however you want. Here is my code so far: from cisc108 import assert_equal def high_score(scores: [int])->int: max_num =...

  • IN PYHTON CODE Question #3 Produce a function prime_factors.dict that has one integer variable n that...

    IN PYHTON CODE Question #3 Produce a function prime_factors.dict that has one integer variable n that is assumed to be greater than 1. The function will return a dictionary with keys the integers from 2 to n, inclusive, and with values the set of prime factors of each key. So, the command prime_factors.dict (8) will return the dictionary 2 123,3: 3),4 2),5 (53,6 2,3),7:7),8 {2)) Note that even though the prime number 2 appears twice in the prime fac- torization...

  • I need help making my array into a function that can called by the main function...

    I need help making my array into a function that can called by the main function using my program. This is C programming int prime (int x) { int i; i = 2; while (i < x) { if (x % i == 0) return 0; i++; } return 1; } int main (void){ int n; scanf ("%d", &n); if (n < 1) { printf ("Error: at least one data value must be provided.\n"); return 1; } int a[n]; int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT