mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-30 17:38:40 +01:00
highly divisible triangular number
This commit is contained in:
@@ -0,0 +1,54 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
n := 1
|
||||||
|
d := 1
|
||||||
|
for Tau(d) <= 500 {
|
||||||
|
n++
|
||||||
|
d += n
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("num is", d, n)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Tau(num int) int {
|
||||||
|
if num == 1 {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
n := num
|
||||||
|
i := 2
|
||||||
|
p := 1
|
||||||
|
|
||||||
|
for i*i < n {
|
||||||
|
c := 1
|
||||||
|
for n%i == 0 {
|
||||||
|
n /= i
|
||||||
|
c++
|
||||||
|
}
|
||||||
|
i++
|
||||||
|
p *= c
|
||||||
|
}
|
||||||
|
|
||||||
|
if n == num || n > 1 {
|
||||||
|
p *= 1 + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetTriangularNumber(count int) int {
|
||||||
|
ret := count * (count + 1) / 2
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListDivisors(input int) []int {
|
||||||
|
var ret []int
|
||||||
|
for i := 1; i <= input; i++ {
|
||||||
|
if input%i == 0 {
|
||||||
|
ret = append(ret, i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
@@ -0,0 +1,125 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestTau(t *testing.T) {
|
||||||
|
tt := []struct {
|
||||||
|
name string
|
||||||
|
input int
|
||||||
|
want int
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"1",
|
||||||
|
1,
|
||||||
|
1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"2",
|
||||||
|
2,
|
||||||
|
2,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"10",
|
||||||
|
10,
|
||||||
|
4,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"28",
|
||||||
|
28,
|
||||||
|
6,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"76576500",
|
||||||
|
76576500,
|
||||||
|
576,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tt {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
got := Tau(tc.input)
|
||||||
|
if got != tc.want {
|
||||||
|
t.Errorf("Tau(%d)=%d, want %d", tc.input, got, tc.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetTriangularNumber(t *testing.T) {
|
||||||
|
tt := []struct {
|
||||||
|
name string
|
||||||
|
count int
|
||||||
|
want int
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"1",
|
||||||
|
1,
|
||||||
|
1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"2",
|
||||||
|
2,
|
||||||
|
3,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"7",
|
||||||
|
7,
|
||||||
|
28,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"10",
|
||||||
|
10,
|
||||||
|
55,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tt {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
got := GetTriangularNumber(tc.count)
|
||||||
|
if !reflect.DeepEqual(got, tc.want) {
|
||||||
|
t.Errorf("GetTriangularNumber(%d)=%v, want %v", tc.count, got, tc.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestListDivisors(t *testing.T) {
|
||||||
|
tt := []struct {
|
||||||
|
name string
|
||||||
|
input int
|
||||||
|
want []int
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"1",
|
||||||
|
1,
|
||||||
|
[]int{1},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"3",
|
||||||
|
3,
|
||||||
|
[]int{1, 3},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"6",
|
||||||
|
6,
|
||||||
|
[]int{1, 2, 3, 6},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"28",
|
||||||
|
28,
|
||||||
|
[]int{1, 2, 4, 7, 14, 28},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tt {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
got := ListDivisors(tc.input)
|
||||||
|
if !reflect.DeepEqual(got, tc.want) {
|
||||||
|
t.Errorf("ListDivisors(%d)=%v, want %v", tc.input, got, tc.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user