Created
March 5, 2019 11:51
-
-
Save LordRahl90/91f1149763bdb58240a211d9176cf7e8 to your computer and use it in GitHub Desktop.
Project Euler #3 Implementation in golang.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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