Problem 1: Testing for divisibility Suppose I want to know whether a number, x is evenly...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Problem 1: Testing for divisibility Suppose I want to know whether a number, x is evenly divisible by a number y. When I use the phrase "evenly divisible", I mean division without any remainder. So, x is evenly divisible by y if we can divide x by y without leaving any remainder behind (the remainder is equal to 0). Sometimes it is easy to tell whether one number evenly divides another just by looking. If y is 2, then we know that all even numbers (numbers ending with 0, 2, 4, 6, or 8) can be evenly divided by 2 and that all odd numbers (numbers ending with 1, 3, 5, 7, or 9) cannot be evenly divided by 2. The same is true if y is equal to 5. All numbers ending in 5 or 0 can be evenly divided by 5. However, there are many cases of divisibility that are harder to check. Determining whether a number is evenly divisible by 3 or 7 can be difficult to do just by looking, especially for larger numbers. Computer can always do this easily. Recall that the modulus operator, %, is used to return just the remainder from an integer division operation. The expression, x %y, which is read as x modulo y (or x mod y), integer divides x by y and returns just the remainder of the division operation. We can use this to easily determine whether x is evenly divided by y. Ifx % y evaluates to 0, then we can conclude that .x is evenly divisible by y. The file "hw2problem 1.py" contains a Python function called divisible (), which takes one argument, an integer n. Complete the body of the function to do the following: First, create an empty list. Then for each of the integers I through 9, determine whether it evenly divides n. If so, then add True to the list. If not, then add False to the list. Once you have checked whether n is divisible by each of the integers from 1 through 9, return the list of Booleans. For example, suppose the input n is equal to 126. 126 is evenly divisible by 1, 2, 3, 6, 7, and 9. So, I will add True at the first three elements, then two False entries for 4 and 5, then True for 6 and 7, False for 8, and True for 9. The result list can be seen in the table below. I then return the list.. Examples: Function Call divisible (126) divisible (20) divisible (1024) divisible (17) divisible (539 Return Value [True, True, True, False, False, True, True, False, Truel [True, True, False, True, True, False, False, False, False] [True, True, False, True, False, False, False, True, False] [True, False, False, False, False, False, False, False, False] [True, False, False, False, False, False, True, False, False] # #IAE 101 (Fall 2019) # HW 2, Problem 1 def divisible(n): # ADD YOUR CODE HERE return -1 # CHANGE OR REMOVE THIS LINE # DO NOT DELETE THE FOLLOWING LINES OF CODE! YOU MAY # CHANGE THE FUNCTION CALLS TO TEST YOUR WORK WITH # DIFFERENT INPUT VALUES. == "__main__": divisible(126) print("divisible(126) is:", test1) if -_name_ test1 print() test2 = divisible(20) print("divisible(20) is:", test2) print() test3 divisible (1024) print("divisible (1024) is:", test3) = print() test4 divisible(17) print("divisible(17) is:", test4) = print() test5 divisible(523) print("divisible (523) is:", test5) print() Problem 1: Testing for divisibility Suppose I want to know whether a number, x is evenly divisible by a number y. When I use the phrase "evenly divisible", I mean division without any remainder. So, x is evenly divisible by y if we can divide x by y without leaving any remainder behind (the remainder is equal to 0). Sometimes it is easy to tell whether one number evenly divides another just by looking. If y is 2, then we know that all even numbers (numbers ending with 0, 2, 4, 6, or 8) can be evenly divided by 2 and that all odd numbers (numbers ending with 1, 3, 5, 7, or 9) cannot be evenly divided by 2. The same is true if y is equal to 5. All numbers ending in 5 or 0 can be evenly divided by 5. However, there are many cases of divisibility that are harder to check. Determining whether a number is evenly divisible by 3 or 7 can be difficult to do just by looking, especially for larger numbers. Computer can always do this easily. Recall that the modulus operator, %, is used to return just the remainder from an integer division operation. The expression, x %y, which is read as x modulo y (or x mod y), integer divides x by y and returns just the remainder of the division operation. We can use this to easily determine whether x is evenly divided by y. Ifx % y evaluates to 0, then we can conclude that .x is evenly divisible by y. The file "hw2problem 1.py" contains a Python function called divisible (), which takes one argument, an integer n. Complete the body of the function to do the following: First, create an empty list. Then for each of the integers I through 9, determine whether it evenly divides n. If so, then add True to the list. If not, then add False to the list. Once you have checked whether n is divisible by each of the integers from 1 through 9, return the list of Booleans. For example, suppose the input n is equal to 126. 126 is evenly divisible by 1, 2, 3, 6, 7, and 9. So, I will add True at the first three elements, then two False entries for 4 and 5, then True for 6 and 7, False for 8, and True for 9. The result list can be seen in the table below. I then return the list.. Examples: Function Call divisible (126) divisible (20) divisible (1024) divisible (17) divisible (539 Return Value [True, True, True, False, False, True, True, False, Truel [True, True, False, True, True, False, False, False, False] [True, True, False, True, False, False, False, True, False] [True, False, False, False, False, False, False, False, False] [True, False, False, False, False, False, True, False, False] # #IAE 101 (Fall 2019) # HW 2, Problem 1 def divisible(n): # ADD YOUR CODE HERE return -1 # CHANGE OR REMOVE THIS LINE # DO NOT DELETE THE FOLLOWING LINES OF CODE! YOU MAY # CHANGE THE FUNCTION CALLS TO TEST YOUR WORK WITH # DIFFERENT INPUT VALUES. == "__main__": divisible(126) print("divisible(126) is:", test1) if -_name_ test1 print() test2 = divisible(20) print("divisible(20) is:", test2) print() test3 divisible (1024) print("divisible (1024) is:", test3) = print() test4 divisible(17) print("divisible(17) is:", test4) = print() test5 divisible(523) print("divisible (523) is:", test5) print()
Expert Answer:
Answer rating: 100% (QA)
def divisiblen s i1 while i 10 if ni 0 if i1 s sTrue else s s True else if i1 s ... View the full answer
Related Book For
Posted Date:
Students also viewed these programming questions
-
There are many cases of serious wage and hour violations by employers, particularly in the retail sector. These cases feature employers failing to provide breaks, pressuring workers to underreport...
-
A statistics professor wants to know whether a student's mark on the second statistics test is a good indicator of the student's mark on the final exam. She records the two marks for a random sample...
-
Suppose researchers want to know whether smoking cigars increases the risk of esophageal cancer. a. Could they conduct a randomized experiment to test this? Explain. b. If they conducted an...
-
Suppose there are 50 companies in a commercial building. Initially there is no automatic fire sprinkler system in the public area of the building. Each company has identical individual demand curve...
-
In Problem 1-4, determine convergence or divergence for each of the series. Indicate the test you use? 1. 2. 3. 4. 200 n! n+ 3 Vn + 1
-
Professor Gaedel has written a program that he claims implements Dijkstras algorithm. The program produces .d and . for each vertex V. Give an O(V + E)-time algorithm to check the output of the...
-
While the electromagnetic interaction between the photons and the charged electrons and positrons kept them in equilibrium with each other during the early universe, show that the direct...
-
The comparative balance sheet of Merrick Equipment Co. for Dec. 31, 2014 and 2013, is: The income statement for the year ended December 31, 2014, is as follows: Additional data obtained from an...
-
Direct Labor Budgeted $14,200, Actual $14,200, Indirect Labor Budgeted 5,800, Actual 5,400, Utilities Budgeted 1,900, Actual 2,200, Rent Budgeted 3500, Actual 3500, Marketing Budgeted 150, Actual...
-
Aman and Juanita have $150,000 in their business bank account. You have calculated their warehouse stock and equipment to be valued at a total of $480,000. They want to take out a policy for...
-
If Ali is admitted into SLM ( Salem , Lamees, Maher ) partnership, by purchasing half of Maher's share, and pays Maher double the value of the share, then: a . Maher's capital will be debited by...
-
FoodBots produces robotic parts for the food manufacturing industry. It is a private company owned by two equal shareholders: Imaan and Sofia. They started the company in 2015 and were initially...
-
What are possible muddy areas when aligning lessons and assessments?
-
Let x, y = R". Determine in each case below if (x, y) is an inner product for R". In case it is not then indicate which axioms are not satisfied. n (a) * (x, y) = xi|yi| i=1 n (b) (x, y) = xyi (c) ...
-
500 mg of liquid tetracycline has been prescribed. 500 mg is contained in 20 ml. A measuring device marked in teaspoons is being used. 20 mL is equivalent to how many teaspoons?
-
List 3 possible primary or secondary sources you, the student, can use for each research topic below. Next, explain in in detail why you can use those sources for the topic. (Very important). You...
-
Problem 2. Hair salon versioning. A local hair salon in New Haven is re-designing its business plan. The owner has discovered that they can make more money by offering two packages: "Deluxe" and...
-
A genetically engineered strain of Escherichia coli (E. coli) is used to synthesize human insulin for people suffering from type I diabetes mellitus. In the following simplified reaction scheme,...
-
Refer to Exercise 4. Redraw the picture under the assumption that you will collect 100 measurements instead of only nine. Discuss how the picture differs from the one in Exercise 4. In exercise 4,...
-
Refer to Example 13.2, in which we tested whether there was a relationship between gender and driving after drinking alcohol. Remember that the Supreme Court used the data to determine whether a law...
-
According to The Wellness Encyclopedia (University of California, 1991, p. 17): Alcohol consumed to excess increases the risk of cancer of the mouth, pharynx, esophagus, and larynx. These risks...
-
An example of a posttranslational covalent modification that may affect protein function is a. phosphorylation. b. acetylation. c. methylation. d. Any of the above can affect protein function.
-
During attenuation, when tryptophan levels are high, the ________ stem-loop forms and transcription _________ the trpL gene. a. 12, ends just past b. 34, ends just past c. 12, continues beyond d. 34,...
-
The binding of _______ to lac repressor causes lac repressor to _______ to the operator site, thereby _______ transcription. a. glucose, bind, inhibiting b. allolactose, bind, inhibiting c. glucose,...
Study smarter with the SolutionInn App