gpt-4.1-mini-2025-04-14· OpenAI
LMArena Human Preference 140K
Write JavaScript code to find the smallest prime larger than an input
- User
Write JavaScript code to find the smallest prime larger than an input
- Assistant
Certainly! Here's a simple JavaScript function that finds the smallest prime number larger than a given input number:
function isPrime(num) { if (num <= 1) return false; if (num <= 3) return true; if (num % 2 === 0 || num % 3 === 0) return false; for (let i = 5; i * i <= num; i += 6) { if (num % i === 0 || num % (i + 2) === 0) return false; } return true; } function nextPrime(n) { let candidate = n + 1; while (!isPrime(candidate)) { candidate++; } return candidate; } // Example usage: console.log(nextPrime(10)); // Output: 11 console.log(nextPrime(17)); // Output: 19Explanation:
isPrime(num)checks if a number is prime using an efficient method.nextPrime(n)starts fromn + 1and increments until it finds a prime number.
Let me know if you want it optimized or explained further!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.