57 lines
805 B
Go
57 lines
805 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"bufio"
|
||
|
"fmt"
|
||
|
"log"
|
||
|
"os"
|
||
|
"strconv"
|
||
|
)
|
||
|
|
||
|
func loadPrimes() []int64 {
|
||
|
return loadPrimesTxt()
|
||
|
}
|
||
|
|
||
|
func loadPrimesTxt() []int64 {
|
||
|
file, err := os.Open("prime.txt")
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
|
||
|
var primes []int64
|
||
|
|
||
|
scanner := bufio.NewScanner(file)
|
||
|
|
||
|
for scanner.Scan() {
|
||
|
nextPrime, err := strconv.ParseInt(scanner.Text(), 10, 64)
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
primes = append(primes, nextPrime)
|
||
|
}
|
||
|
|
||
|
file.Close()
|
||
|
|
||
|
return primes
|
||
|
}
|
||
|
|
||
|
func writePrimes(primes []int64) {
|
||
|
writePrimesTxt(primes)
|
||
|
}
|
||
|
|
||
|
func writePrimesTxt(primes []int64) {
|
||
|
file, err := os.Create("prime.txt")
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
|
||
|
writer := bufio.NewWriter(file)
|
||
|
|
||
|
for _, prime := range primes {
|
||
|
writer.WriteString(fmt.Sprintf("%d\n", prime))
|
||
|
}
|
||
|
|
||
|
writer.Flush()
|
||
|
file.Close()
|
||
|
}
|