Skip to content
TheChatArchiveThe Chat Archive
gpt-4.1-mini-2025-04-14· OpenAIHugging Face LMArena Human Preference 140K

Write JavaScript code to find the smallest prime larger than an input

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with grok-3-preview-02-24’s answer.
  1. User

    Write JavaScript code to find the smallest prime larger than an input

  2. 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: 19
    

    Explanation:

    • isPrime(num) checks if a number is prime using an efficient method.
    • nextPrime(n) starts from n + 1 and increments until it finds a prime number.

    Let me know if you want it optimized or explained further!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.