Skip to content
TheChatArchiveThe Chat Archive
GLMGLM-5 / Pony Alpha· unknownHugging Face Frontier Reasoning Traces

Write a Python function to check if a number is a prime.

  1. User

    Write a Python function to check if a number is a prime.

  2. GLM Assistant

    We need to write a Python function to check if a number is prime. The user probably expects a straightforward implementation, but we need to ensure it's correct and efficient for reasonable numbers. We'll produce a function that takes an integer n and returns True if prime, else False. We'll handle edge cases: numbers less than 2 are not prime. For n=2, it's prime. For even numbers >2, not prime. Then we can check odd divisors up to sqrt(n). We'll use math.isqrt for integer sqrt.

    We can also implement a simple trial division.

    We need to output the function only? The instruction: "Write a Python function to check if a number is a prime." So we should output the code.

    We'll produce a clean function with docstring.

    We can also add type hints.

    I'll write:

    import math

    def is_prime(n: int) -> bool: """Return True if n is a prime number, else False.""" if n < 2: return False if n == 2: return True if n % 2 == 0: return False limit = math.isqrt(n) for i in range(3, limit + 1, 2): if n % i == 0: return False return True

    That's standard.

    Alternatively, we could include handling of small primes. But this is fine.

    We'll output the code block.

    Make sure to import math inside the function? Usually import at top.

    So final answer: Provide the Python function.

    We might also include a note about efficiency.

    But as per instruction, just the function.

    Thus, final output:

    import math
    
    def is_prime(n: int) -> bool:
        """Check if a number is prime.
    
        Args:
            n: Integer to check.
    
        Returns:
            True if n is prime, False otherwise.
        """
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        limit = math.isqrt(n)
        for i in range(3, limit + 1, 2):
            if n % i == 0:
                return False
        return True
    

    We could also include a main guard for testing but not necessary.

    We'll produce that.

    
    import math
    
    def is_prime(n: int) -> bool:
        """Check if a number is prime.
    
        Args:
            n: Integer to check.
    
        Returns:
            True if n is prime, False otherwise.
        """
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        limit = math.isqrt(n)
        for i in range(3, limit + 1, 2):
            if n % i == 0:
                return False
        return True
    
    

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

Write a Python function to check if a number is a prime. · The Chat Archive