Created
December 27, 2014 12:32
-
-
Save danackerson/3b3573211a7e79f8705d to your computer and use it in GitHub Desktop.
Perfect Squares
This file contains 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 ( | |
"math" | |
"math/big" | |
"fmt" | |
) | |
// change prime() to main() for execution => can't have 2 main()'s and go test | |
func main(){ | |
index := 0 | |
maxNums := int64(1000000) | |
primes := [][]int64{} | |
perfectSquare := 1 | |
for value := int64(1); value < maxNums; value++ { | |
i := big.NewInt(value) | |
isPrime := i.ProbablyPrime(1) | |
if isPrime { | |
square := int64(math.Pow(float64(value),2)) | |
newPrime := []int64{ value, square, 0 } | |
primes = append(primes, newPrime) | |
if index > 0 { | |
diffPlusOne := primes[index][1] - primes[index - 1][1] + 1 | |
root_of_diff := math.Sqrt(float64(diffPlusOne)) | |
integerPart, decimalPart := math.Modf(root_of_diff) | |
if decimalPart == 0 { | |
primes[index][2] = int64(integerPart) | |
fmt.Printf("%d: squareRoot gives %d with %f remainder\n", value, int(integerPart), decimalPart) | |
perfectSquare++ | |
} else { | |
primes[index][2] = int64(-1) | |
} | |
} | |
index++ | |
} | |
} | |
fmt.Printf("%d/%d are perfect roots\n", perfectSquare, maxNums) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment