Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion bundle-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,29 @@ func main() {
w.Header().Set("Content-Type", "application/gzip")
http.ServeFile(w, r, r.URL.Path[1:])
})

printPrimeNumbers(5,19)
// start HTTP server with `http.DefaultServeMux` handler
log.Fatal(http.ListenAndServe(":9000", nil))

}

func printPrimeNumbers(num1, num2 int){
if num1<2 || num2<2{
fmt.Println("Numbers must be greater than 2 for this to work.")
return
}
for num1 <= num2 {
isPrime := true
for i:=2; i<=int(math.Sqrt(float64(num1))); i++{
if num1 % i == 0{
isPrime = false
break
}
}
if isPrime {
fmt.Printf("Found Prime number: %d ", num1)
}
num1++
}
fmt.Println()
}
Comment on lines +28 to +47

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

The printPrimeNumbers function currently prints the prime numbers directly to the console. It would be more useful if it returned a slice of prime numbers instead. This way, the function can be used in different contexts, such as returning the prime numbers in an HTTP response.

Suggested change
func printPrimeNumbers(num1, num2 int){
if num1<2 || num2<2{
fmt.Println("Numbers must be greater than 2 for this to work.")
return
}
for num1 <= num2 {
isPrime := true
for i:=2; i<=int(math.Sqrt(float64(num1))); i++{
if num1 % i == 0{
isPrime = false
break
}
}
if isPrime {
fmt.Printf("Found Prime number: %d ", num1)
}
num1++
}
fmt.Println()
}
func printPrimeNumbers(num1, num2 int) []int {
primes := []int{}
if num1 < 2 || num2 < 2 {
return primes
}
for num1 <= num2 {
isPrime := true
for i := 2; i <= int(math.Sqrt(float64(num1))); i++ {
if num1 % i == 0 {
isPrime = false
break
}
}
if isPrime {
primes = append(primes, num1)
}
num1++
}
return primes
}