adventofcode/2017/day23/day23-part2.go

55 lines
765 B
Go

package main
import (
"fmt"
"math"
)
func main() {
var a, b, c, d, e, f, g, h int
// Suppress go warnings
_, _, _, _, _, _, _, _ = a, b, c, d, e, f, g, h
// Derived from test.go
b = 109900
c = 126900
// Unoptimized:
/*
for ; b < c+1; b += 17 {
f = 1
for d = 2; d < b+1; d++ {
for e = 2; e < b+1; e++ {
if d*e == b {
f = 0
}
}
}
// When is f 0?
// when d*e == b for "certain values" of b
fmt.Println(d, e, b)
if f == 0 {
h++
}
// b = 109900
}
*/
// Optimized
for ; b < c+1; b += 17 {
if !IsPrime(b) {
h++
}
}
fmt.Println(h)
}
func IsPrime(value int) bool {
for i := 2; i <= int(math.Floor(float64(value)/2)); i++ {
if value%i == 0 {
return false
}
}
return value > 1
}