The following code aims to check whether a number is prime and find the total number...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
The following code aims to check whether a number is prime and find the total number of prime numbers less than 10,000. However, the output is 9999 which is wrong. Could you please explain why the output is 9999? Could you please revise and optimize this code? Also explain precisely why you have done these revisions. Optimizing the code means to avoid the unnecessary steps or the codes can write a more concise way. yes str("This is a prime number.") nostr("This is not a prime number.") def prime (number): for divisor in range (2, number // 2 + 1): if number divisor == 0: return yes return no def prime_10k (): count = 0 n = 10000 for x in range (1, n): if prime (x): count = count+1 print ("The number of prime numbers under", n, "is", count) prime 10k () [15 marks] The following code aims to check whether a number is prime and find the total number of prime numbers less than 10,000. However, the output is 9999 which is wrong. Could you please explain why the output is 9999? Could you please revise and optimize this code? Also explain precisely why you have done these revisions. Optimizing the code means to avoid the unnecessary steps or the codes can write a more concise way. yes str("This is a prime number.") nostr("This is not a prime number.") def prime (number): for divisor in range (2, number // 2 + 1): if number divisor == 0: return yes return no def prime_10k (): count = 0 n = 10000 for x in range (1, n): if prime (x): count = count+1 print ("The number of prime numbers under", n, "is", count) prime 10k () [15 marks]
Expert Answer:
Answer rating: 100% (QA)
The code provided has a few issues that are causing incorrect output Here are the problems and the revised code 1 Indentation The code block inside th... View the full answer
Related Book For
Introduction to Java Programming, Comprehensive Version
ISBN: 978-0133761313
10th Edition
Authors: Y. Daniel Liang
Posted Date:
Students also viewed these programming questions
-
x Gr.6 Determine the moment of inertia about the centroidal y- axis of the fabricated area shown in figure below. ly =0.7 inch, material is a channel C150x12, A= 2.4 in 1.92" -0.514"
-
: (i) What data structures are maintained by the page manager. (ii) What happens when a machine performs a read operation to a page. (iii) What happens when a machine performs a write operation to a...
-
Miller-Rabin test to check whether a number N is composite. This will involve computing a N1 mod N for some value of a. [10 marks] Carry out the steps for N = 65 and a = 1, 2, 8 and 12. on what each...
-
As you might imagine, the chart of accounts for a manufacturing firm would be different from that of a service firm. Not surprisingly, service firms differ so much that software now exists for almost...
-
Olaf Gundersen, the CEO of Wireless Telecom Company, is in a quandary. Last year he accepted the Maximum Megahertz Project suggested by six up-and coming young R&D corporate stars. Although Olaf did...
-
What is the initial direction of deflection for the charged particles entering the magnetic fields shown in FIGURE Q29.6? a. b. x x x x x x x x x x x x
-
Part 1. On January 1, 2000, Liberty Co. entered into a 12-year lease on a building. The lease con- rac lequires (1) annual (prepaid) rental payments of $26,400 each January 1 throughout the life of e...
-
Interpreting notes on off-balance-sheet financing. Louisiana-Pacific Corporation (LP) sold certain timber assets and received cash and notes receivable from the purchaser. LP then engaged in a...
-
4) 5 y -3 -5-4-3-2-11 -3- Domain: X 3 Range: 93-2
-
Regulators, in protecting the public interest, are examining Wincox Insurance Company's financial statements. These regulators impose requirements on Wincox's financial statements because of which...
-
In an employment agency, the auditor asked the office manager if, and how, the performance of assigned personnel is monitored or measured during a placement. The manager replied, "Our policy is...
-
How will accounting help you move forward in your accounting career? When determining fair and reasonable pricing, think these factors provide enough parameters. Why or why not?
-
ELO CORP for the month of Oct 2011: Direct Materials used 10,000; Direct labor rate per hour 5.00 ; Overhead cost 2,300.00 ; Direct labor hours employed 500.00 ; Gross Profit rate 25% based on...
-
Design a Python function that accepts a variable number of arguments and returns their sum. Ensure the function can handle both positional and keyword arguments. Provide examples demonstrating its...
-
When the Copper Corporation buys inventory, it must often rely on short-term bank financing to pay for the goods. Bank financing is usually in the form of a short-term self-liquidating loan, where...
-
Describe a selection interview and its primary use. Give an in-depth and situational use for the selection interview process.
-
Refer to the Finance Policy & Procedure Manual, how are invoices and supporting documentation filed? What is the timeframe for retaining invoices and supporting documentation? Your answer should be...
-
Comptech Ltd is a manufacturer of optical equipment. In September 2019, Ed Thompson the Chief Research Officer, attended a conference in Switzerland that focused on optical developments for the 21st...
-
Write a program that prompts the user to enter a file name, then displays the frequency table of the characters in the file and displays the Huffman code for each character.
-
Write a program that enables two users to chat. Implement one user as the server (Figure 31.21a) and the other as the client (Figure 31.21b). The server has two text areas: one for entering text and...
-
Create animation in Figure 16.46b to meet the following requirements: Allow the user to specify the animation speed in a text field. Get the number of iamges and images file-name prefix from the...
-
Explain what impact an increase in debt will have on the of shares.
-
What are the two risks for a shareholder of an indebted company?
-
Why is the cost of equity for a company with no debt equal to the average weighted cost of capital?
Study smarter with the SolutionInn App