Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement the sieve of Eratosthenes as a Go program: a method for computing prime numbers, known to the ancient Greeks. This method will compute all

Implement the sieve of Eratosthenes as a Go program: a method for computing prime numbers, known to the ancient Greeks.
This method will compute all prime numbers up to n.
Choose an n (i.e., via user input).
First insert all numbers from 2 to n into a set.
Then erase all multiples of 2(except 2); that is,4,6,8,10,12 package main
import (
"fmt"
)
func main(){
var n int
fmt.Print("Enter a positive integer: ")
fmt.Scan(&n)
set :=[]int{}
Divide2 :=[]int{}
for i :=2; i <= n; i++{
set = append(set, i)
}
for _, num := range set {
if isPrime(num, len(set)){
Divide2= append(Divide2, num)
}
}
fmt.Println("Prime numbers up to", n, "are:", Divide2)
}
func isPrime(n int, size int) bool {
if n <2{
return false
}
for i :=2; i*i < size; i++{
if n%i !=0{
return true
}
}
return false
}
Go up to n.
Fix the code for GO

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What is the purpose of the statement of cash flows?

Answered: 1 week ago

Question

How would we like to see ourselves?

Answered: 1 week ago