6.4. Exercises

  1. Fibonacci numbers have many applications in the field of mathematics and in the study of computer algorithms. The first two Fibonacci numbers, which we will call F1 and F2, are defined to be 0 and 1, respectively. Thereafter, each successive Fibonacci number Fi is defined to be the sum of the two preceding Fibonacci numbers Fi-2 and Fi-1. So F3 is calculated by adding together the values of F1 and F2. Write a program to calculate the first 20 Fibonacci numbers, storing each number as it is calculated into a list. (Hint: Each new Fibonacci number can be appended to the end of the list as the sum of the last two items in the list.)

  2. Based on the population program used in this chapter, write a program to display the city with the smallest population as well as the city with the largest population.

    set USCities to {{"Boston", 574000}, {"Chicago", 2784000}, {"Dallas", 1007000}, ¬
        {"Houston", 1631000}, {"Los Angeles", 3485000}, {"Philadelphia", 1586000}, ¬
        {"San Diego", 1111000}, {"San Francisco", 724000}, {"New York", 7323000}}
  3. Repeat the preceding exercise (#2), substituting the following list for USCities:

    set USCities to {¬ {name:"Boston", population:574000}, ¬ {name:"Chicago", population:2784000}, ¬ {name:"Dallas", population:1007000}, ¬ {name:"Houston", population:1631000}, ¬ {name:"Los Angeles", population:3485000}, ¬ {name:"Philadelphia", population:1586000}, ¬ {name:"San Diego", population:1111000}, ¬ {name:"San Francisco", population:724000}, ¬ {name:"New York", ...

Get Beginning AppleScript® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.