From 1e73911dd5317ff61c917060648dedb3b018cfca Mon Sep 17 00:00:00 2001 From: VicRen Date: Mon, 4 Jan 2021 14:36:06 +0800 Subject: [PATCH] 206: Concealed square --- 206_concealed_square/main.go | 39 +++++++++++++++++++++++++++++++ 206_concealed_square/main_test.go | 33 ++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 206_concealed_square/main.go create mode 100644 206_concealed_square/main_test.go diff --git a/206_concealed_square/main.go b/206_concealed_square/main.go new file mode 100644 index 0000000..d1b3964 --- /dev/null +++ b/206_concealed_square/main.go @@ -0,0 +1,39 @@ +package main + +import ( + "fmt" + "math" + "math/big" +) + +func main() { + solution() +} + +func solution() { + 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 isMatch(x.Mul(x, x)) { + fmt.Println(x) + fmt.Println("num is", int64(i)*10) + } + } +} + +func isMatch(i *big.Int) bool { + x := i.String() + num := x[0] + for i := 2; i < len(x); i += 2 { + if x[i] == num+1 { + num++ + continue + } + return false + } + return true +} diff --git a/206_concealed_square/main_test.go b/206_concealed_square/main_test.go new file mode 100644 index 0000000..e314369 --- /dev/null +++ b/206_concealed_square/main_test.go @@ -0,0 +1,33 @@ +package main + +import ( + "math/big" + "testing" +) + +func Test_isMatch(t *testing.T) { + fn, _ := (&big.Int{}).SetString("10203040506070809", 0) + type args struct { + i *big.Int + } + tests := []struct { + name string + args args + want bool + }{ + { + "test1", + args{ + fn, + }, + true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := isMatch(tt.args.i); got != tt.want { + t.Errorf("isMatch() = %v, want %v", got, tt.want) + } + }) + } +}