Is_prime - feedback on my different approach


#1



Hi all!

I'm quite new and I came up with a slightly different approach to this problem. Essentially it creates a list of divisors, checks to see if that list contains 2 items(1 and x) and if so returns True, else returns False.

I was hoping to get some feedback from the more experienced here. The solution worked but I'm not sure if this was a good way to solve.

Thanks


def is_prime(x):
    if x < 2:
        return False
    elif x == 2:
        return True
    else:
        num = x
        lis = range(1,num +1)
        divi = []
        for i in lis:
            if num % i == 0:
                divi.append(i)
        if len(divi) == 2:
            return True
        else:
            return False


#2

This is a great solution to the problem. While it may not be the quickest solution to the problem, it is most certainly thinking "outside the box!"


#3

Thanks for that.

I now realise for starters that I needlessly created the num variable when I could have just use x.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.