Thread: syntax thingy x % 2 != 0 amd x % 3 != 0

1. No Profile Picture
=) wannabe?
Devshed Regular (2000 - 2499 posts)

Join Date
Jul 2002
Location
florida
Posts
2,153
Rep Power
18

syntax thingy x % 2 != 0 amd x % 3 != 0

Code:
```def prms(x):
return x % 2 != 0 and x % 3 != 0
r = filter(prms,range(2,25))
print r```
that'll return prime numbers from 2 to 25.

but i dont understand this line

return x % 2 != 0 and x % 3 != 0

is 0 flase in this case? what does the % sign mean.

please explain.

thank you
2. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2003
Location
Los Angeles
Posts
6
Rep Power
0
Someone else may give you a more complete answer, but I believe the line is testing for remainders in the division of x by 2 and 3. If the remainder for either division is 0 then the number cannot be prime.
3. No Profile Picture
=) wannabe?
Devshed Regular (2000 - 2499 posts)

Join Date
Jul 2002
Location
florida
Posts
2,153
Rep Power
18
remainder is 0? i dont get that.
4. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2003
Location
Los Angeles
Posts
6
Rep Power
0
It's so simple, you will be wacking yourself on the head.

x % 2 #returns remainder of x / 2.

Prime numbers, by definition, are only divisible by 1 and themselves (duh, right?), so if "x" is evenly divisible by 2 and/or 3 then it cannot be prime and is not passed to "r"
5. No Profile Picture
=) wannabe?
Devshed Regular (2000 - 2499 posts)

Join Date
Jul 2002
Location
florida
Posts
2,153
Rep Power
18
=) i figured that part out. i just dont get the syntax.

i dont get the % and != 0 parts
6. I've never seen anything like this before, it looks like an if-return combination. where did you see this?

% is a mathimatical operator, i dont really know how to describe it but i've used it in PHP, Perl and Python when working on the web to create alternate row colors. you'd do a loop and check the iterator number to see if it % 2.

!= 0 means if whatever is not equal to 0 (also faulse).

Hope this helps, it really is a strance bit of code i hae to say. Wasn't awar this was even possible but

Mark.
7. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2002
Location
NC, USA
Posts
364
Rep Power
16
% is the modulus operator. It returns the remainder of two numbers when one is divided by the other. For Example. 7/3 is 2 with a remainder of 1. So if you said x = (7/3) then x would be equal to 2, it drops the remainder. To find the remainder you would say 7%3 which is 1 because the remainder of 7/3 is 1. So, y = 7%3 would set y equal to 1.
8. Thanks a lot damon, good to know .

Mark.
9. No Profile Picture
=) wannabe?
Devshed Regular (2000 - 2499 posts)

Join Date
Jul 2002
Location
florida
Posts
2,153
Rep Power
18
ooh. so as long as the remainder is not 0 the number is prime. damn!. thanks a lot =)

i saw that here http://www.python.org/doc/current/tut/node7.html
10. interesting, thanks for the info.
11. No Profile Picture
Tattooed Python-Lovin' Freak-Boy
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2001
Location
orange county, CA
Posts
16
Rep Power
0
this line
Code:
`return x % 2 != 0 and x % 3 != 0`
will return 1 if both expressions evaluate to true, or 0 otherwise (python 2.2 uses 1 and 0 for booleans, 2.3 uses the words True or False)
12. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2003
Posts
133
Rep Power
15
Just remember, using
Code:
`x%2 != 0 and x %3 !=0`
to see if x is a prime number will only work up to, and not including, 25.
Last edited by percivall; July 22nd, 2003 at 05:00 AM.
13. No Profile Picture
=) wannabe?
Devshed Regular (2000 - 2499 posts)

Join Date
Jul 2002
Location
florida
Posts
2,153
Rep Power
18
why?
14. Once you reach 25, there are more numbers (factors) to consider in determining if a number is prime.

ie- 25/2 != 0 and 25/3 != 0, but 25/5 == 0, so 25 is not prime, even though it fits the criteria of that line of code. Below 25, all the non-prime numbers are either even (divisible by 2) or divisible by 3, so that code works.
15. No Profile Picture
=) wannabe?
Devshed Regular (2000 - 2499 posts)

Join Date
Jul 2002
Location
florida
Posts
2,153
Rep Power
18
oh. they never explained that

thanks for pointing that out.