1.5 Exercises
-
Imagine that you are creating a pocket calculator. You have created the functionality for all the buttons except x 2, the button that squares a number, and exp, which allows you to calculate baseexponent, where exponent is an integer. You may use any other functionality a calculator would normally have: for example, (+, -, *, /, =).
-
Create the functionality for the x 2 button.
-
Create the functionality for the exp button.
-
-
In the third-grade math class of French mathematician Carl Gauss, the teacher needed to give the students some busywork. She asked the class to compute the sum of the first 100 numbers (1 to 100). Long before the rest of the class had finished. Carl raised his hand and told his teacher that he had the answer: 5,050.
-
Craft an algorithm that will sum the first n numbers (assuming n ≥ 1). How many steps does your algorithm take to complete when n = 100? How many steps does it take when n = 1,000?
-
Can you create an algorithm like Gauss’s where the number of steps does not depend on n?
-
-
A palindrome is a word or phrase that reads the same way forward and backward, like “racecar.” Describe a sequence of steps that determines if a word or phrase is a palindrome.
-
Consider the three mazes shown in Figure 1-3. Describe two different algorithms for solving a maze. Discuss advantages and disadvantages of each algorithm. Then look at the maze and predict which algorithm will complete first. See if your predictions were correct by applying your algorithms ...
Get Computer Science Programming Basics in Ruby 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.