From 33e5d4b47ecdb3eff37bcfaa641f3a7bb8997eea Mon Sep 17 00:00:00 2001 From: VicRen Date: Wed, 25 Nov 2020 15:17:34 +0800 Subject: [PATCH] 42: update test --- 42_coded_triangle_numbers/main.go | 11 ++++------- 42_coded_triangle_numbers/main_test.go | 10 ++++++++++ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/42_coded_triangle_numbers/main.go b/42_coded_triangle_numbers/main.go index c607109..b52ecd5 100644 --- a/42_coded_triangle_numbers/main.go +++ b/42_coded_triangle_numbers/main.go @@ -3,6 +3,7 @@ package main import ( "fmt" "io/ioutil" + "math" "strings" ) @@ -40,7 +41,7 @@ func worthOf(word string) int { sum := 0 for i := 0; i < len(word); i++ { a := word[i : i+1][0] - w := uint8(a) - BASE + w := a - 'A' + 1 sum += int(w) } return sum @@ -49,12 +50,8 @@ func worthOf(word string) int { func isTriangleNumber(n int) bool { //tn = ½n(n+1) x := 2 * n - res := x - //牛顿法求平方根 - for res*res > x { - res = (res + x/res) / 2 - } - for i := res; i > 0; i-- { + r := int(math.Sqrt(float64(x))) + for i := r; i > 0; i-- { if i*(i+1) == x { return true } diff --git a/42_coded_triangle_numbers/main_test.go b/42_coded_triangle_numbers/main_test.go index cf81ffb..964bc98 100644 --- a/42_coded_triangle_numbers/main_test.go +++ b/42_coded_triangle_numbers/main_test.go @@ -86,6 +86,16 @@ func Test_isTriangleNumber(t *testing.T) { args{29}, false, }, + { + "55", + args{55}, + true, + }, + { + "56", + args{56}, + false, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {