mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-24 23:04:42 +01:00
47: distinct primes factors
This commit is contained in:
71
47_distinct_primes_factors/main.go
Normal file
71
47_distinct_primes_factors/main.go
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
"sort"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
nums := make([]int, 0)
|
||||||
|
i := 2
|
||||||
|
for {
|
||||||
|
primes := distinctPrimesOf(i)
|
||||||
|
if len(primes) == 4 {
|
||||||
|
l := len(nums)
|
||||||
|
if l == 0 || (nums[l-1] == i-1) {
|
||||||
|
nums = append(nums, i)
|
||||||
|
} else {
|
||||||
|
nums = []int{i}
|
||||||
|
}
|
||||||
|
if len(nums) == 4 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
fmt.Println(nums)
|
||||||
|
}
|
||||||
|
|
||||||
|
func distinctPrimesOf(n int) []int {
|
||||||
|
m := make(map[int]struct{})
|
||||||
|
max := int(math.Sqrt(float64(n)))
|
||||||
|
divider := 2
|
||||||
|
for divider <= max {
|
||||||
|
if n%divider == 0 {
|
||||||
|
if isPrime(divider) {
|
||||||
|
m[divider] = struct{}{}
|
||||||
|
}
|
||||||
|
x := n / divider
|
||||||
|
if isPrime(x) {
|
||||||
|
m[x] = struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
divider++
|
||||||
|
}
|
||||||
|
var ret []int
|
||||||
|
for k := range m {
|
||||||
|
ret = append(ret, k)
|
||||||
|
}
|
||||||
|
sort.Ints(ret)
|
||||||
|
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
|
||||||
|
}
|
||||||
40
47_distinct_primes_factors/main_test.go
Normal file
40
47_distinct_primes_factors/main_test.go
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_distinctPrimesOf(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
n int
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want []int
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"4",
|
||||||
|
args{4},
|
||||||
|
[]int{2},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"14",
|
||||||
|
args{14},
|
||||||
|
[]int{2, 7},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"644",
|
||||||
|
args{644},
|
||||||
|
[]int{2, 7, 23},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if got := distinctPrimesOf(tt.args.n); !reflect.DeepEqual(got, tt.want) {
|
||||||
|
t.Errorf("distinctPrimesOf() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user