The Sagemath/Python code below modifies the previous PF (N) function to simultaneously collect common prime factors...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
The Sagemath/Python code below modifies the previous PF (N) function to simultaneously collect common prime factors p to find the GCD. (a) Add short comments to all lines that end in # explaining that line of code (b) Describe HOW the code finds the GCD. Include a detailed example in words for pfGCD(30,84) this is how it works (c) What does n,mx = x,an do? and could it be replaced by an-mx; mx-mn? Why or Why not? www answer []# all primesc-sqrt(1000) primes = [2,3,5,7,11,13,17,19,23,29,31] def pfGCD (a,b): "return GCD of a and b when < 1000" min(a,b); mx-max(a,b) mn if mn<1 or mx>999: #check range print("%d and %d must be > and <1000" % (a,b)) return 0 # gcd = 1 # for p in primes: # while mn%p==0 and mx%p--0: # # # gcd * p mn // p mx //- p while mn%p--0: mn //- p. while mx%p--e: mx //- p if mn>mx: # # # # return gcd mn, mx mx, mn if mx%mn--0: return gcd*mn # if mn-1 or p*p>mx: return gcd #python trick for a,b in [(660, 350), (99,53), (30,84)]: print("pfGCD (%d, %d) - % (a,b),pfGCD (a,b)) A1.5 (a) Complete the Sagemath/Python code below to print the table while implementing the Euclidean algorithm. Add code where indicated and explain why the marked statements are necessary. they are necessary because In [ ]: def EuclidGCDtable(a,b): "print table while finding GCD" print("\n a b q r = 1 #why is this necessary? while r>0: #q (add code here) #r (add code here) print("%4d %4d %4d %4d" % (a,b,q,r), end=' ') if r==0: - print("gcd", b) return b print() a = b; b = r #why is this necessary? for a,b in [(660, 350), (99,53), (30,84)]: EuclidGCDtable(a,b) (b) create a new function EuclidGCD (a, b) without the print statements and show that it gives the same results as pfGCD (a,b) in Exercise A1.4. In [ ]: def EuclidGCD (a,b): "add code here" for a,b in [(660, 350), (99,53), (30,84)]: print("gcd ( %d,%d) =" % (a,b), end=', ') print (EuclidGCD (a,b), "Euclid", end=' ') print (pfGCD (a,b), "prime factor") 10 11 The Sagemath/Python code below modifies the previous PF (N) function to simultaneously collect common prime factors p to find the GCD. (a) Add short comments to all lines that end in # explaining that line of code (b) Describe HOW the code finds the GCD. Include a detailed example in words for pfGCD(30,84) this is how it works (c) What does n,mx = x,an do? and could it be replaced by an-mx; mx-mn? Why or Why not? www answer []# all primesc-sqrt(1000) primes = [2,3,5,7,11,13,17,19,23,29,31] def pfGCD (a,b): "return GCD of a and b when < 1000" min(a,b); mx-max(a,b) mn if mn<1 or mx>999: #check range print("%d and %d must be > and <1000" % (a,b)) return 0 # gcd = 1 # for p in primes: # while mn%p==0 and mx%p--0: # # # gcd * p mn // p mx //- p while mn%p--0: mn //- p. while mx%p--e: mx //- p if mn>mx: # # # # return gcd mn, mx mx, mn if mx%mn--0: return gcd*mn # if mn-1 or p*p>mx: return gcd #python trick for a,b in [(660, 350), (99,53), (30,84)]: print("pfGCD (%d, %d) - % (a,b),pfGCD (a,b)) A1.5 (a) Complete the Sagemath/Python code below to print the table while implementing the Euclidean algorithm. Add code where indicated and explain why the marked statements are necessary. they are necessary because In [ ]: def EuclidGCDtable(a,b): "print table while finding GCD" print("\n a b q r = 1 #why is this necessary? while r>0: #q (add code here) #r (add code here) print("%4d %4d %4d %4d" % (a,b,q,r), end=' ') if r==0: - print("gcd", b) return b print() a = b; b = r #why is this necessary? for a,b in [(660, 350), (99,53), (30,84)]: EuclidGCDtable(a,b) (b) create a new function EuclidGCD (a, b) without the print statements and show that it gives the same results as pfGCD (a,b) in Exercise A1.4. In [ ]: def EuclidGCD (a,b): "add code here" for a,b in [(660, 350), (99,53), (30,84)]: print("gcd ( %d,%d) =" % (a,b), end=', ') print (EuclidGCD (a,b), "Euclid", end=' ') print (pfGCD (a,b), "prime factor") 10 11
Expert Answer:
Answer rating: 100% (QA)
Lets first complete the EuclidGCDtable function and then create the EuclidGCD function without print ... View the full answer
Related Book For
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill
Posted Date:
Students also viewed these programming questions
-
The fact that for any linear map the rank plus the nullity equals the dimension of the domain shows that a necessary condition for the existence of a homomorphism between two spaces, onto the second...
-
An investor has decided to model PPB ple shares using the lognormal model. Using historical data, she has estimated the drift and volatility parameters to be 6% and 25% respectively. PPB's current...
-
1. Sam and alex bought 20 blue and red tickets worth $100. The blue tickets were three dollars each and the red tickets were $11 each. How many blue and red tickets did they buy? 2. Shelly is five...
-
Table illustrates the quantities, marginal costs, average variable costs, and average costs of a competitive firm. Refer to table 4. How much is the unit profit at price =$30? [the problem is based...
-
A curve in a speed track has a radius of 200 m and a rated speed of 180 km/h. (See Sample Prob. 12.6 for the definition of rated speed.) Knowing that a racing car starts skidding on the curve when...
-
Write a program that performs as a very simple calculator. Your calculator should be able to handle the four basic math operations add, subtract, multiply, and divide on two input values. Your...
-
Is there a best method for completing performance appraisals for each of the three different types of employees in an MNE? If so, describe the ways in which an HRIS may help.
-
Giant acquired all of Small's common stock on January 1, 2011. Over the next few years, Giant applied the equity method to the recording of this investment. At the date of the original acquisition,...
-
Smee Inc. produces all-natural organic peanut butter. The peanut butter is sold in 12-ounce jars. The sales budget for the first four months of the year is as follows: January February March April...
-
The Tusquittee Company is a retail company that began operations on October 1, 2018, when it incorporated in the state of North Carolina. The Tusquittee Company is authorized to issue 100,000 shares...
-
Consider the DEBUG screen shown below to answer this question. xx-0000 BX-0105 CX-0801 DX-0000 SP-00FD BP-0000 $1-0114 DI-0000 DS-1234 ES 073 $$-073F CS 073F IP 0100 MU UP ET PL MZ NA PO CY 073F:0100...
-
Discussion Topic Activity Time: 3 hours Directions: Valuation is the analytical process of establishing the worth of an asset or a business. There are many techniques used for doing a valuation. We...
-
. Black & Reiss found that the juvenile's deference (attitude) toward police had little to no impact as to whether the juvenile would be arrested. In other words, if a juvenile was exceptionally rude...
-
Question: How Limited Is Our Liability? You are the Safety Director for a small, family-held corporation named East Bound and Down Trucking Company. The corporation is held in thirds by three...
-
Arlene is a new agent at Betty's Buy & Sell Brokerage. Arlene meets with a new buyer, Velma, a 65-year-old widow, who is looking for a new home. Velma tells Arlene - "I need peace and quiet, I do not...
-
D&J are excited to commence business; however, they need start-up capital. David is insolvent, and John is married in Community of property. John is concerned about his wife withholding permission to...
-
33) For the equation: 2 KCIO3 (s) 2 KCl (s) + 3 02 (g), you start with some KCIO3 that decomposes into the products. At equilibrium, there is some solid remaining and the total pressure in the flask...
-
A company has the following incomplete production budget data for the first quarter: In the previous December, ending inventory was 200 units, which was the minimum required, at 10% of projected...
-
Bill and Guilda each own 50 percent of the stock of Radiata Corporation, an S corporation. Guilda's basis in her stock is $25,000. On July 31, 2012, Bill sells his stock, with a basis of $40,000, to...
-
Cedar Corporation has an S corporation election in effect. During the 2012 calendar tax year, the corporation had ordinary taxable income of $200,000, and on January 15, 2012, the corporation paid...
-
For each of the following situations, indicate whether the taxpayer(s) is (are) required to file a tax return for 2012. Explain your answer. a. Helen is a single taxpayer with interest income in 2012...
-
A change in chromosome structure that does not involve a change in the total amount of genetic material is a. a deletion. b. a duplication. c. an inversion. d. none of the above.
-
The term endopolyploidy refers to the phenomenon of having a. too many chromosomes. b. extra chromosomes inside the cell nucleus. c. extra sets of chromosomes in certain cells of the body. d. extra...
-
Which of the following is a type of plasmid? a. F factor (fertility factor) b. R factor (resistance plasmid) c. Virulence plasmids d. All of the above are types of plasmids.
Study smarter with the SolutionInn App