Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "strconv"
- "time"
- "math"
- )
- func main() {
- var input string
- var lim int
- var nums []bool
- var s, n, m int
- fmt.Print("Enter limit for this search: ")
- fmt.Scanln(&input)
- lim, _ = strconv.Atoi(input)
- if lim == 0 {
- lim = 100000000
- }
- s = int(math.Sqrt(float64(lim)))
- startTime := time.Now()
- nums = make([]bool, lim + 1)
- for n = 2; n < s; n++ {
- if nums[n] == false {
- m = n * n
- for m <= lim {
- nums[m] = true
- m += n
- }
- }
- }
- endTime := time.Since(startTime)
- fmt.Println("It took ", endTime, " time")
- fmt.Print("Do you want to list results (Y/N)?")
- fmt.Scanln(&input)
- if input == "Y" || input == "y" {
- fmt.Println("The primes up to ", lim, " are:")
- } else {
- fmt.Println("(please wait for prime count)")
- }
- m = 0
- for n = 2; n < lim; n++ {
- if nums[n] == false {
- if input == "Y" || input == "y" {
- fmt.Println(n)
- }
- m++
- }
- }
- fmt.Println("")
- fmt.Println("Total primes = ", m)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement