Skip to content

Instantly share code, notes, and snippets.

@LordRahl90
Created March 5, 2019 11:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LordRahl90/91f1149763bdb58240a211d9176cf7e8 to your computer and use it in GitHub Desktop.
Save LordRahl90/91f1149763bdb58240a211d9176cf7e8 to your computer and use it in GitHub Desktop.
Project Euler #3 Implementation in golang.
package main
import (
"fmt"
)
func primeNumbers(n int, pChan chan int) {
defer close(pChan)
for i := 2; i <= n; i++ {
prime := true
for j := 2; j <= i-1; j++ {
if i%j == 0 {
prime = false
break //since we know this isnt a clean guy
}
}
if prime {
pChan <- i
}
}
}
func solution(n int) int {
pChan := make(chan int)
go primeNumbers(n, pChan)
var max int
for v := range pChan {
if n%v == 0 {
n /= v
if v > max {
max = v
}
}
if n <= 1 {
break
}
}
fmt.Println("Max value is: ", max)
return max
}
func main() {
k := solution(600851475143)
fmt.Println("Largest Prime Factor is: ", k)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment