munch2024 / munch

2 stars 11 forks source link

Duplicate/Code Refactoring Task #70

Closed rparker2003 closed 8 months ago

rparker2003 commented 8 months ago

I chose a code snippet from a previous class of mine that was written in Python. The code is a function that is used to determine if a given number is a prime number and returns a boolean on its primality.

The areas requiring improvement are the redundant comparisons for n == 2, and n % 2 == 0. The goals are to refactor the code by eliminating the redundant comparisons and modifying the code to keep functionality after refactoring.


def is_prime(n):
    if n <= 1:
        return False
    elif n == 2:
        return True
    elif n % 2 == 0:
        return False
    else:
        for i in range(3, int(n**0.5) + 1, 2):
            if n % i == 0:
                return False
        return True```