Skip to content

Conversation

@gchaware
Copy link
Owner

@gchaware gchaware commented Jun 26, 2023

Added prime number function

Summary by CodeRabbit

New Feature:
- Added a `printPrimeNumbers` function to print prime numbers between two given integers

🎉 A prime addition, so fine and neat,
Between two numbers, primes we'll meet.
With every step, the code unfurls,
A wondrous dance of prime-numbered twirls. 🌟

Added prime number function
@coderabbit-nyctonid
Copy link

Image description CodeRabbit

Here is the summary of changes:

  • A new printPrimeNumbers function has been added, which prints prime numbers between two given integers.
  • The main function now calls the printPrimeNumbers function.
File Summary
bundle-server/main.go Added printPrimeNumbers function and called it in the main function.

Chat with Image description CodeRabbit Bot (@coderabbitai)

  • Reply on review comments left by this bot to ask follow-up questions. A review comment is a comment on a diff or a file.
  • Invite the bot into a review comment chain by tagging @coderabbitai in a reply.

Code suggestions

  • The bot may make code suggestions, but please review them carefully before committing since the line number ranges may be misaligned.
  • You can edit the comment made by the bot and manually tweak the suggestion if it is slightly off.

Ignoring further reviews

  • Type @coderabbitai: ignore anywhere in the PR description to ignore further reviews from the bot.


In the recent run, only the files that changed from the base of the PR and between ab560ca055115585bfecf6dd17f46fed4cf614ef and 09dc6322b2122107fb604118f7fcd375a5b03da5 commits were reviewed.

Comment on lines +28 to +47
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()
}

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 to the console. It would be more useful to return a slice of prime numbers instead, so that the caller can decide how to use the results.

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 {
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
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants