• No results found

1. Greatest Common Divisor. The greatest common divisor is the largest number which will evenly divide two other numbers. Examples: GCD( 5, 10 ) = 5, the largest number that evenly divides 5 and 10. GCD( 21, 28 ) = 7, the largest number that divides 21 and 28. GCD’s are used to reduce fractions. Once you have the GCD of the numerator and denominator, they can both be divided by the GCD to reduce the fraction to simplest form. 21/28 reduces to 3/4.

Greatest Common Divisor of two integers,

p

and

q

Loop. Loop untilp=q.

Swap. Ifp < q then swappandq,pq.

Subtract. Ifp > q then subtractqfrom p,p←p−q. Result. Printp

2. Extracting the Square Root. This is a procedure for approximating the square root. It works by dividing the interval which contains the square root in half. Initially, we know the square root of the number is somewhere between 0 and the number. We locate a value in the middle of this interval and determine of the square root is more or less than this midpoint. We continually divide the intervals in half until we arrive at an interval which is small enough and contains the square root. If the interval is only 0.001 in width, then we have the square root accurate to 0.001

Square Root of a number,

n

Two Initial Guesses.

g1←0

g2←n

At this point,g1×g1−n≤0≤g2×g2−n.

Loop. Loop until|g1×g1−n| ÷n <0.001.

Midpoint. mid←(g1+g2)÷2

Midpoint Squared vs. Number. cmp←mid×mid−n

Which Interval?

ifcmp≤0theng1←mid. ifcmp≥0theng2←mid.

ifcmp= 0, mid is the exact answer! Result. Printg1

3. Sort Four Numbers. This is a challenging exercise in if-statement construction. For some additional insight, see[Dijkstra76], page 61.

Given 4 numbers (W,X, Y,Z)

Assign variablesw,x,y,zso thatw≤x≤y≤z andw, x, y, zare from W,X,Y, andZ.

Do not use an array. One way to guarantee the second part of the above is to initializew, x, y,z to W,X,Y,Z, and then use swapping to rearrange the variables.

Hint: There are only a limited combination of out-of-order conditions among four variables. You can design a sequence of if statements, each of which fixes one of the out-of-order conditions. This sequence of if statements can be put into a loop. Once all of the out-of-order conditions are fixed, the numbers are in order, the loop can end.

4. Highest Power of 2. This can be used to determine how many bits are required to represent a number. We want the highest power of 2 which is less than or equal to our target number. For example64100<128. The highest power of25100<26.

Given a number n, find a numberpsuch that 2pn <2p+1.

This can be done with only addition and multiplication by 2. Multiplication by 2, but the way, can be done with the ‘<<’ shift operator. Do not use the pow()function, or even the ‘**’ operator, as these are too slow for our purposes.

Consider using a variable c, which you keep equal to2p. An initialization might be ‘p = 1’, ‘c = 2’. When you increment pby 1, you also doublec.

Develop your own loop. This is actually quite challenging, even though the resulting program is tiny. For additional insight, see [Gries81], page 147.

5. How Much Effort to Produce Software? The following equations are the basic COCOMO esti- mating model, described in [Boehm81]. The input, K, is the number of 1000’s of lines of source; that is total source lines divided by 1000. Development Effort, whereK is the number of 1000’s of lines of source. E is effort in staff-months.

E= 2.4×K1.05

Development Cost, where E is effort in staff-months, R is the billing rate. C is the cost in dollars (assuming 152 working hours per staff-month)

C=E×R×152

Project Duration, whereE is effort in staff-months. Dis duration in calendar months.

D= 2.5×E0.38

Staffing, where E is effort in staff-months, D is duration in calendar months. S is the average staff size.

S= E

D

Evaluate these functions for projects which range in size from 8,000 lines (K = 8) to 64,000 lines (K = 64) in steps of 8. Produce a table with lines of source, Effort, Duration, Cost and Staff size. 6. Wind Chill Table. Wind chill is used by meteorologists to describe the effect of cold and wind

combined. Given the wind speed in miles per hour, V, and the temperature in °F, T, the Wind Chill, w, is given by the formula below. SeeWind Chill in Numeric Types and Expressions for more information.

35.74 + 0.6215×T−35.75×(V0.16) + 0.4275×T×(V0.16)

Wind speeds are for 0 to 40 mph, above 40, the difference in wind speed doesn’t have much practical impact on how cold you feel.

Evaluate this for all values of V (wind speed) from 0 to 40 mph in steps of 5, and all values of T (temperature) from -10 to 40 in steps of 5.

7. Celsius to Fahrenheit Conversion Tables. We’ll make two slightly different conversion tables. For values of Celsius from -20 to +30 in steps of 5, produce the equivalent Fahrenheit temperature. The following formula converts C (Celsius) to F (Fahrenheit).

F= 32 +21232 100 ×C

For values of Fahrenheit from -10 to 100 in steps of 5, produce the equivalent Celsius temperatures. The following formula converts F (Fahrenheit) to C (Celsius).

C= (F−32)× 100 21232

8. Dive Planning Table. Given a surface air consumption rate, c, and the starting, s, and final, f, pressure in the air tank, a diver can determine maximum depths and times for a dive. For more information, seeSurface Air Consumption Ratein Numeric Types and Expressions. Acceptc,s and f from input, then evaluate the following fordfrom 30 to 120 in steps of 10. Print a table oft andd. For each diver,cis pretty constant, and can be anywhere from 10 to 20, use 15 for this example. Also, s andf depend on the tank used, typical values ares=2500 andf =500.

t= 33(s−f)

c(d+ 33)

9. Computingπ. Each of the following series compute increasingly accurate values ofπ(3.1415926...) • π 4 = 1 1 3 + 1 5 1 7 + 1 9 1 11+· · ·π 2 6 = 1 + 1 22+ 1 32+ 1 42+· · · ∑ (1 )k( 4 2 1 1 )

π= 1 + 1 3+ 1·2 3·5 + 1·2·3 3·5·7 +· · ·

10. Computing e. A logarithm is a power of some base. When we use logarithms, we can effectively multiply numbers using addition, and raise to powers using multiplication. Two Python built-in func- tions are related to this: math.log()andmath.exp(). Both of these compute what are called natural logarithms, that is, logarithms where the base is e. This constant,e, is available in themathmodule, and it has the following formal definition: Definition ofe.

e= ∑

0≤k<∞ 1

k!

For more information on the (Σ) operator, seeDigression on The Sigma Operator.

Then! operator is “factorial”. Interestingly, it’s a post-fix operator, it comesafter the value it applies to.

n! =(n−1)×(n−2)× · · · ×1.

For example,4! = 4×3×2×1 = 24. By definition,0! = 1. If we add up the values 1

0! + 1 1! + 1 2! + 1

3! +· · · we get the value of e. Clearly, when we get to about

1/10!, the fraction is so small it doesn’t contribute much to the total. We can do this with two loops, an outer loop to sum up the 1

k! terms, and an inner loop to compute

thek!.

However, if we have a temporary value of k!, then each time through the loop we can multiply this temporary byk, and then add1/tempto the sum.

You can test by comparing your results against math.e,e≈2.71828 or ‘math.exp(1.0)’. 11. Hailstone Numbers. For additional information, see[Banks02].

Start with a small number, n,1≤n <30.

There are two transformation rules that we will use:

• Ifn is odd, multiple by 3 and add 1 to create a new value forn. • Ifn is even, divide by 2 to create a new value forn.

Perform a loop with these two transformation rules until you get ton = 1. You’ll note that whenn= 1, you get a repeating sequence of 1, 4, 2, 1, 4, 2, ...

You can test for oddness using the % (remainder) operation. If ‘n % 2 == 1’ , the number is odd, otherwise it is even.

The two interesting facts are the “path length”, the number of steps until you get to 1, and the maximum value found during the process.

Tabulate the path lengths and maximum values for numbers 1..30. You’ll need an outer loop that ranges from 1 to 30. You’ll need an inner loop to perform the two steps for computing a new n until n == 1; this inner loop will also count the number of steps and accumulate the maximum value seen during the process.

Check: for 27, the path length is 111, and the maximum value is 9232.