From 89c3a251f81ea9de68fec86e24e18b1b71ace25e Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Fri, 20 Oct 2017 10:33:10 -0400 Subject: [PATCH] [x/tour] tour: make Sqrt exercise more accessible Also don't assume prior knowledge of switch statement, which I noticed right below the Sqrt exercise. Change-Id: I1f3e191aa924a79c822780353a526d8edaafc168 Reviewed-on: https://go-review.googlesource.com/72130 Reviewed-by: Austin Clements X-Tour-Commit: 65fff99b2f877724f97e10c96175e2a2bef5be86 --- tour/content/flowcontrol.article | 44 +++++++++++++++++++++++-------- tour/content/img/newton.png | Bin 790 -> 0 bytes 2 files changed, 33 insertions(+), 11 deletions(-) delete mode 100644 tour/content/img/newton.png diff --git a/tour/content/flowcontrol.article b/tour/content/flowcontrol.article index 7eccf84e..a67d7ec6 100644 --- a/tour/content/flowcontrol.article +++ b/tour/content/flowcontrol.article @@ -73,32 +73,54 @@ in `main` begins.) * Exercise: Loops and Functions -As a way to play with functions and loops, implement the square root function using Newton's method. +As a way to play with functions and loops, let's implement a square root function: given a number x, we want to find the number z for which z² is most nearly x. -Newton's method is to approximate `Sqrt(x)` by picking a starting point _z_ first, and repeating: - -.image /content/img/newton.png - -Hint: Iterate and return the final value of _z_ as the answer: +Computers typically compute the square root of x using a loop. +Starting with some guess z, we can adjust z based on how close z² is to x, +producing a better guess: z -= (z*z - x) / (2*z) -To begin with, repeat the calculation 10 times and see how close you get to the answer for various values (1, 2, 3, ...). +Repeating this adjustment makes the guess better and better +until we reach an answer that is as close to the actual square root as can be. -Next, change the loop condition to stop once the value has stopped changing (or only changes by a very small amount). See if that's more or fewer than 10 iterations. How close are you to the [[https://golang.org/pkg/math/#Sqrt][math.Sqrt]]? +Implement this in the `func`Sqrt` provided. +A decent starting guess for z is 1, no matter what the input. +To begin with, repeat the calculation 10 times and print each z along the way. +See how close you get to the answer for various values of x (1, 2, 3, ...) +and how quickly the guess improves. -Hint: To declare and initialize a floating point value, give it floating point syntax or use a conversion: +Hint: To declare and initialize a floating point value, +give it floating point syntax or use a conversion: z := 1.0 z := float64(1) +Next, change the loop condition to stop once the value has stopped +changing (or only changes by a very small amount). +See if that's more or fewer than 10 iterations. +Try other initial guesses for z, like x, or x/2. +How close are your function's results to the [[https://golang.org/pkg/math/#Sqrt][math.Sqrt]] in the standard library? + +(Note: If you are interested in the details of the algorithm, the z² − x above +is how far away z² is from where it needs to be (x), and the division by 2z is the derivative +of z², to scale how much we adjust z by how quickly z² is changing. +This general approach is called [[https://en.wikipedia.org/wiki/Newton%27s_method][Newton's method]]. +It works well for many functions but especially well for square root.) + .play flowcontrol/exercise-loops-and-functions.go * Switch -You probably knew what `switch` was going to look like. +A `switch` statement is a shorter way to write a sequence of `if`-`else` statements. +It runs the first case whose value is equal to the condition expression. -A case body breaks automatically, unless it ends with a `fallthrough` statement. +Go's switch is like the one in C, C++, Java, JavaScript, and PHP, +except that Go only runs the selected case, not all the cases that follow. +In effect, the `break` statement that is needed at the end of each case in those +languages is provided automatically in Go. +Another important difference is that Go's switch cases need not +be constants, and the values involved need not be integers. .play flowcontrol/switch.go diff --git a/tour/content/img/newton.png b/tour/content/img/newton.png deleted file mode 100644 index 3691e5765af1e0ca39f79ba0ea05f8396952dc87..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 790 zcmV+x1L^#UP)dWm{e3$Bx4OA0007^ zNklE8`9*sv#b1N%Du5qHE84Uc| zAsUa^OkiCIPtjI9J$pMM_>m}Z9MvlTtAf9{x3oDS>jCVzLY0JI9^j)RF_&lLjhiEV z^BCcppFT)tEAEu7ts@HAnzhPS)%eXqE?cusSQQ3A_$*-WEtp289KQ<5Wh?i3y8t7Q z%vPSU75>&-kjqw{FC1&PFL{#L$~C?h{`J`^*~)c~o1APF5Azh9Y~_uKduX=)1Kzcx U^#E4t!2kdN07*qoM6N<$f^Fw%CjbBd