From 9383aba292819d13a3140d5e277c548c3bd312b4 Mon Sep 17 00:00:00 2001 From: VicRen Date: Wed, 6 Jan 2021 08:47:13 +0800 Subject: [PATCH] update euler 206 --- 206_concealed_square/main.go | 21 ++++++++++-- 206_concealed_square/main_test.go | 55 +++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 3 deletions(-) diff --git a/206_concealed_square/main.go b/206_concealed_square/main.go index d1b3964..ab60eb8 100644 --- a/206_concealed_square/main.go +++ b/206_concealed_square/main.go @@ -4,6 +4,7 @@ import ( "fmt" "math" "math/big" + "time" ) func main() { @@ -11,20 +12,34 @@ func main() { } func solution() { + ts := time.Now() n := math.Sqrt(19293949596979899) t := math.Sqrt(10203040506070809) for i := n; i >= t; i-- { - if i == 1389019170 { - fmt.Println(i) - } x := (&big.Int{}).SetInt64(int64(i)) + if !isEndsWith37(x.String()) { + continue + } if isMatch(x.Mul(x, x)) { fmt.Println(x) fmt.Println("num is", int64(i)*10) + fmt.Println(time.Since(ts)) + break } } } +func isEndsWith37(s string) bool { + if len(s) == 0 { + return false + } + c := s[len(s)-1] + if c == '3' || c == '7' { + return true + } + return false +} + func isMatch(i *big.Int) bool { x := i.String() num := x[0] diff --git a/206_concealed_square/main_test.go b/206_concealed_square/main_test.go index e314369..0357ac1 100644 --- a/206_concealed_square/main_test.go +++ b/206_concealed_square/main_test.go @@ -7,6 +7,7 @@ import ( func Test_isMatch(t *testing.T) { fn, _ := (&big.Int{}).SetString("10203040506070809", 0) + fn2, _ := (&big.Int{}).SetString("10203040506070808", 0) type args struct { i *big.Int } @@ -22,6 +23,13 @@ func Test_isMatch(t *testing.T) { }, true, }, + { + "test2", + args{ + fn2, + }, + false, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -31,3 +39,50 @@ func Test_isMatch(t *testing.T) { }) } } + +func Test_isEndsWith37(t *testing.T) { + type args struct { + s string + } + tests := []struct { + name string + args args + want bool + }{ + { + "test1", + args{ + "123", + }, + true, + }, + { + "test2", + args{ + "127", + }, + true, + }, + { + "test3", + args{ + "12", + }, + false, + }, + { + "test4", + args{ + "", + }, + false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := isEndsWith37(tt.args.s); got != tt.want { + t.Errorf("isEndsWith37() = %v, want %v", got, tt.want) + } + }) + } +}