Last active
August 29, 2015 14:04
-
-
Save haukurk/f4d939bc49689efb77d9 to your computer and use it in GitHub Desktop.
golang tour solutions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 25: Loops and Functions | |
/////////////////////////////////////// | |
package main | |
import ( | |
"fmt" | |
"math" | |
) | |
func Sqrt(x float64) float64 { | |
const delta_threshold = 2.2000e-10 | |
z := 1.0 | |
last := x | |
for math.Abs(last-z) > delta_threshold { | |
last, z = z, z - (( z * z - x ) / ( 2 * z )) | |
//fmt.Println(z, last, math.Abs(last-z)) | |
} | |
return z | |
} | |
func main() { | |
fmt.Printf("Newtons Approx: %v, math.Sqrt: %v ", Sqrt(2), math.Sqrt(2)) | |
} | |
// Exercise 38: Slices | |
/////////////////////////////////////// | |
package main | |
import "code.google.com/p/go-tour/pic" | |
func Pic(dx, dy int) [][]uint8 { | |
// Create y slices | |
picture := make([][]uint8, dy) | |
// Create x slices | |
for x := range picture { | |
picture[x] = make([]uint8, dx) | |
} | |
// Generate integers | |
for y := range picture { | |
for x := range picture[y] { | |
//picture[y][x] = uint8((x+y)/2) | |
picture[y][x] = uint8(x*y) | |
} | |
} | |
return picture | |
} | |
func main() { | |
pic.Show(Pic) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment