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:
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
-
: (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...
-
Marketing management is the art and science of choosing target markets and getting, keeping, and growing customers through creating, delivering, and communicating superior customer value. Respond to...
-
Suppose the supply and demand schedules for cell phones are as follows: a. Find equilibrium price and quantity in the cell phone market. b. Find consumer surplus, producer surplus, and total surplus...
-
Consider the multiple regression model fit to the National Football League team performance data in Problem 3.1. Problem 3.1 Consider the National Football League data in Table B.1. a. Construct a...
-
Fred's Freight employs three drivers who are paid $20 per hour for regular time and $30 for overtime. A single pickup and delivery requires, on average, one hour of driver time. Drivers are paid for...
-
Discuss the concepts of mean, median, mode, and standard deviation. Include when each should be used and evaluate the differences of each.
-
1. Using the SBTi tool, explore appropriate interim targets for Energy Australia's Scope 1 and 2 emissions aligned with a 1.5C emissions trajectory. In a graphical format, compare your company's:...
-
Given that the following statement is incorrect, rewrite it correctly: The probability of a baby being born a boy is 5050.
-
Based on a Harris poll, there is a 5050 chance that a randomly selected adult has pierced ears. Express the indicated degree of likelihood as a probability value between 0 and 1.
-
Using the information in this chapter, determine which tax form you should use and outline the types of records you will need. Talk with several taxpayers in different socioeconomic groups or stages...
-
Consider a cash flow and interest profile as shown: The worth of these cash flows at the end of year 3 is a. $5,000. b. $5,504.72. c. $5,994.56. d. $5,440. Year 0 Year 1 Year 2 Year 3 Cash Flow at...
-
Perform the indicated operations, if defined, for the following vectors and matrices. \(\left(\mathbf{A B}^{T} ight) \mathbf{v}\) -2 1 -3 1 1 A = 1 -3 2 1 32 B = V= W = 0 0 4 5
-
For the final Portfolio Project, you will submit a professional presentation(using PowerPoint or a similar format), developing an organizations employee legal handbook that could be utilized during...
-
Describe a group you belong or have belonged discuss the stages of group development and suggest how to improve the group effectiveness by using the group development model.
-
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...
-
An atom loses an electron to another atom. Is this an example of a physical or chemical change? (a) chemical change involving the formation of ions (b) physical change involving the formation of ions...
-
Why are ores so valuable? (a) They are sources of naturally occurring gold. (b) Metals can be efficiently extracted from them. (c) They tend to occur in scenic mountainous regions. (d) They hold many...
-
Aluminum ions carry a 3+ charge, and chloride ions carry a 1- charge. What is the chemical formula for the ionic compound aluminum chloride? (a) Al 3 Cl (b) AlCl 3 (c) Al 3 Cl 3 (d) AlCl
Study smarter with the SolutionInn App