mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-24 21:24:46 +01:00
35: circular primes
This commit is contained in:
70
35_circular_primes/main.go
Normal file
70
35_circular_primes/main.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
saved := map[int]struct{}{}
|
||||
for i := 2; i < 1e6; i++ {
|
||||
fmt.Println("i:", i)
|
||||
if _, ok := saved[i]; ok {
|
||||
continue
|
||||
}
|
||||
ns := circularNumbersOf(i)
|
||||
tmpMap := map[int]struct{}{}
|
||||
for _, n := range ns {
|
||||
if isPrime(n) {
|
||||
tmpMap[n] = struct{}{}
|
||||
}
|
||||
}
|
||||
if len(tmpMap) == len(ns) {
|
||||
for k := range tmpMap {
|
||||
saved[k] = struct{}{}
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Println("num:", len(saved), saved)
|
||||
}
|
||||
|
||||
func circularNumbersOf(n int) []int {
|
||||
var ret []int
|
||||
retMap := map[int]struct{}{}
|
||||
ns := strconv.Itoa(n)
|
||||
ret = append(ret, n)
|
||||
retMap[n] = struct{}{}
|
||||
l := len(ns)
|
||||
if l < 2 {
|
||||
return ret
|
||||
}
|
||||
for i := 1; i < l; i++ {
|
||||
ns = ns[1:] + ns[:1]
|
||||
num, _ := strconv.Atoi(ns)
|
||||
if _, ok := retMap[num]; ok {
|
||||
continue
|
||||
}
|
||||
retMap[num] = struct{}{}
|
||||
ret = append(ret, num)
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
104
35_circular_primes/main_test.go
Normal file
104
35_circular_primes/main_test.go
Normal file
@@ -0,0 +1,104 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_isPrime(t *testing.T) {
|
||||
type args struct {
|
||||
n int
|
||||
}
|
||||
tt := []struct {
|
||||
name string
|
||||
args args
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
"1",
|
||||
args{1},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"2",
|
||||
args{2},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"4",
|
||||
args{4},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"17",
|
||||
args{17},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"37",
|
||||
args{37},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"73",
|
||||
args{73},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"13 * 3 * 5 * 5 * 17 * 31 * 737",
|
||||
args{3 * 3 * 5 * 5 * 17 * 31 * 73},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"large_one",
|
||||
args{2876*2876 - 63*2876 + 977},
|
||||
false,
|
||||
},
|
||||
}
|
||||
for _, tc := range tt {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if got := isPrime(tc.args.n); got != tc.want {
|
||||
t.Errorf("isPrime() = %v, want %v", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_circularNumbersOf(t *testing.T) {
|
||||
type args struct {
|
||||
n int
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want []int
|
||||
}{
|
||||
{
|
||||
"197",
|
||||
args{197},
|
||||
[]int{197, 971, 719},
|
||||
},
|
||||
{
|
||||
"19",
|
||||
args{19},
|
||||
[]int{19, 91},
|
||||
},
|
||||
{
|
||||
"11",
|
||||
args{11},
|
||||
[]int{11},
|
||||
},
|
||||
{
|
||||
"12345",
|
||||
args{12345},
|
||||
[]int{12345, 23451, 34512, 45123, 51234},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := circularNumbersOf(tt.args.n); !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("circularNumbersOf() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user