Last active
August 29, 2015 14:06
-
-
Save udzura/c907649e8a730c719b18 to your computer and use it in GitHub Desktop.
tour of go memo
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
package main | |
import ( | |
"fmt" | |
"math" | |
) | |
const RepeatTimes = 3 | |
func Sqrt(x float64) float64 { | |
z := float64(1) | |
for i := 0; i < RepeatTimes; i++ { | |
z = newton(x, z) | |
} | |
return z | |
} | |
func Sqrt_AutoStop(x float64) float64 { | |
z1 := float64(1) | |
z2 := newton(x, z1) | |
for math.Abs(z1 - z2) > float64(1e-15) { | |
z1 = z2 | |
z2 = newton(x, z2) | |
} | |
return z2 | |
} | |
func newton(x float64, z float64) float64 { | |
return z - (math.Pow(z, 2) - x) / (2 * z) | |
} | |
func main() { | |
fmt.Println(Sqrt(2)) | |
fmt.Println(Sqrt_AutoStop(2)) | |
fmt.Println(math.Sqrt(2)) | |
// 1.4142156862745099 | |
// 1.414213562373095 | |
// 1.4142135623730951 | |
} |
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
package main | |
import ( | |
"code.google.com/p/go-tour/pic" | |
"math" | |
) | |
func Pic(dx, dy int) [][]uint8 { | |
canvas := make([][]uint8, dx) | |
for i := 0; i < dx; i++ { | |
row := make([]uint8, dy) | |
canvas[i] = row | |
} | |
for x := 0; x < dx; x++ { | |
for y := 0; y < dx; y++ { | |
f := powrainbow // change me | |
canvas[x][y] = uint8(f(x, y)) | |
} | |
} | |
return canvas | |
} | |
// 遊び過ぎ... | |
func grad(x, y int) int { return x ^ y % 8 } | |
func para(x, y int) int { return x ^ y } | |
func mid(x, y int) int { return (x + y) / 2 } | |
func circ(x, y int) int { return x * x + y * y } | |
func bi(x, y int) int { return x * y } | |
func tri(x, y int) int { return x * x * x + y * y * y } | |
func powrainbow(x, y int) int { | |
return int((math.Pow(float64(x), 0.5) + | |
math.Pow(float64(y), 0.5)) * 32) | |
} | |
func main() { | |
pic.Show(Pic) | |
} |
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
package main | |
import ( | |
"code.google.com/p/go-tour/wc" | |
"unicode" | |
"strings" | |
) | |
func WordCount(s string) map[string]int { | |
aMap := make(map[string]int) | |
splitBy := func(c rune) bool { | |
return unicode.IsSpace(c) | |
} | |
fields := strings.FieldsFunc(s, splitBy) | |
for _, word := range(fields) { | |
if _, ok := aMap[word]; ok { | |
aMap[word] += 1 | |
} else { | |
aMap[word] = 1 | |
} | |
} | |
return aMap | |
} | |
func main() { | |
wc.Test(WordCount) | |
} |
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
package main | |
import "fmt" | |
// fibonacci is a function that returns | |
// a function that returns an int. | |
func fibonacci() func() int { | |
var i, j, j2 = 1, 0, 0 | |
return func() int { | |
j2 = i + j | |
i, j = j, j2 | |
return j2 | |
} | |
} | |
func main() { | |
f := fibonacci() | |
for i := 0; i < 10; i++ { | |
fmt.Println(f()) | |
} | |
} |
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
package main | |
import ( | |
"fmt" | |
"math" | |
"math/cmplx" | |
) | |
func Cbrt(x complex128) complex128 { | |
newton := func(xi complex128, zi complex128) complex128 { | |
return zi - (zi * zi * zi - xi) / (3 * zi * zi) | |
} | |
z1 := complex128(1) | |
z2 := newton(x, z1) | |
for cmplx.Abs(z1 - z2) > float64(1e-15) { | |
z1 = z2 | |
z2 = newton(x, z2) | |
} | |
return z2 | |
} | |
func main() { | |
fmt.Println(Cbrt(27)) | |
fmt.Println(math.Pow(27, (1.0/3.0))) | |
fmt.Println(cmplx.Pow(27, (1.0/3.0))) | |
fmt.Println(Cbrt(5)) | |
fmt.Println(math.Pow(5, (1.0/3.0))) | |
fmt.Println(cmplx.Pow(5, (1.0/3.0))) | |
} | |
/* | |
(3+0i) | |
2.9999999999999996 | |
(2.9999999999999996+0i) | |
(1.709975946676697+0i) | |
1.7099759466766968 | |
(1.7099759466766968+0i) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
tour36 /
powrainbow
の例。きれい?