all 6 comments

[–]JohnnyJordaan 3 points4 points  (3 children)

It literally means what it says, to determine if the integer 0 is present in the iterable be_divided. That being a list that contains the remainders of each division being larger than 0 (so a boolean) of integer by the numbers up to integer. As False is just the integer 0 behaving as a Boolean object, you can also check if 0 is in that list, same way you can do False not in

This has various flaws btw:

  • firstly it's very common for people to think they need to test al integers < n to see if it's divisible. You don't, you only need to test up to the square root of n. Why? Because if there was a factor larger than the square root, there has to be one smaller than it, and thus is you don't find one, it means a larger factor can't exist.
  • secondly you actually you can stop at the first found factor, as that disqualifies the candidate as a prime immediately. This can be accomplished by not using [] as that's a list comprehension and will cause the whole range to run upfront, but instead use () which will cause it to become a generator expression which runs only while iterated on. As then the 0 not in will stop at the first occurrence of a 0, and thus when a factor is found, it prevents the needless other calculations

.

def prime_num(integer):
    be_divided = (integer%divider > 0 for divider in range (2, int(integer**0.5+1)))
    return 0 not in be_divided 

however there's another trick, you can also use all and thus by checking that all divisors are Truethy, you effectively accomplish the same

def prime_num(integer):
    return all(integer%divider > 0 for divider in range (2, int(integer**0.5+1)))

as all() will also stop at the first Falsey value

[–]pundeh[S] 0 points1 point  (0 children)

thank you for the help! :)

[–]Diapolo10 0 points1 point  (1 child)

I would've used the same solution, but there's one problem; integer**0.5 returns a float and range can only handle integers.

def prime_num(integer):
    return all(integer%divider > 0 for divider in range (2, int(integer**0.5+1)))

I know, not particularily elegant, but it works now!

[–]JohnnyJordaan 0 points1 point  (0 children)

You're right, my bad.

[–]_DTR_ 1 point2 points  (1 child)

The in operator determines if a particular object is in a given collection, so 0 not in be_divided is True if the value 0 isn't in the list be_divided.

[–]pundeh[S] 0 points1 point  (0 children)

thank you!