Last active
November 26, 2020 11:11
-
-
Save jmbrunskill/48c640aedd79a84bd7a08c4b0069c040 to your computer and use it in GitHub Desktop.
Solution to Matt Parkers Math Puzzles # 19 using Go- http://www.think-maths.co.uk/19challenge
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 | |
//Matt Parkers Math Puzzles number 19 | |
//Find an integer N for which the the sum of the squares of the first N primes is divisible by N and N is greater than 19 | |
func main() { | |
//Shamelessly use a precalculated list of 38 primes | |
primes := [38]int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163} | |
sumOfSquares := 0 | |
//Loop through the primes to create the sum of the squares | |
for i, p := range primes { | |
//add the square of the prime | |
sumOfSquares += (p * p) | |
//Check if remainder is 0 after dividing by i+1 (as i starts from 0) | |
if i > 19 && sumOfSquares%(i+1) == 0 { | |
println("N:", i+1) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://godoc.org/github.com/jbarham/primegen.go might be useful if you wanted to extend this code to find more solutions