Skip to content

Instantly share code, notes, and snippets.

@danackerson
Created December 27, 2014 12:32
Show Gist options
  • Save danackerson/3b3573211a7e79f8705d to your computer and use it in GitHub Desktop.
Save danackerson/3b3573211a7e79f8705d to your computer and use it in GitHub Desktop.
Perfect Squares
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