Page 190 - Learn To Program With Scratch
P. 190

When the inner loop terminates after looping three times, P is incre-
                         mented by 1, and the second iteration of the outer loop starts. The value
                         of S is reset to 1, and the inner loop is executed again. This causes the
                         sprite to say “P2,S1” and “P2,S2” and “P2,S3.” The process continues in a
                         similar manner, causing the sprite to say “P3,S1” and “P3,S2” and “P3,S3”
                         and finally “P4,S1” and “P4,S2” and “P4,S3” before the script ends. Trace
                         through this script to make sure you understand how it works.
                             Now that you’ve seen what nested loops can do, let’s apply this tech-
                         nique to solve an interesting math problem. We want to write a program
                         to find three positive integers n , n , and n  such that n  + n  + n  = 25 and
                                                    1
                                                                             2
                                                                                 3
                                                                         1
                                                              3
                                                       2
                            2
                                         2
                                   2
                         (n )  + (n )  + (n )  = 243. Because computers are good at repetitive tasks,
                           1
                                        3
                                 2
                         our plan is to try all possible combinations of numbers (a technique called
                         exhaustive search) and let the computer do the hard work.
                             Based on our first equation, the first number, n , can have any value
                                                                      1
                         between 1 and 23 since we’ll need to add two numbers to it to get 25. (You
                                                                              2
                         might have noticed that n  can’t be more than 15 because 16  = 256, which
                                               1
                         is greater than 243. But we’ll just ignore our second equation for now and
                         set the upper limit of the loop to 23 anyway.)
                             The second number, n , can be any value between 1 and 24 – n . For
                                                                                    1
                                                2
                         example, if n  is 10, the maximum possible value of n  is 14 because n  must
                                                                        2
                                                                                      3
                                    1
                         be at least 1. If we know n  and n , we can compute n  as 25 – (n  + n ). Then,
                                                     2
                                                                      3
                                                                                   2
                                                                               1
                                               1
                         we’ll need to check whether the sum of the squares of these three numbers
                         is 243. If it is, we are done. Otherwise, we need to try a different combina-
                         tion of n  and n . You can see the finished script to find n , n , and n  in
                                                                           1
                                                                              2
                                1
                                                                                     3
                                       2
                         Figure 7-16.
            NestedLoops2
                     .sb2              The outer loop will try n1 =1, 2, ... , 23.

                                              The inner loop will try n2 =1, 2, ... , (24−n1).

                                                                                 For each (n1,n2)
                                                                                 combination,
                                                                                 compute n3
                                                                                 and check the
                                                                                 sum of squares.
                                                                                 If it is 243,
                                                                                 say the answer
                                                                                 and stop.
                                            Try the next value of n2.

                                          None of the tried n2 values worked. Try next value of n1.
                                              We tried all possible (n1,n2) combinations and nothing
                                              worked. The problem does not have a solution.
                         Figure 7-16: This script searches for three positive numbers whose sum is 25 and whose
                         sum of squares is 243 .
           168   Chapter 7
                                           www.it-ebooks.info
   185   186   187   188   189   190   191   192   193   194   195