mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-24 22:34:41 +01:00
smallest multiple
This commit is contained in:
23
smallest_multiple/main.go
Normal file
23
smallest_multiple/main.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
n := 20
|
||||||
|
for {
|
||||||
|
if IsDivisibleFor1to20(n) {
|
||||||
|
fmt.Println("evenly divisible by all of the numbers from 1 to 20:", n)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
n++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func IsDivisibleFor1to20(n int) bool {
|
||||||
|
for i := 1; i <= 20; i++ {
|
||||||
|
if n%i != 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
31
smallest_multiple/smallest_multiple_test.go
Normal file
31
smallest_multiple/smallest_multiple_test.go
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestIsDivisibleFor1to20(t *testing.T) {
|
||||||
|
tt := []struct {
|
||||||
|
name string
|
||||||
|
input int
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"20",
|
||||||
|
20,
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"14",
|
||||||
|
14,
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tt {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
got := IsDivisibleFor1to20(tc.input)
|
||||||
|
if got != tc.want {
|
||||||
|
t.Errorf("IsDivisibleFor1to20(%d)=%v, want %v", tc.input, got, tc.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user