package main
import (
"fmt"
)
func largestPrimeFactor(n int) int {
div := 2
maxPFact := -1
for n != 0 {
if n%div != 0 {
div++
} else {
maxPFact = n
n /= div // Integer division
if n == 1 {
break
}
}
}
return maxPFact
}
func main() {
fmt.Println(largestPrimeFactor(124)) // 2 x 2 x 31
fmt.Println(largestPrimeFactor(288)) // 2 x 2 x 2 x 2 x 2 x 3 x 3
fmt.Println(largestPrimeFactor(1288)) // 2 x 2 x 2 x 7 x 23
fmt.Println(largestPrimeFactor(100000000)) // 2, 2, 2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5, 5, 5
}
/*
run:
31
3
23
5
*/