Skip to content

Instantly share code, notes, and snippets.

@hayamiz
Created October 22, 2014 15:09
Show Gist options
  • Save hayamiz/4e60fab3e364dfffff56 to your computer and use it in GitHub Desktop.
Save hayamiz/4e60fab3e364dfffff56 to your computer and use it in GitHub Desktop.
//usr/bin/env go run $0 $@ ; exit
package main
import (
"fmt"
"math"
)
func factor(n int) (int, int) {
sqrt_n := int(math.Sqrt(float64(n)) + 0.1)
if n & 1 == 0 {
return 2, n / 2
} else {
for p := 3; p <= sqrt_n; p += 2 {
if n % p == 0 {
return p, n / p
}
}
}
return 1, n
}
func main() {
max_p := 0
n := 600851475143
for n > 1{
var p int
p, n = factor(n)
if p == 1 {
if n > max_p {
max_p = n
}
break
}
if p > max_p {
max_p = p
}
}
fmt.Println(max_p)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment