Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use python to slove the following Exercise 5: Prime Exploration (2 pts) Find the sum of all the four-digit primes. That is primes in the

Use python to slove the following

Exercise 5: Prime Exploration (2 pts)

Find the sum of all the four-digit primes. That is primes in the range from 1000 to 9999. Note the smallest prime is 2. The number 1 is not a prime.

Definition: A positive number 2 is a prime, if and only if its only divisors are 1 and n itself.

Open the starter code in primes.py This contains a cool prime detector method named is_prime(n).

Study carefully how this code works.

def is_prime(n):

return (n > 1) and not any(n%i == 0 for i in range(2, n))

You might not be used to the any command. You can roughly think of any and all as series of logical or and and operators, respectively

any : returns True when at least one of the elements is Truthy.

all : returns True only when all the elements are Truthy.

Be sure to paste the sum of all the four-digit primes in the provided answer template and also the code you used to complete the solution() method, which was just a stub at first.

Now find the sum of all the four-digit primes.

foloowing is the starter code:

# Prime Exploration # Here is a fancy helper method to detect if a positive integer n is prime.  def is_prime(n): return (n > 1) and not any(n%i == 0 for i in range(2, n)) # DEMO 1 def demo1(): print(" * * * * * DEMO 1 * * * * * ") # Find all the primes from 1 to 11  print("Here are all the primes less than or equal to 11.") for n in range(1, 12): if is_prime(n): print(n), # DEMO 2 # This next demo uses a list comprehension with a filter to spit out a list of the primes less than or equal to 15. def demo2(): print(" * * * * * DEMO 2 * * * * * ") print("Here are all the primes less than or equal to 15 using a list comprehension.") list1 = range(1000, 9999) # remember range is inclusive/exclusive  primes = [n for n in list1 if is_prime(n) ] for prime in primes: print(prime, end=" ") def solution(): # Find the sum of all the four-digit primes - that is primes in the range from 1000 to 9999.  # Add you new code here.  pass  if __name__ == "__main__": demo1() demo2() # solution()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David Kroenke, David J. Auer

3rd Edition

0131986252, 978-0131986251

More Books

Students also viewed these Databases questions

Question

What is the difference between Needs and GAP Analyses?

Answered: 1 week ago

Question

What are ERP suites? Are HCMSs part of ERPs?

Answered: 1 week ago