mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-24 23:04:42 +01:00
42: update test
This commit is contained in:
@@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"math"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -40,7 +41,7 @@ func worthOf(word string) int {
|
|||||||
sum := 0
|
sum := 0
|
||||||
for i := 0; i < len(word); i++ {
|
for i := 0; i < len(word); i++ {
|
||||||
a := word[i : i+1][0]
|
a := word[i : i+1][0]
|
||||||
w := uint8(a) - BASE
|
w := a - 'A' + 1
|
||||||
sum += int(w)
|
sum += int(w)
|
||||||
}
|
}
|
||||||
return sum
|
return sum
|
||||||
@@ -49,12 +50,8 @@ func worthOf(word string) int {
|
|||||||
func isTriangleNumber(n int) bool {
|
func isTriangleNumber(n int) bool {
|
||||||
//tn = ½n(n+1)
|
//tn = ½n(n+1)
|
||||||
x := 2 * n
|
x := 2 * n
|
||||||
res := x
|
r := int(math.Sqrt(float64(x)))
|
||||||
//牛顿法求平方根
|
for i := r; i > 0; i-- {
|
||||||
for res*res > x {
|
|
||||||
res = (res + x/res) / 2
|
|
||||||
}
|
|
||||||
for i := res; i > 0; i-- {
|
|
||||||
if i*(i+1) == x {
|
if i*(i+1) == x {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -86,6 +86,16 @@ func Test_isTriangleNumber(t *testing.T) {
|
|||||||
args{29},
|
args{29},
|
||||||
false,
|
false,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"55",
|
||||||
|
args{55},
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"56",
|
||||||
|
args{56},
|
||||||
|
false,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user