Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old July 8th, 2003, 10:20 PM
wannabe wannabe is offline
=) wannabe?
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 2002
Location: florida
Posts: 2,153 wannabe User rank is Lance Corporal (50 - 100 Reputation Level)wannabe User rank is Lance Corporal (50 - 100 Reputation Level)wannabe User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 21 h 58 m 25 sec
Reputation Power: 9
Send a message via AIM to wannabe Send a message via Yahoo to wannabe
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

Reply With Quote
  #2  
Old July 8th, 2003, 11:01 PM
joebonanno joebonanno is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Los Angeles
Posts: 6 joebonanno User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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.

Reply With Quote
  #3  
Old July 9th, 2003, 12:05 AM
wannabe wannabe is offline
=) wannabe?
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 2002
Location: florida
Posts: 2,153 wannabe User rank is Lance Corporal (50 - 100 Reputation Level)wannabe User rank is Lance Corporal (50 - 100 Reputation Level)wannabe User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 21 h 58 m 25 sec
Reputation Power: 9
Send a message via AIM to wannabe Send a message via Yahoo to wannabe
remainder is 0? i dont get that.

Reply With Quote
  #4  
Old July 9th, 2003, 12:34 AM
joebonanno joebonanno is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Los Angeles
Posts: 6 joebonanno User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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"

Reply With Quote
  #5  
Old July 9th, 2003, 11:41 AM
wannabe wannabe is offline
=) wannabe?
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 2002
Location: florida
Posts: 2,153 wannabe User rank is Lance Corporal (50 - 100 Reputation Level)wannabe User rank is Lance Corporal (50 - 100 Reputation Level)wannabe User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 21 h 58 m 25 sec
Reputation Power: 9
Send a message via AIM to wannabe Send a message via Yahoo to wannabe
=) i figured that part out. i just dont get the syntax.

i dont get the % and != 0 parts

Reply With Quote
  #6  
Old July 9th, 2003, 12:47 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #7  
Old July 9th, 2003, 01:08 PM
damonbrinkley damonbrinkley is offline
Modz
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Location: NC, USA
Posts: 364 damonbrinkley User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 m 18 sec
Reputation Power: 6
Send a message via AIM to damonbrinkley
% 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.

Reply With Quote
  #8  
Old July 9th, 2003, 02:22 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Thanks a lot damon, good to know .

Mark.

Reply With Quote
  #9  
Old July 9th, 2003, 03:44 PM
wannabe wannabe is offline
=) wannabe?
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 2002
Location: florida
Posts: 2,153 wannabe User rank is Lance Corporal (50 - 100 Reputation Level)wannabe User rank is Lance Corporal (50 - 100 Reputation Level)wannabe User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 21 h 58 m 25 sec
Reputation Power: 9
Send a message via AIM to wannabe Send a message via Yahoo to wannabe
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

Reply With Quote
  #10  
Old July 9th, 2003, 04:44 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
interesting, thanks for the info.

Reply With Quote
  #11  
Old July 10th, 2003, 06:24 PM
inkedmn inkedmn is offline
Tattooed Python-Lovin' Freak-Boy
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: orange county, CA
Posts: 16 inkedmn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to inkedmn Send a message via AIM to inkedmn
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)

Reply With Quote
  #12  
Old July 22nd, 2003, 03:53 AM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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 04:00 AM.

Reply With Quote
  #13  
Old July 22nd, 2003, 12:29 PM
wannabe wannabe is offline
=) wannabe?
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 2002
Location: florida
Posts: 2,153 wannabe User rank is Lance Corporal (50 - 100 Reputation Level)wannabe User rank is Lance Corporal (50 - 100 Reputation Level)wannabe User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 21 h 58 m 25 sec
Reputation Power: 9
Send a message via AIM to wannabe Send a message via Yahoo to wannabe
why?

Reply With Quote
  #14  
Old July 22nd, 2003, 12:57 PM
karsh44's Avatar
karsh44 karsh44 is offline
Just another guy
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jun 2003
Location: Wisconsin
Posts: 2,915 karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 13 h 6 m 22 sec
Reputation Power: 76
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.

Reply With Quote
  #15  
Old July 22nd, 2003, 01:22 PM
wannabe wannabe is offline
=) wannabe?
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 2002
Location: florida
Posts: 2,153 wannabe User rank is Lance Corporal (50 - 100 Reputation Level)wannabe User rank is Lance Corporal (50 - 100 Reputation Level)wannabe User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 21 h 58 m 25 sec
Reputation Power: 9
Send a message via AIM to wannabe Send a message via Yahoo to wannabe
oh. they never explained that

thanks for pointing that out.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > syntax thingy x % 2 != 0 amd x % 3 != 0


Thread Tools  Search this Thread 
Search this Thread: