Skip to content

Fix the primality testing logic for integer arguments less than 2 #4

@someCoder-a11y

Description

@someCoder-a11y

The primality test returns 1 for integer arguments less than 2.

The current Python logic for checking if a number is prime (1 if all(a % k for k in range(2, a)) else 0) returns incorrect results for integers less than 2. This is because for integer a < 2, range(2, a) is an empty list, so the all gets no values to check, making it evaluate to True, so the ternary if statement chooses the 1, because True is truthy. Therefore, whenever "p" is called with an integer argument less than 2, 1 is pushed onto the stack, wrongly indicating that it's prime. I'd recommend putting an a < 2 check to make it work properly with all integers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions