mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-24 22:54:43 +01:00
37: truncatable primes
This commit is contained in:
68
37_truncatable_primes/main.go
Normal file
68
37_truncatable_primes/main.go
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
sum := 0
|
||||||
|
start := 8
|
||||||
|
count := 0
|
||||||
|
for {
|
||||||
|
if count == 11 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if isTruncatablePrimesL2R(start) && isTruncatablePrimesR2L(start) {
|
||||||
|
count++
|
||||||
|
fmt.Println("tp:", start)
|
||||||
|
sum += start
|
||||||
|
}
|
||||||
|
start++
|
||||||
|
}
|
||||||
|
fmt.Println("sum:", sum)
|
||||||
|
}
|
||||||
|
|
||||||
|
func isTruncatablePrimesL2R(n int) bool {
|
||||||
|
s := strconv.Itoa(n)
|
||||||
|
for len(s) > 0 {
|
||||||
|
x, _ := strconv.Atoi(s)
|
||||||
|
if !isPrime(x) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
s = s[1:]
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func isTruncatablePrimesR2L(n int) bool {
|
||||||
|
s := strconv.Itoa(n)
|
||||||
|
for len(s) > 0 {
|
||||||
|
l := len(s)
|
||||||
|
x, _ := strconv.Atoi(s)
|
||||||
|
if !isPrime(x) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
s = s[:l-1]
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func isPrime(n int) bool {
|
||||||
|
if n <= 1 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
res := n
|
||||||
|
//牛顿法求平方根
|
||||||
|
for res*res > n {
|
||||||
|
res = (res + n/res) / 2
|
||||||
|
}
|
||||||
|
divider := 2
|
||||||
|
for divider <= res {
|
||||||
|
if n%divider == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
divider++
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
83
37_truncatable_primes/main_test.go
Normal file
83
37_truncatable_primes/main_test.go
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_isTruncatablePrimesL2R(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
n int
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"3797",
|
||||||
|
args{3797},
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"797",
|
||||||
|
args{797},
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"97",
|
||||||
|
args{97},
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"96",
|
||||||
|
args{96},
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if got := isTruncatablePrimesL2R(tt.args.n); got != tt.want {
|
||||||
|
t.Errorf("isTruncatablePrimesL2R() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_isTruncatablePrimesR2L(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
n int
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"3797",
|
||||||
|
args{3797},
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"379",
|
||||||
|
args{379},
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"37",
|
||||||
|
args{37},
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"96",
|
||||||
|
args{96},
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if got := isTruncatablePrimesR2L(tt.args.n); got != tt.want {
|
||||||
|
t.Errorf("isTruncatablePrimesR2L() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user