mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-24 21:24:46 +01:00
20210112
This commit is contained in:
@@ -4,15 +4,15 @@ func main() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func search(n int, nums []int) int {
|
func search(target int, nums []int) int {
|
||||||
low, high := 0, len(nums)-1
|
low, high := 0, len(nums)-1
|
||||||
for low <= high {
|
for low <= high {
|
||||||
mid := low + (high-low)/2
|
mid := low + (high-low)/2
|
||||||
if nums[mid] == n {
|
if nums[mid] == target {
|
||||||
return mid
|
return mid
|
||||||
} else if n < nums[mid] {
|
} else if target < nums[mid] {
|
||||||
high = mid - 1
|
high = mid - 1
|
||||||
} else if n > nums[mid] {
|
} else if target > nums[mid] {
|
||||||
low = mid + 1
|
low = mid + 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
69
58_spiral_primes/main.go
Normal file
69
58_spiral_primes/main.go
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
n := 26239
|
||||||
|
for {
|
||||||
|
fmt.Println(n)
|
||||||
|
nums, err := findSprialDiagonals(n)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if primeRatio(nums) < 0.1 {
|
||||||
|
fmt.Println(n)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
n += 2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func findSprialDiagonals(width int) ([]int, error) {
|
||||||
|
if width%2 == 0 {
|
||||||
|
return nil, errors.New("invalid width")
|
||||||
|
}
|
||||||
|
x := 3
|
||||||
|
ret := []int{1}
|
||||||
|
cw := 3
|
||||||
|
d := 2
|
||||||
|
for width/cw > 0 {
|
||||||
|
ret = append(ret, []int{x, x + d, x + 2*d, x + 3*d}...)
|
||||||
|
x = x + 3*d + cw + 1
|
||||||
|
cw += 2
|
||||||
|
d = cw - 1
|
||||||
|
}
|
||||||
|
return ret, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func primeRatio(nums []int) float64 {
|
||||||
|
pc := float64(0)
|
||||||
|
for _, n := range nums {
|
||||||
|
if isPrime(n) {
|
||||||
|
pc++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return pc / float64(len(nums))
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
27
58_spiral_primes/main_test.go
Normal file
27
58_spiral_primes/main_test.go
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func Test_primeRatio(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
nums []int
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want float64
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"test1",
|
||||||
|
args{[]int{2, 3, 5, 8}},
|
||||||
|
0.75,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if got := primeRatio(tt.args.nums); got != tt.want {
|
||||||
|
t.Errorf("primeRatio() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user