mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-24 19:54:43 +01:00
prime factors 2021
I'm back
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
.idea/*
|
||||||
17
prime_factors_2021/prime_factors.go
Normal file
17
prime_factors_2021/prime_factors.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
func PrimeFactorsOf(n int) []int {
|
||||||
|
var ret = make([]int, 0)
|
||||||
|
var divider = 2
|
||||||
|
for divider < n {
|
||||||
|
for n % divider == 0 {
|
||||||
|
ret = append(ret, divider)
|
||||||
|
n = n/divider
|
||||||
|
}
|
||||||
|
divider++
|
||||||
|
}
|
||||||
|
if n > 1 {
|
||||||
|
ret = append(ret, n)
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
68
prime_factors_2021/prime_factors_test.go
Normal file
68
prime_factors_2021/prime_factors_test.go
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestPrimeFactorsOf(t *testing.T) {
|
||||||
|
tt := []struct{
|
||||||
|
name string
|
||||||
|
input int
|
||||||
|
want []int
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"1",
|
||||||
|
1,
|
||||||
|
[]int{},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"2",
|
||||||
|
2,
|
||||||
|
[]int{2},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"3",
|
||||||
|
3,
|
||||||
|
[]int{3},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"4",
|
||||||
|
4,
|
||||||
|
[]int{2, 2},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"5",
|
||||||
|
5,
|
||||||
|
[]int{5},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"6",
|
||||||
|
6,
|
||||||
|
[]int{2, 3},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"7",
|
||||||
|
7,
|
||||||
|
[]int{7},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"8",
|
||||||
|
8,
|
||||||
|
[]int{2, 2, 2},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"9",
|
||||||
|
9,
|
||||||
|
[]int{3, 3},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tc := range tt {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
got := PrimeFactorsOf(tc.input)
|
||||||
|
if !reflect.DeepEqual(got, tc.want) {
|
||||||
|
t.Errorf("PrimeFactorsOf %d = %v, want %v", tc.input, got, tc.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user