Last active
May 30, 2018 10:25
-
-
Save kidandcat/20b576dc0702619c2089a56245b414cc to your computer and use it in GitHub Desktop.
go run prime.go NumberOfDigits
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 ( | |
"fmt" | |
"os" | |
"strconv" | |
) | |
func main() { | |
// RSA-2048 is a decimal number of 617 digits | |
numDigits, err := strconv.Atoi(os.Args[1]) | |
if err != nil { | |
panic(err) | |
} | |
m := makeMatrix(digitNumberToInt(numDigits)) | |
purge(m, 0) | |
} | |
func digitNumberToInt(digits int) int { | |
res := "" | |
for i := 0; i < digits; i++ { | |
fmt.Println("Composing number:", i) | |
res = res + "9" | |
} | |
fmt.Println("Converting string to number", res) | |
r, err := strconv.Atoi(res) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println("Done") | |
return r | |
} | |
func purge(m []int, n int) { | |
if n%1000 == 0 { | |
fmt.Println("Purge iteration:", n, "/", len(m)) | |
} | |
if len(m) == n { | |
for _, n := range m { | |
if n != 0 { | |
fmt.Print(strconv.Itoa(n) + " ") | |
} | |
} | |
fmt.Println() | |
os.Exit(0) | |
} | |
if m[n] < 2 || m[n] == 0 { | |
purge(m, n+1) | |
} | |
for i := 0; i < len(m); i++ { | |
if m[i] != 0 && m[i] != m[n] && m[i]%m[n] == 0 { | |
m[i] = 0 | |
} | |
} | |
purge(m, n+1) | |
} | |
func makeMatrix(num int) []int { | |
matrix := []int{} | |
for i := 2; i < num; i++ { | |
if i%1000 == 0 { | |
fmt.Println("Initializing matrix:", i, "/", num) | |
} | |
if i%2 != 0 { | |
matrix = append(matrix, i) | |
} | |
} | |
return matrix | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment