-
Notifications
You must be signed in to change notification settings - Fork 0
Update main.go #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
| printPrimeNumbers(5,19) | ||
| // start HTTP server with `http.DefaultServeMux` handler |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The printPrimeNumbers function is called directly in the main function. It would be better to move this call to a separate function or an HTTP handler to improve modularity and maintainability.
| printPrimeNumbers(5,19) | |
| // start HTTP server with `http.DefaultServeMux` handler | |
| func main() { | |
| http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | |
| http.ServeFile(w, r, r.URL.Path[1:]) | |
| }) | |
| http.HandleFunc("/prime", primeHandler) | |
| // start HTTP server with `http.DefaultServeMux` handler | |
| log.Fatal(http.ListenAndServe(":9000", nil)) | |
| } | |
| func primeHandler(w http.ResponseWriter, r *http.Request) { | |
| printPrimeNumbers(5, 19) | |
| } |
| 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() | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The printPrimeNumbers function currently prints the prime numbers directly to the console. To make it more reusable, consider returning a slice of prime numbers instead of printing them. This way, the function can be used in different contexts, such as returning the prime numbers in an API response or processing them further.
| 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 { | |
| if num1 < 2 || num2 < 2 { | |
| fmt.Println("Numbers must be greater than 2 for this to work.") | |
| return nil | |
| } | |
| primes := []int{} | |
| 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 | |
| } |
Added Prime Number Function
Summary by CodeRabbit
Thank you for providing the summary of changes. Please share the code snippet or the relevant diff, so I can review it and provide suggestions to improve the code quality.