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: 23 additions & 0 deletions bundle-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,30 @@ func main() {
http.ServeFile(w, r, r.URL.Path[1:])
})

printPrimeNumbers(5,19)

Comment on lines +23 to +24

Choose a reason for hiding this comment

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

Image description CodeRabbit

It's not a good practice to call printPrimeNumbers directly in the main function. Instead, consider creating an HTTP handler for this functionality and use it as an API endpoint.

// 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.")
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("%d ", num1)
}
num1++
}
fmt.Println()
}
Comment on lines 27 to +49

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 can be improved by separating the prime number checking logic into a separate function called isPrime. This will make the code more modular and easier to maintain. Additionally, instead of printing the prime numbers directly, consider returning them as a slice of integers.

Suggested change
}
func printPrimeNumbers(num1, num2 int){
if num1<2 || num2<2{
fmt.Println("Numbers must be greater than 2.")
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("%d ", num1)
}
num1++
}
fmt.Println()
}
func main() {
// ...
http.HandleFunc("/prime-numbers", func(w http.ResponseWriter, r *http.Request) {
primeNumbers := getPrimeNumbers(5, 19)
fmt.Fprintf(w, "Prime numbers: %v", primeNumbers)
})
// ...
}
func getPrimeNumbers(num1, num2 int) []int {
if num1 < 2 || num2 < 2 {
return []int{}
}
var primeNumbers []int
for num1 <= num2 {
if isPrime(num1) {
primeNumbers = append(primeNumbers, num1)
}
num1++
}
return primeNumbers
}
func isPrime(num int) bool {
if num < 2 {
return false
}
for i := 2; i <= int(math.Sqrt(float64(num))); i++ {
if num%i == 0 {
return false
}
}
return true
}