-
Notifications
You must be signed in to change notification settings - Fork 0
Update main.go #2
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
Added the function again
| printPrimeNumbers(5,19) | ||
|
|
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.
|
|
||
| } | ||
|
|
||
| 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() | ||
| } |
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 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.
| } | |
| 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 | |
| } |
Added the function again
Summary by CodeRabbit
I have reviewed the summary of changes, and I would like to provide some suggestions to improve the
printPrimeNumbersfunction.Logic: Instead of printing the prime numbers directly within the function, consider returning a slice of prime numbers. This will make the function more reusable and easier to test.
Performance: The current implementation checks if each number is prime by iterating from 2 to the number itself. You can optimize this by only checking up to the square root of the number.
Here's an updated version of the
printPrimeNumbersfunction:This version of the function returns a slice of prime numbers between the given range, and the performance is improved by checking for prime numbers up to the square root of the number.