mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-24 20:14:45 +01:00
44: pentagon numbers
This commit is contained in:
36
44_pentagon_numbers/main.go
Normal file
36
44_pentagon_numbers/main.go
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
j := 1
|
||||||
|
f := true
|
||||||
|
for f {
|
||||||
|
a := j * (3*j - 1) / 2
|
||||||
|
for k := j + 1; k < 5000; k++ {
|
||||||
|
b := k * (3*k - 1) / 2
|
||||||
|
d := b - a
|
||||||
|
if isPentagonalNumber(d) && isPentagonalNumber(a+b) {
|
||||||
|
fmt.Println(j, k, a, b, d, a+b)
|
||||||
|
f = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
j++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func pentagonalNumberN(n int) int {
|
||||||
|
return n * (3*n - 1) / 2
|
||||||
|
}
|
||||||
|
|
||||||
|
func isPentagonalNumber(n int) bool {
|
||||||
|
r := math.Sqrt(float64(1 + 24*n))
|
||||||
|
if r-float64(int(r)) != 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return int(r)%6 == 5
|
||||||
|
}
|
||||||
81
44_pentagon_numbers/main_test.go
Normal file
81
44_pentagon_numbers/main_test.go
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func Test_pentagonalNumberN(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
n int
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want int
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"1",
|
||||||
|
args{1},
|
||||||
|
1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"2",
|
||||||
|
args{2},
|
||||||
|
5,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"10",
|
||||||
|
args{10},
|
||||||
|
145,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if got := pentagonalNumberN(tt.args.n); got != tt.want {
|
||||||
|
t.Errorf("pentagonalNumberN() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_isPentagonalNumber(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
n int
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"1",
|
||||||
|
args{1},
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"5",
|
||||||
|
args{5},
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"6",
|
||||||
|
args{6},
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"120",
|
||||||
|
args{120},
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"145",
|
||||||
|
args{145},
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if got := isPentagonalNumber(tt.args.n); got != tt.want {
|
||||||
|
t.Errorf("isPentagonalNumber() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user