gpt-4-0613· OpenAI Arena Human Preference 55K
give me a piece of code in golang that calculates the next prime number above 10000
- User
give me a piece of code in golang that calculates the next prime number above 10000
- Assistant
Sure, here's a simple Go code snippet that calculates the next prime number above 10,000:
package main import ( "fmt" "math" ) // Function to check if a number is prime func isPrime(n int) bool { if n <= 1 { return false } else if n <= 3 { return true } else if n%2 == 0 || n%3 == 0 { return false } i := 5 for math.Pow(float64(i), 2) <= float64(n) { if n%i == 0 || n%(i+2) == 0 { return false } i += 6 } return true } // Function to get the next prime number func nextPrime(n int) int { if n <= 1 { return 2 } prime := n found := false // Loop continuously until isPrime returns // true for a number greater than n for !found { prime++ if isPrime(prime) { found = true } } return prime } func main() { fmt.Println(nextPrime(10000)) }This code defines two functions:
isPrimeto check if a number is prime, andnextPrimeto find the next prime number given a number. It then uses these functions in themainfunction to find and print the next prime number above 10,000.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.