Problem 5 — Smallest multiple
“2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?”
So…
2520 / 1 remainder 0 2520 / 2 remainder 0 2520 / 3 remainder 0 2520 / 4 remainder 0 2520 / 5 remainder 0 2520 / 6 remainder 0 2520 / 7 remainder 0 2520 / 8 remainder 0 2520 / 9 remainder 0 2520 / 10 remainder 0
answer 232792560
package main
import "fmt"
func main() {
v1()
fmt.Println("--------- v2")
v2()
}
func v2() {
max := 2520
for i := max; ; i++ {
can := true
for j := 1; j <= 20; j++ {
if i%j != 0 {
can = false
break
}
}
if can {
max = i
break
}
}
fmt.Println(max)
}
//===============================
func v1() {
fmt.Println(whatIsRemainder(100, 10))
fmt.Println(whatIsRemainder(99, 10))
for x := 20; true; x++ {
norem := divBy1to20(x)
if norem {
// at the lowest number
fmt.Println("========== ", x)
return
}
}
}
func divBy1to20(x int) bool {
for d := 1; d <= 20; d++ {
_, s := whatIsRemainder(x, d)
if s == false {
return false
}
}
return true
}
func whatIsRemainder(n, denom int) (int, bool) {
remainder := 0
noRemainder := false
remainder = n % denom
if remainder == 0 {
noRemainder = true
} else {
noRemainder = false
}
return remainder, noRemainder
}