Created
October 29, 2019 12:54
-
-
Save alessiosavi/579548cffde67222505455ce3c916290 to your computer and use it in GitHub Desktop.
Calculate the max prime number in Go
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
import ( | |
"log" | |
"math" | |
) | |
// Copied from https://www.geeksforgeeks.org/find-largest-prime-factor-number/ | |
func CalculateMaxPrimeFactor(n int64) int64 { | |
var maxPrime int64 = -1 | |
var i int64 | |
for n%2 == 0 { | |
maxPrime = 2 | |
n >>= 1 // -> n/=2 | |
} | |
for i = 3; float64(i) <= math.Sqrt(float64(n)); i += 2 { | |
for n%i == 0 { | |
maxPrime = i | |
n = n / i | |
} | |
} | |
if n > 2 { | |
maxPrime = n | |
} | |
log.Println(maxPrime) | |
return maxPrime | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment