Professional Documents
Culture Documents
Math and More Loops
Math and More Loops
4.1. Introduction
In 1965, Gordon Moore observed that the density of components in
integrated circuits (such as computer CPUs) was doubling every year
or two1 . This observation came to be known as “Moore’s Law” and it Figure 4.1: An illustration of “Moore’s
continued to be valid for several decades, although recently the rate Law” for CPUs. Note that the vertical
axis is logarithmic.
has slowed2 . Similar “Moore’s Laws” have been observed for other Source: Wikimedia Commons
Computers are very good at doing things over and over again very
rapidly. Previously we’ve used “for” loops for this. In this chapter,
we’ll look at several other kinds of loops available in the C programming
language. We’ll start out by using a “for” loop to test how fast your
The first “PC”: The IBM PC 5150,
computer is. Along the way, we’ll find out about C’s math functions
introduced in 1981.
and use them to give your computer something substantial to chew on. Source: Wikimedia Commons
#include <math.h>
But what about. . . ?
In the first case, the programmer puts the output of sqrt into
an integer variable. Since sqrt returns a double number, this
means that the decimal part of the number will be chopped off.
The g++ compiler will warn you about this, but it will go ahead
and compile the program.
g++ will
g++ will give
give aa warning.
warning.
double q;
int i; g++ will
g++ will give
give an
an error,
error, and
and
refuse to do this.
refuse to do this.
i = sqrt(10.);
q = sqrt(10.,2.);
2.5e+3 = 2, 500
6.02e+23 = 6.02 × 1023 (≃ Avogadro’s number)
5e-11 = 5 × 10−11
Notice that 103 is just 1e+3 (“one times ten to the third power”), not
10e+3. Here the e means “times ten to the . . . ”.
Why do we set sum equal to zero before we start the program’s loop?
Won’t it just be zero automatically? No, not necessarily. You shouldn’t
assume that a variable has any particular value before you explicitly
give it one. Remember that variables are temporary boxes in the
computer’s memory. After the program is done with them, the same
chunk of memory can be re-used by other programs. In some cases,
if you don’t explicitly give a variable a value, it will contain whatever
random data happens to be at that memory location, leftover from the
last program that used it.4 4
Some compilers will automatically set
all variables to zero at the beginning of
a program, but it’s best not to assume
Program 4.1: timer.cpp (Version 1) this.
#include <stdio.h>
#include <time.h>
#include <math.h>
int main () {
int i;
int tstart;
int delay;
double sum = 0.0;
tstart = time(NULL);
This is important for a variable like sum in Program 4.1. Notice the
line in bold. Each time around the loop, this sets the new value of sum
√
equal to the old value plus i. If we didn’t explicitly set sum = 0.0
before we began adding things up, then the “old value” of sum would
be undefined (and possibly some bizarre, unexpected number) the first
time we went through the loop.
10 % 7 gives 3
1001 % 10 gives 1
25 % 7 gives 4
Program 4.2 uses the modulo operator to print out the elapsed time,
and the number of square roots that have been summed so far, every
million times around the loop. It does this by looking at i % 1000000
(we can read this as “i modulo one million”). When this quantity is
zero, it means that i is a multiple of 1,000,000.
#include <stdio.h>
#include <time.h>
#include <math.h>
int main () {
int i;
int tstart;
int delay;
double sum = 0.0;
tstart = time(NULL);
What does “modulo” mean anyway? Where does that word come
from?
Take a look at the two clocks in Figure 4.4. Can you tell how much
time has passed? Not necessarily, because clocks count to twelve,
and then they start over again. This is what mathematicians call
“modular arithmetic”. In the case of the clocks, we could say that
they have a “modulus” of twelve.
Modern computers make this much easier for us. Let’s write a program
that uses C’s math functions to generate a table of values for cos(θ )
and sin(θ ) for various values of θ. Before we start, it might be good to
remind ourselves what sine and cosine are. Take a look at Figure 4.7. If
you imagine a point travelling along the circumference of a circle with
a radius of 1, then cos(θ ) and sin(θ ) are just the x and y coordinates of Figure 4.5: Math tables were once
the point when it’s at the angle θ. Let’s start out with θ = 0 and move widely used to find values for
trigonometric functions, logarithms,
around the circle in 100 steps, until we get back to where we started. and other functions. Source: Wikimedia Commons
For a good overview of the techniques
used in constructing such tables, see
this Wikipedia article
r=1
Figure 4.6: Betty Jennings and Frances
Bilas operating ENIAC.
Source: Wikimedia Commons
Remember that there are two different systems for measuring angles:
degrees and radians. When you go all the way around a circle, you’ve
turned by 360°. This is equivalent to 2π radians. C’s trigonometric
functions all use radians, so our program will need to divide 2π radians
into 100 steps, and calculate the sine and cosine for each.
#include <stdio.h>
#include <math.h>
int main () {
double theta = 0.0;
double step = 2.0 * M_PI / 100.0;
int i;
Cosine
0
each step is 2π/100, since the whole circle is 2π radians and we want
to divide it up into 100 steps. -0.5
-1
0 1 2 3 4 5 6
Also notice that we use the symbol M_PI that’s conveniently provided Angle (radians)
for us by math.h.
1
Now run it again, like this, to write the table into a file: -1
0 1 2 3 4 5 6
./trig > trig.dat Angle (radians)
0
this command:
-0.5
plot "trig.dat" -1
-1 -0.5 0 0.5 1
Cosine
You should see something that looks like the top graph in
Figure 4.8. Now try giving gnuplot this command:
Figure 4.8: Plots of θ versus cos(θ ), θ
plot "trig.dat" using 1:3 versus sin(θ ), and cos(θ ) versus sin(θ ).
chapter 4. math and more loops 113
The result should be the first two graphs laid on top of each
other. Finally, try this:
If your file contains more than two columns, you can tell gnuplot which
ones to use as x and y with the “using” qualifier. If you say “using
1:3”, that means “column 1 is x and column 3 is y”. We can ask gnuplot
to superimpose multiple graphs by giving it a comma-separated list of
things to plot, as we did in the next-to-last “plot” command in the
exercise above.
As you can see from the bottom graph in Figure 4.8, our values for Before computers and calculators
became widely available, the slide rule
cos(θ ) and sin(θ ) really do correspond to the x and y values of a point was widely used for calculations
at various angles, as they should. (The circle looks flattened because involving logarithms or trigonometric
the vertical and horizontal scales are different. By default, gnuplot fits functions.
Source: Wikimedia Commons
its graphs into a rectangular window that’s wider than it is tall. You
can fix this by telling gnuplot “set size square”.)
while (CONDITION) {
BLOCK OF STATEMENTS
}
The statements inside the loop will be acted upon again and again, as
long as the “CONDITION” is true. You might notice that this looks a
lot like an “if” statement, where a block of statements is only executed
if some condition is met. With “if”, the block of statements is only
acted upon once, but with while they’re done over and over, for as
long as the condition continues to be met. Here’s an example:
int i = 0;
while ( i < 10 ) {
printf ( "%d\n", i );
i++;
}
The code in this example would print out the integers from zero to nine.
This is the same kind of thing we’ve done with “for” loops, but done
in a different way. Consider the following example, though:
int i = 0;
while ( i < 1000000 ) {
i = rand();
printf ( "%d\n", i );
}
numerical value from one to thirteen. The object of the game is to get
the sum of all your cards as close to twenty-one as possible, without
going over. Each time Program 4.4 goes through its while loop, it
picks a random number from one to thirteen, then adds this number to
the sum so far. It keeps doing this for as long as the sum is less than
twenty-one.
#include <stdio.h>
#include <stdlib.h>
#include <time.h> Traditional playing cards have either
int main () { numbers or faces on them. The values
of the numbers are self-explanatory. For
int sum = 0; the faces, we count Jack, Queen and
int card; King as 11, 12 and 13, respectively.
Source: Wikimedia Commons
Do you see how this makes
srand(time(NULL)); a number between 1 and 13?
while ( sum < 21 ) {
card = (int)( 1 + 13.0*rand()/(1.0 + RAND_MAX) );
sum += card;
printf ("Got %d. Sum is now %d\n", card, sum );
}
}
In the real game of Blackjack, after each card is dealt the player is asked
whether he/she wants another. If the player is very close to twenty-one
already, he or she may choose not to get any more cards, hoping that all
of the other players will either go over twenty-one, or not get as close.
(Whichever player gets closest to twenty-one, without going over, wins.) The Card Players by Catherine Ann
Dorset. (Note that one of the players
Let’s modify our program to allow for this. Take a look at Program 4.5. seems to be a Great Auk, which sadly
became extinct in the mid nineteenth
Century.)
Source: Wikimedia Commons
srand(time(NULL));
while ( sum < 21 ) {
card = (int)( 1 + 13.0*rand()/(1.0 + RAND_MAX) );
sum += card;
printf ("Got %d. Sum is now %d\n", card, sum );
if ( sum == 21 ) {
printf ("You WIN!\n");
} else if ( sum > 21 ) {
printf ("You lose!\n");
} else {
printf ("Enter 1 to continue or 0 to quit while you're ahead: ");
scanf("%d", &ans);
if ( ans != 1 ) {
printf("Your final score was %d\n",sum);
break;
}
}
}
}
As you can see, we’ve added an “if” statement to deal with the various
possible outcomes. If the sum is exactly twenty-one, we tell the player
he or she has won. If it’s over twenty-one, we identify the player as
a loser. If the sum is under twenty-one, we give the player a choice:
continue or quit? If the player chooses to continue, we go around the
loop again.
chapter 4. math and more loops 117
But what if the player chooses to quit? How can we make the loop stop
right now, without waiting for the sum to get greater than twenty-one?
To do this, we use the C language’s “break” statement. A break
statement causes the loop it’s in to stop immediately.
Figure 4.9 shows another program that uses the break statement. The
program in the figure does a countdown, from ten toward zero, but
before it reaches zero the countdown is stopped by using break.
C’s break statements are often useful when your program is searching
for something. Imagine you’re looking through a big stack of books,
trying to find one with a particular title. You start from the top and
look at the books one at a time until you find the one you want. Then
you stop. You don’t keep looking through the rest of the stack.
What if you use break inside two or more nested loops, like this?:
The break statement only halts the innermost loop containing it.
In the example above, the break would stop the nstorms loop,
and the computer would go back to the top of the nrocks loop. If
there were more rocks left to do, it would continue with the next
rock, and start the nstorms loop again for the new rock.
In the second example, the break statement would stop the outer,
nrocks, loop, and the computer would continue without doing
anything else with either of these loops.
What if you wanted to skip the rest of this trip around a loop, but not
stop looping? You can do that, too, using C’s “continue” statement.
chapter 4. math and more loops 119
Loop number 0
This number is below 5.
Loop number 1
This number is below 5.
Loop number 2
This number is below 5.
Loop number 3
This number is below 5.
Loop number 4
This number is below 5.
Loop number 5
Loop number 6
Loop number 7
Loop number 8
Loop number 9
Figure 4.10 shows another countdown example. This time, for some
reason, Mission Control has decided to omit some numbers from the
countdown. (Maybe they’re superstitious?)
Program 4.6 plays this game. It starts out with 12 coins on the table by
setting the variable coins equal to 12. After telling the user the rules
(using some printf statements) the program begins a while loop.
Each time around the loop one of the players (user or computer) takes
some number of coins, and this number is subtracted from coins. The There are more complicated versions of
while loop keeps going as long as the value of coins is greater than Nim. Often it’s played by laying out a
zero. pyramid of objects (such as the
matchsticks shown here), and only
allowing players to remove objects from
a single row during each turn.
Source: Wikimedia Commons
If you try playing this game, you’ll find that the computer always wins!
By employing a simple strategy, the computer can always win the game.
Can you understand how it works?5 5
There’s an excellent Wikipedia ar-
ticle about the game of Nim and the
mathematics behind it. You’ll also be
Notice that the program uses a continue statement to keep users from amused by Matt Parker’s explanation
cheating. If the user picks a number other than 1, 2, or 3, the program of the game on his YouTube channel,
“Standup Maths”. Take a look if you
sends the user back to the top of the loop to try again. can’t figure out how the computer’s
strategy works.
122 practical computing for science and engineering
Also notice how the program switches between “Player 0” and “Player
1”. After each player’s turn, the variable nextplayer is set to a value
that indicates who the next player should be.
#include <stdio.h>
int main () {
int coins = 12;
int take;
int nextplayer = 0; // Player 0=user, 1=computer
int currentplayer;
currentplayer = nextplayer;
printf ("-------- Player %d's Turn --------\n", currentplayer);
if ( currentplayer == 0 ) {
printf ("How many coins will you take?: ");
scanf("%d", &take);
if ( take > 3 || take < 1 ) {
Player 0 printf ("You must take 1, 2, or 3. Try again\n");
continue;
} The computer’s
nextplayer = 1; winning strategy
} else {
take = 4 - take;
Player 1 printf ("I will take %d of them.\n", take );
nextplayer = 0;
}
int nloops = 0;
int i;
Neither of these loops will print out anything, because their conditions
are never satisfied. In the first loop, nloops is zero, and i will never
be less than zero, and the second loop does nothing for a similar reason.
The statements in these loops will never be acted upon, not even once.
do {
printf ( "%d\n", i );
} while ( i < 0 );
do {
BLOCK OF STATEMENTS
} while (CONDITION);
√
1 1 1
π= 12 1 − + − +···
3 · 3 5 · 32 7 · 33
1
(1 + 2n) · 3n
and that the sign of the terms bounces back and forth between positive
and negative. The terms get smaller and smaller as the series goes on.
Program 4.7 starts calculating the terms in this series and adding them
up. It keeps going until it comes to a term that’s smaller than 10−11 (we
chose this value arbitrarily, deciding that we could ignore corrections
smaller than that). The program uses a do-while loop to do the
work. Notice that we use C’s pow function to get the value of 3n when
calculating each term, and the fabs function to find the absolute value
of the term.6 The alternating signs of the terms is taken care of by the 6
See Figure 4.2.
multiplier variable, which alternates between 1 and −1 (can you
see why?).
After each trip through the loop, the computer checks the absolute value
(since the terms alternate between positive and negative) of the current
term to see if it’s less than our cutoff value of 10−11 . A do-while loop
chapter 4. math and more loops 125
is more convenient than a while loop in this case, since we don’t know
what the value of the first term will be until we’ve gone through the
loop once.
At the end of the program, we print out our estimate of π and compare
it to the “actual” value as given by M_PI. Notice that we have to
√
multiply our sum by 12 to get π (see Madhava’s series, above). The
program’s output looks like this:
0
10 Figure 4.12: The difference betwen our
Difference from Pi
#include <stdio.h>
#include <math.h>
int main () {
double sum = 0.0;
double term;
double multiplier = 1.0;
double small = 1.0e-11;
int nterms = 0;
do {
term = multiplier / (( 1.0 + 2.0*nterms ) * pow(3.0,nterms));
sum += term;
nterms++;
multiplier = -multiplier;
} while ( fabs(term) >= small );
4.12. Conclusion
C provides a rich set of math functions and a versatile toolkit of loop
structures. Together, these allow us to write computer programs that
accomplish in seconds tasks that once took many hours of human labor.
• “for” loops are good for situations where you know in advance
how many times you want to go around the loop.
• while loops are good when you want to keep going until some
condition is met.
• do-while loops are good when you want to do a test after going
through the loop the first time.
chapter 4. math and more loops 127
Practice Problems
1. Create a modified version of Program 4.1 (the first version of the
timer.cpp program) that tells you how many square roots per sec-
ond your computer can do. Call the new program speedtest.cpp.
(a) instead of θ, sin(θ ) and cos(θ ), the new program should print out
√
two columns: θ and θ
(b) instead of going from zero to 2π, do it for 100 steps between zero
and ten.
But what if, instead of adding all the interest at the end of the year,
the bank gave you 50% interest after six months and another 50% Portrait of Jacob Bernoulli (1654-1705).
Source: Wikimedia Commons
128 practical computing for science and engineering
after another six months? (A banker would say that the interest
was “compounded” two times per year.) In the middle of the year
you’d have $1,500. Adding another 50% to that at the end of the year
would give you a total of $2,250. Even better! And if the bank paid
us 25% four times per year we’d end up with $2,441, an even larger 2.8
2.7
amount. Compounding the interest more often apparently gives us
2.6
more money at the end of the year. 2.5
2.4
2.3
In the 17thCentury, Jacob Bernoulli realized that you can find out 2.2
how much money you’ll have at the end of the year by multiplying 2.1
2
your original investment by:
1.9
0 20 40 60 80 100
n
1
(1 + ) n Figure 4.13: This is what a graph of
n your interest.cpp program’s output
should look like. Notice that the value
rises rapidly at first, then levels of to a
where n is the number of times per year that the interest is com- value approaching e.
pounded. He discovered that there’s a limit to how much money
you can make, even if you let n go to infinity. In this limit, the
expression above approaches a value of about 2.718. Today we know
this number as Euler’s Constant, e, the base of natural logarithms8 . 8
e is perhaps the second most
important mathematical constant,
So, the most we’d have at the end of the year would be about $2,718,
after π. If we think of π as the “circle
no matter how often the interest is compounded. constant”, we might think of e as
the “growth constant”. It appears
in equations describing growth
Write a program named interest.cpp that uses the pow function and decay in every area of science.
(see Figure 4.2) to evaluate the mathematical expression above. For For more information, see this
Numberphile video by James Grime:
each value of n from 1 to 100 print n and the expression’s value.
https://www.youtube.com/watch?v=AuA2EAgAegE
(The program’s output should be two columns of numbers.) Check
your program by making sure that the value approaches about 2.718
as n increases.
You can also graph your results by typing ./interest > interest.dat
and then using gnuplot to graph the data. To do this, start gnuplot
and type plot "interest.dat" with linespoints. The re-
sult should look something like Figure 4.13.
1 1 1 1
s= 2
+ 2 + 2 + 2 +···
1 2 3 4
Notice that the terms keep getting smaller and smaller. Keep adding
terms until you come to a term that’s less than 10−6 (include this
chapter 4. math and more loops 129
term in your sum). Print out the sum and the number of terms,
clearly identifying which is which. Your program should also use
this sum to print an estimate of the value of π. How can it do this?
Read on!
The loop should keep doing this for as long as the result is not
equal to 1. Each time around the loop, print the current result. For
example, if the user enters the number 5, the program should print:
16
8
4
2
1
871
2614
3922
1961
937 5884
2812 2942
1406 1471
703 4414
2110 2207
1055 6622
3166 3311
You should find that any number you enter will generate a sequence
1583 9934
4750 4967
2375 14902
7126 7451
3563 22354
that ends in 1. Collatz speculated that this was always true for all
10690 11177
5345 33532
16036 16766
8018 8383
4009 25150
starting numbers, but nobody has ever been able to prove it. The
12028 12575
6014 37726
3007 18863
9022 56590
through 1060 and found to be true for each of them, but there might
15227 2596 2578 2618 2620 2638 95498 2668
be some huge number out there somewhere that doesn’t obey this 17131
51394
25697
77092
1462
2194
487
731
2926
1463
4390
2195
2902
1451
4354
2177
491
1474
737
2212
2950
1475
4426
2213
2969
8908
4454
2227
17906
8953
26860
13430
3004
1502
751
2254
19273 543 3292 3293 3266 553 3320 3341 20146 3382
28910 811 815 822 823 4940 4900 825 830 831 834 835 5012 30220 5074
14455 2434 2446 411 2470 2450 2476 415 2494 417 2506 15110 2537
43366 1217 1223 1234 1235 1225 1238 1246 1247 1252 1253 7555 7612
21683 3652 609 3670 617 3706 3676 619 623 3742 626 3760 627 22666 3806 639
65050 1826 1828 1835 1852 1853 1838 1858 1870 1871 313 1880 1882 11333 1903 1918
32525 913 914 915 5506 924 925 926 5560 918 919 921 929 935 5614 940 941 942 34000 5710 959
97576 2740 457 2746 2753 462 2776 463 2780 459 2758 2764 2788 2806 2807 470 2824 471 17000 2855 2878
48788 1370 1372 1373 8260 231 1388 1390 1378 1379 1382 1394 1403 8422 235 1412 1414 8500 8566 1439
24394 684 685 686 4120 4130 694 695 689 4138 690 691 697 4210 4211 706 707 4250 4283 4318
8. Imagine that your algebra teacher has asked you to simplify the 12197
36592
18296
342
171
2056
1028
514
343
1030
515
2060 2065
6196
3098
347
1042
521
2086
1043
3130 516
2068
1034
517
2069
6208
3104
345
1036
518
2074
1037
3112
2092
1046
523
2105
6316
3158
12634
6317
18952
353
1060
530 3184
1061
2122
531
2125
6376
3188
12850
6425
19276
2159
6478
3239
9148 257 1546 1549 1564 1565 258 1552 259 1556 1570 1579 9476 265 1592 1594 9638 9718
expression 12x + 438. You suspect that there’s some common factor
4574 772 773 774 4648 782 4696 783 129 776 778 780 781 785 4738 796 797 798 4819 4859
2287 386 2320 387 2324 391 2348 2350 388 389 390 2344 2356 2369 398 2392 399 14458 14578
6862 193 1160 1162 1174 1175 194 1168 195 1172 1178 7108 199 1196 1198 7229 7289
3431 580 581 582 587 3526 97 584 586 588 589 3554 598 599 21688 21868
of 12 and 438 that you could pull out, but how can you find it?
10294 290 1744 291 1762 1763 292 293 294 1768 1777 299 1798 10844 10934
5147 864 145 872 874 881 5290 146 880 147 884 885 5332 898 899 5422 903 5467 927
15442 432 436 437 2644 2645 73 440 442 2656 2666 449 2698 2711 2710 16402 2782
7721 216 218 1312 1322 7936 220 221 1328 1333 1348 1349 8134 1355 8201 1391
23164 108 109 656 660 661 3968 110 664 4000 666 674 4048 675 4067 4066 24604 4174
5791
17374
54
27
328
164
82
330
165
496
1984
992
55 332
166
83 500
2000
1000
501
333 337
504
2024
1012
506
2026
1013
3040
12202
6101
18304
2033
6100
3050
12302
6151
18454
2087
6262
3131
8687 41 248 250 1504 252 253 1520 9152 1525 9227 9394
with a simple recipe for finding the greatest common factor of two 26062
13031
39094
124
62
31
125
376
188
752 756
378
189
757
2272
1136
126
63
760
380
190
762
381
1144
4576
2288
27682
13841
41524
4697
14092
7046
795 29321 142 856 858 143 860 861 31144 5285
894 895 32987 161 968 970 972 5840 973 977 990 991
447 2686 98962 484 485 486 2920 2932 495 2974
1342 1343 49481 242 1456 243 1460 1466 1486 1487
671 4030 148444 121 728 730 732 733 743 4462
993 1007 6046 37111 182 1096 183 1100 1115 6694
745 4534 4535 167002 137 824 826 828 829 5020 5021
1118 6802 6803 250504 206 1240 207 1244 1255 7532
839 5102 30616 31313 155 932 933 934 939 5650
4786 799 4846 807 29726 890 5344 888 892 893 5366
7180 1199 7270 1211 44590 1336 222 223 1340 8050
3590 3598 603 3635 3634 615 22295 668 111 669 670 4025
1795 1799 1810 10906 1817 1846 66886 334 2008 335 12076
5386 897 5398 905 5453 5452 923 33443 167 1004 1006 6038
8080
4040
2692
1346
673
2699
8098
4049
2716
1358
679
16360
8180
4090
2726
1363
681
2770
1385
4156
100330
50165
251
754
503
1510
755
3019
9058
4529
2020 12148 2038 2045 2044 2078 75248 2098 2134 377 2266 13588
1010 6074 1019 6136 1022 1039 37624 1049 1067 1132 1133 6794
505 3037 3058 3068 511 519 3118 18812 3148 3202 566 3400 567 3397
1516 9112 1529 1534 1558 1559 9406 1574 1601 283 1700 1702 10192
758 759 4556 4588 767 779 4678 4703 786 787 4804 801 850 851 5096 849
379 2278 2294 2302 2338 2339 14110 393 2362 2402 2404 425 2554 2548
1138 1139 1147 1151 1169 7018 7055 1180 1181 1201 1202 1276 1277 1274
569 3418 3442 3454 3508 3509 585 21166 590 3544 591 3604 601 638 3832 636 637
854 5128 855 5164 5182 876 877 5264 878 31750 886 887 900 901 902 907 958 159 956 957
427 2564 2566 2582 2591 438 2632 439 15875 443 2662 450 2704 451 2722 479 478 2872
1282 1283 1291 7774 219 1316 1318 47626 1330 1331 225 1352 1354 1361 1438 239 1436
1924 1925 1937 11662 1972 329 1978 71440 1996 1997 338 2032 339 2042 2158 359
961 962 5776 963 5812 969 5831 978 979 984 986 988 989 35720 999 996 997 998 5992 169 1016 1018 1021 1079 1078
2884 481 2888 2890 2906 2908 17494 489 2938 492 493 494 2968 17860 2998 498 2992 499 2996 508 509 510 3064 3238 539
1442 1444 1445 1453 1454 8747 1468 1469 246 1480 247 1484 8930 1499 249 1496 1498 254 1528 255 1532 1619 1618
method. The program should start by asking the user for two 721
2164
1082
722
361
4336
2168
1084
723
2170
1085
726
363
4360
2180
1090
727
2182
1091
26242
13121
39364
734
367
1102
4408
2204
735
2206
1103
747
2242
1121
123 740
370
185
741
2224
1112
742
371
1114
4465
13396
6698
4498
2249
6748
748
374
187
749
2248
1124 1126
750
375
127
382
191
764 765
2296
1148
766
383
1150
4858
2429
7288
809
2428
1214
891
2674
1337
540 541 542 3256 545 3274 19682 551 3310 3364 556 557 3349 558 3374 561 562 563 574 575 3644 607 606 4012
integers. When you run the program, it should look something like 270
135
1624
812
406
813
2440
271
814
407
1628
817
2452
1636
818
409
1637
4912
2456
819
2458
9841
29524
14762
1654
827
2482
1655
4966
2483
1682
841
2524
278
139
1672
836
418
10048
5024 837
2512 419
279
838 843
2530
1687
5062
2531
840
420
1684
842
421
281
844
422
1690
2536
845
287
862
431
1726
863
2590
1822
911
2734
303
910
455
2006
1003
3010
203 1220 1222 1226 1228 1229 7381 1241 7450 1262 209 1256 1258 1265 7594 210 1264 211 1268 1294 1295 1367 1366 1505
this:
610 611 612 613 614 3688 22144 3724 3725 630 631 624 628 629 3796 3797 105 632 634 647 3886 4102 683 687 4516
305 1834 306 1840 307 1844 11072 1862 11176 315 1894 312 314 1888 1898 11392 316 317 1942 1943 2051 2050 2062 2258
912 916 917 153 920 922 5536 930 931 5588 943 945 946 947 156 157 944 948 949 5696 158 952 954 971 5830 987 6154 1025 1031 1129
456 458 2752 460 461 2768 465 2794 2830 2836 473 2842 78 472 474 2848 79 476 477 2914 2915 2962 507 3077 3076 513 3094 3388
228 229 1376 230 1384 1396 1397 1415 1418 1420 1421 39 236 237 1424 238 1432 1457 8746 1481 1522 9232 1538 1540 1547 1694
114 688 115 692 693 696 698 4192 705 4246 708 709 710 4264 118 712 714 119 716 717 4372 4373 729 4444 761 777 4616 769 770 771 4642 846 847
57 344 346 2080 348 349 2096 2116 2123 354 2128 355 2132 59 356 357 358 2152 2186 13120 2188 2222 2284 2332 2308 385 2314 2321 423 2542
86
43
173
520
260
1040
522
261
174
87
262
524
1048
1576
525
1058
529
1588
6370
3185
9556
177
528
264
1064
532
266
1066
533
1600
178
89
1072
536
268 269
179
538
1076 1093
3280
1640
6560
546
273
547
1642
1094
555
1666
1111
3334
1667
1142
571
1714
1166
583
1750
1154
577
1732
1156
578
289
1157
3472
1736
579
1738
6964
3482
1741
633
1900
1270
1906
635
1271
3814
1907
130 784 131 788 789 792 794 4778 132 133 800 134 808 810 816 820 821 833 5002 857 875 866 867 868 869 870 5224 882 883 950 951 953 5722 955
196
576
394
197
98
2368
1184
592
396
198
99
397
1192
596 597
2389
7168
3584
66
33
400
200
100
67 404
202
101
405
1216
608
408
204
102
410
205
616
2464
1232
618
2500
1250
625
2501
7504
3752 642
2572
1286
643
2626
1313
3940
433
1300
650
2602
1301
3904
434
217
648
2608
1304
652
435
1306
653
2612 441
1324
662 3976
2650
1325
663
475
1426
713
2854
1427
4282
2860
1430
715
2861
8584
4292
2866
1433
4300
288 49 296 298 1792 50 304 51 308 309 1876 321 1930 1970 325 1952 324 326 1960 331 1988 1990 2140 2141 2146 2150
GCF is 6 144
72
36
148
74
37
149
448
224
896 25 152
76
38
154
77
232
928
464
936
468
234
938
469
1408
964
482
241
965
2896
1448
966
483
1450
985
2956
960
1478
480
976
488
240
162
81
244
163
490
245
980
1472
2944
981
1492
994
497 2986
1493
995
534
267
1070
535
1606
6424
3212
1073
3220
1610
537
1612
1075
3226
1613
18 112 19 116 117 704 720 724 725 738 739 120 122 736 744 746 4480 753 802 803 804 805 806 4840
9 56 58 352 360 362 2176 369 2218 60 61 368 372 373 2240 2260 401 2410 402 2416 403 2420
28 29 176 180 181 1088 1108 1109 30 184 186 1120 1130 1204 1205 201 1208 1210
14 88 90 544 552 554 3328 15 92 93 560 564 565 600 602 3616 604 605
7 44 45 272 276 277 1664 46 280 282 1696 300 301 1808 302 1816
Hint 1: Remember that the % operator gives you the remainder after
768 22 136 138 832 23 140 141 848 852 853 150 904 906 151 908 909
division.
48 52 53 320 336 340 341
12 13 80 84 85 512
6 40 42 256
3 20 21 128
16
8
32
64
long as rem != 0. 2
11
This is also sometimes called the
“greatest common divisor” or “greatest
common denominator”.
chapter 4. math and more loops 131
2
1 1 1 1 1 1 1.8
s= + + + + + +···
1 2 4 8 16 32 1.6 Size of term
Sum of terms
1.4
1.2
1
Notice that the denominators of the terms start with 1, and each de- 0.8
0.6
nominator is two times as large as the preceding one. Your program 0.4
should keep adding terms until it comes to one that’s smaller than 0.2
0
10−9 (include this term in your sum). 0 2 4 6 8 10
Term Number
smaller than the preceding term, your program should show a sum
that’s approximately 2.
The “.20” between % and lf tells the program to print twenty digits
after the decimal point. By watching how the terms change, we can
see them get smaller and smaller, and we can see the sum get closer
and closer to 2.