[Tutor] Windows Power Shell

2010-08-21 Thread Alan Gauld
A recent windows update delivered the Windows Power Shell to my 
desktop.


I'd heard of this but never used it till now. I've only started 
playing with it but
it is essentially DOS on steroids. It brings Windows users a shell 
that seems
to be very close to Unix shells like Bash in power and flexibility 
(and scripting
language). It's very early days yet, but I wondered if any other tutor 
members

had started using WPS?

Some examples of the extra features include regex,  objects, process 
control,

remote invocation, better loop structures etc...

Regards,

Alan G. 



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] flow problem with a exercise

2010-08-21 Thread Roelof Wobben


 


From: waynejwer...@gmail.com
Date: Fri, 20 Aug 2010 15:07:56 -0500
Subject: Re: [Tutor] flow problem with a exercise
To: rwob...@hotmail.com
CC: tutor@python.org


On Fri, Aug 20, 2010 at 2:48 PM, Roelof Wobben  wrote:


Oke, 
 
I don''t understand it complety.
 
return not arg%2 
 
Why use not here ?
 
I think that arg%2 is True not makes it false. 



What happens when you replace arg with a value? % is modulo division, so it 
just returns the remainder.


2 % 2 = ?
2%2=0 
 
4 % 2 = ?
4%2=0
 
7 % 2 = ?
 
7%2=1 
 
11 % 2 = ?
 
11%2 =1 

What is the truth value of 0 and 1?


print 'True' if 0 else 'False'
 
False 
 
print 'True' if 1 else 'False'
 
True

So what is the outcome of the following?


result = 2 % 2
print result, not result
if not result:
   print 'Even'
if result:
   print 'odd'
 
0  True
Even 
 
So 1 is True and 0 is False according to Python.


 

 
Another question.
 
How can I round outcome of a calculation.
 
round ( ( t-32)/1.8) does not work because I get a message that there are two 
arguments.
 
Outcome = (t-32)/1.8
outcome2 = round (outcome) does not work because the argument must be a string 
or a number 


What is the type of t?


 In [39]: t = 3


In [40]: round((t-32)/1.8)
Out[40]: -16.0


In [41]: t = 3.0


In [42]: round((t-32)/1.8)
Out[42]: -16.0


Works fine for me.
 
Correct, 
But I see one wierd thing.
 
round ((42-32)/1.8) gives a output -16.0 but (42-32)/1.8) gives also -16.0 
I was expectting that round will give 16 as output because round (32.0) is the 
same as round (32.0, 0) so there will be 0 decimals.
And I see one decimal.
 
Roelof
 


HTH,
Wayne ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] flow problem with a exercise

2010-08-21 Thread Evert Rol
>  In [39]: t = 3
> 
> In [40]: round((t-32)/1.8)
> Out[40]: -16.0
> 
> In [41]: t = 3.0
> 
> In [42]: round((t-32)/1.8)
> Out[42]: -16.0
> 
> Works fine for me.
>  
> Correct,
> But I see one wierd thing.
>  
> round ((42-32)/1.8) gives a output -16.0 but (42-32)/1.8) gives also -16.0
> I was expectting that round will give 16 as output because round (32.0) is 
> the same as round (32.0, 0) so there will be 0 decimals.
> And I see one decimal.

The rounding doesn't influence how it's printed.
>From help(round):

"""
round(...)
round(number[, ndigits]) -> floating point number

Round a number to a given precision in decimal digits (default 0 digits).
This always returns a floating point number.  Precision may be negative.
"""

It *rounds* the number to ndigits, not prints. It always returns a floating 
point number though (not an integer).
Since floats are normally represented with at least one trailing digit after 
the decimal dot, you see a zero.
Eg,
>>> float(1)
1.0

If you want to get rid of the trailing .0, convert it to an integer:
>>> int(round(1.1))
1

Also consider:
>>> round(1.15, 0)
1.0
>>> round(1.15, 1)
1.2
>>> round(1.15, 2)
1.1499


(Note again how the last rounded 1.15 is represented. It's just a 
representation though, and showing the imprecision you intrinsicaly get when 
dealing with floating point numbers.)

If you really want different behaviour when printing (or using) floating point 
numbers, perhaps have a look at the decimal module: 
http://docs.python.org/library/decimal.html


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] flow problem with a exercise

2010-08-21 Thread Dave Angel
You need to figure out how to get your email program to do quoting.  As 
it stands, there's no good way to tell what part of the following 
message was from you, and what part was from wayne, or maybe others.   
Probably all you need to do is to do a reply-all to the message, and 
it'll mark the existing text with a ">" symbol.  Then make sure 
everything you type is *not* quoted.


Roelof Wobben wrote:
 



From: waynejwer...@gmail.com


result = 2 % 2
print result, not result
if not result:
   print 'Even'
if result:
   print 'odd'
 
0  True
Even 
 
So 1 is True and 0 is False according to Python.



  
No, True is a specific object that differs from all other objects.  
Likewise False.  However, other objects, including especially integers, 
lists, and strings, can be used in a boolean context, and there are 
specific rules that decide whether a given object will be used as true 
(lowercase) or false.  For integers values, all nonzero items are 
considered true, while zero is considered false.
 

 
Another question.
 
How can I round outcome of a calculation.
 
round ( ( t-32)/1.8) does not work because I get a message that there are two arguments.
 
Outcome = (t-32)/1.8
outcome2 = round (outcome) does not work because the argument must be a string or a number 



What is the type of t?


 In [39]: t = 3


In [40]: round((t-32)/1.8)
Out[40]: -16.0


In [41]: t = 3.0


In [42]: round((t-32)/1.8)
Out[42]: -16.0


Works fine for me.
 
Correct, 
But I see one wierd thing.
 
round ((42-32)/1.8) gives a output -16.0 but (42-32)/1.8) gives also -16.0 
I was expectting that round will give 16 as output because round (32.0) is the same as round (32.0, 0) so there will be 0 decimals.

And I see one decimal.
 
Roelof
 



HTH,
Wayne
You're confusing the operation of round() with the operation of 
converting to a string for printing.  Round changes a floating point 
(binary) value to have a certain quantization, which approximates what 
you might want to print.  It does not, however, affect the way print 
will subsequently deal with that floating point number.   It's no 
different than when you use a numeric literal with varying amounts of 
apparent precision.

x = 4.2
x = 4.20
x = 4.20

all produce precisely the same floating point object.

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] flow problem with a exercise

2010-08-21 Thread Roelof Wobben


 

> Subject: Re: [Tutor] flow problem with a exercise
> From: evert@gmail.com
> Date: Sat, 21 Aug 2010 12:39:05 +0200
> CC: tutor@python.org
> To: rwob...@hotmail.com
> 
> > In [39]: t = 3
> > 
> > In [40]: round((t-32)/1.8)
> > Out[40]: -16.0
> > 
> > In [41]: t = 3.0
> > 
> > In [42]: round((t-32)/1.8)
> > Out[42]: -16.0
> > 
> > Works fine for me.
> > 
> > Correct,
> > But I see one wierd thing.
> > 
> > round ((42-32)/1.8) gives a output -16.0 but (42-32)/1.8) gives also -16.0
> > I was expectting that round will give 16 as output because round (32.0) is 
> > the same as round (32.0, 0) so there will be 0 decimals.
> > And I see one decimal.
> 
> The rounding doesn't influence how it's printed.
> From help(round):
> 
> """
> round(...)
> round(number[, ndigits]) -> floating point number
> 
> Round a number to a given precision in decimal digits (default 0 digits).
> This always returns a floating point number. Precision may be negative.
> """
> 
> It *rounds* the number to ndigits, not prints. It always returns a floating 
> point number though (not an integer).
> Since floats are normally represented with at least one trailing digit after 
> the decimal dot, you see a zero.
> Eg,
> >>> float(1)
> 1.0
> 
> If you want to get rid of the trailing .0, convert it to an integer:
> >>> int(round(1.1))
> 1
> 
> Also consider:
> >>> round(1.15, 0)
> 1.0
> >>> round(1.15, 1)
> 1.2
> >>> round(1.15, 2)
> 1.1499
> 
> 
> (Note again how the last rounded 1.15 is represented. It's just a 
> representation though, and showing the imprecision you intrinsicaly get when 
> dealing with floating point numbers.)
> 
> If you really want different behaviour when printing (or using) floating 
> point numbers, perhaps have a look at the decimal module: 
> http://docs.python.org/library/decimal.html
> 
> 


Oke, 

 

Thank you. Learned another thing about Python.

 

Roelof

 
  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] check the terminal keyword

2010-08-21 Thread ANKUR AGGARWAL
i m making a app in which i launch application using os.system("input from
user"). I want to check whether the input entered by the user matches with
the exact keyword in terminal or not.
like i want to launch vlc so user should provide me input as " vlc" in order
to launch the app just like he did from terminal. I want to make a check on
the input spell so that if he type "wlc" it shows an error to it. I have an
little idea abt sys.agrv but don't know how to use it... Plz help me guys.
Thanks in advance
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] prime test problem

2010-08-21 Thread Roelof Wobben

Hello, 

 

I have to make a programm which can test if a number is a prime.

I know a prime is a number which can only be diveded by 1 and itself.

 

One way is was thinking about is to make a loop which try if % has output 0.

But that don't work.

 

Can someone give me a hint what's the best approach is.

 

Roelof

 
  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] prime test problem

2010-08-21 Thread Nitin Das
For this problem u will get lots of solutions on the net.
e.g wilson's theorem , sieve of eranthoses etc.

--nitin

On Sat, Aug 21, 2010 at 7:05 PM, Roelof Wobben  wrote:

>  Hello,
>
> I have to make a programm which can test if a number is a prime.
> I know a prime is a number which can only be diveded by 1 and itself.
>
> One way is was thinking about is to make a loop which try if % has output
> 0.
> But that don't work.
>
> Can someone give me a hint what's the best approach is.
>
> Roelof
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] prime test problem

2010-08-21 Thread Robert Berman
Googling 'computing prime numbers' produced about 64,100,000 results . That
should certainly get you started.

 

Robert

 

From: tutor-bounces+bermanrl=cfl.rr@python.org
[mailto:tutor-bounces+bermanrl=cfl.rr@python.org] On Behalf Of Roelof
Wobben
Sent: Saturday, August 21, 2010 9:36 AM
To: tutor@python.org
Subject: [Tutor] prime test problem

 

Hello, 
 
I have to make a programm which can test if a number is a prime.
I know a prime is a number which can only be diveded by 1 and itself.
 
One way is was thinking about is to make a loop which try if % has output 0.
But that don't work.
 
Can someone give me a hint what's the best approach is.
 
Roelof
 

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] check the terminal keyword

2010-08-21 Thread Nitin Das
You can use raw_input like for e.g.
u_input = raw_input("user_command")
n then u can check u_input against anything. if doesn't matches then u can
throw an error.
I hope it shall help you.

--nitin


On Sat, Aug 21, 2010 at 6:37 PM, ANKUR AGGARWAL wrote:

> i m making a app in which i launch application using os.system("input from
> user"). I want to check whether the input entered by the user matches with
> the exact keyword in terminal or not.
> like i want to launch vlc so user should provide me input as " vlc" in
> order to launch the app just like he did from terminal. I want to make a
> check on the input spell so that if he type "wlc" it shows an error to it. I
> have an little idea abt sys.agrv but don't know how to use it... Plz help me
> guys.
> Thanks in advance
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] check the terminal keyword

2010-08-21 Thread Evert Rol
> i m making a app in which i launch application using os.system("input from 
> user"). I want to check whether the input entered by the user matches with 
> the exact keyword in terminal or not.
> like i want to launch vlc so user should provide me input as " vlc" in order 
> to launch the app just like he did from terminal. I want to make a check on 
> the input spell so that if he type "wlc" it shows an error to it. I have an 
> little idea abt sys.agrv but don't know how to use it... Plz help me guys.

Check the return value from os.system. If it's not 0, there was an error, 
likely caused by incorrect input.
Better yet, catch the error message you get back from the command line, and act 
on that (eg, show it to the user).

Eg, 
>>> r = os.system("hello")
sh: hello: command not found
>>> print r
32512

And have a look at the subprocess module, which I think is now the de facto 
module to run external processes. 

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] prime test problem

2010-08-21 Thread Roelof Wobben

Hello, 
 
I know.
I have read them all I believe but I can't see how I can convert the algebra to 
a working programm.
 
Roelof

 


Date: Sat, 21 Aug 2010 19:15:03 +0530
Subject: Re: [Tutor] prime test problem
From: nitin@gmail.com
To: rwob...@hotmail.com
CC: tutor@python.org

For this problem u will get lots of solutions on the net.
e.g wilson's theorem , sieve of eranthoses etc.


--nitin


On Sat, Aug 21, 2010 at 7:05 PM, Roelof Wobben  wrote:


Hello, 
 
I have to make a programm which can test if a number is a prime.
I know a prime is a number which can only be diveded by 1 and itself.
 
One way is was thinking about is to make a loop which try if % has output 0.
But that don't work.
 
Can someone give me a hint what's the best approach is.
 
Roelof
 

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] prime test problem

2010-08-21 Thread Roelof Wobben

Hello, 

 

I forget to mention that I try to follow this book : Think like a computer 
scientist and I'm now working on chapter 4.

 

Roelof

 


From: rwob...@hotmail.com
To: tutor@python.org
Date: Sat, 21 Aug 2010 14:41:03 +
Subject: Re: [Tutor] prime test problem




Hello, 
 
I know.
I have read them all I believe but I can't see how I can convert the algebra to 
a working programm.
 
Roelof

 


Date: Sat, 21 Aug 2010 19:15:03 +0530
Subject: Re: [Tutor] prime test problem
From: nitin@gmail.com
To: rwob...@hotmail.com
CC: tutor@python.org

For this problem u will get lots of solutions on the net. 
e.g wilson's theorem , sieve of eranthoses etc.


--nitin


On Sat, Aug 21, 2010 at 7:05 PM, Roelof Wobben  wrote:


Hello, 
 
I have to make a programm which can test if a number is a prime.
I know a prime is a number which can only be diveded by 1 and itself.
 
One way is was thinking about is to make a loop which try if % has output 0.
But that don't work.
 
Can someone give me a hint what's the best approach is.
 
Roelof
 

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor



___ Tutor maillist - 
Tutor@python.org To unsubscribe or change subscription options: 
http://mail.python.org/mailman/listinfo/tutor   
 ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] prime test problem

2010-08-21 Thread Evert Rol
> Hello, 
>  
> I know.
> I have read them all I believe but I can't see how I can convert the algebra 
> to a working programm.

And if you just search Google for "Python prime number algorithm"? Perhaps it's 
cheating, so you'll have to try and fully understand the code first before you 
run it (be sure to read comments if there are any, eg the activatestate 
recipes; can have tons of useful extra info & links).
Generally, try to start with a program yourself, and then see where you get 
stuck. You could then post your stuck algorithm to the list and ask for advice; 
your current question is a bit too general, and thus harder to answer.

  Evert


>  
> Roelof
> 
>  
> Date: Sat, 21 Aug 2010 19:15:03 +0530
> Subject: Re: [Tutor] prime test problem
> From: nitin@gmail.com
> To: rwob...@hotmail.com
> CC: tutor@python.org
> 
> For this problem u will get lots of solutions on the net.
> e.g wilson's theorem , sieve of eranthoses etc.
> 
> --nitin
> 
> On Sat, Aug 21, 2010 at 7:05 PM, Roelof Wobben  wrote:
> Hello, 
>  
> I have to make a programm which can test if a number is a prime.
> I know a prime is a number which can only be diveded by 1 and itself.
>  
> One way is was thinking about is to make a loop which try if % has output 0.
> But that don't work.
>  
> Can someone give me a hint what's the best approach is.
>  
> Roelof
>  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] prime test problem

2010-08-21 Thread Steven D'Aprano
On Sun, 22 Aug 2010 12:41:03 am Roelof Wobben wrote:
> Hello,
>
> I know.
> I have read them all I believe 

You've read all 64 million pages that Google finds? Wow, you must be a 
fast reader! Well done!

> but I can't see how I can convert the 
> algebra to a working programm.

Have you *tried*?

Perhaps you should try something a little bit less ambitious. Write a 
program to test whether a number is divisible by 3. Then write a 
program to test whether a number is divisible by 3 or 5. Then write a 
third program to test whether a number is divisible by 3, 5 or 7. 

Then generalise that third program.

Off you go. Come back when you have some code. Even if it isn't working 
code, at least try something.



-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] prime test problem

2010-08-21 Thread Roelof Wobben

Hello,

 

I tried it with this simple programm 

 

def is_prime(n):
x=2 
while x <= int(n**0.5)+1:
if n % x == 0: 
return False

x=x+1; 
return True
  
x=is_prime(7)
if x==True:
print 7, "is een prime getal"
else :
print 7, "is geen prime getal"

 

 

But this one gets in a indefinitive loop.

 

Roelof


 
> From: st...@pearwood.info
> To: tutor@python.org
> Date: Sun, 22 Aug 2010 02:49:26 +1000
> Subject: Re: [Tutor] prime test problem
> 
> On Sun, 22 Aug 2010 12:41:03 am Roelof Wobben wrote:
> > Hello,
> >
> > I know.
> > I have read them all I believe 
> 
> You've read all 64 million pages that Google finds? Wow, you must be a 
> fast reader! Well done!
> 
> > but I can't see how I can convert the 
> > algebra to a working programm.
> 
> Have you *tried*?
> 
> Perhaps you should try something a little bit less ambitious. Write a 
> program to test whether a number is divisible by 3. Then write a 
> program to test whether a number is divisible by 3 or 5. Then write a 
> third program to test whether a number is divisible by 3, 5 or 7. 
> 
> Then generalise that third program.
> 
> Off you go. Come back when you have some code. Even if it isn't working 
> code, at least try something.
> 
> 
> 
> -- 
> Steven D'Aprano
> ___
> Tutor maillist - Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Windows Power Shell

2010-08-21 Thread Bill Allen
Alan,

I have used WPS 1.0 for some time at work to script software installs, etc.
It is very powerful and gives full .NET visibility to "DOS" level scripts.
In fact, it is a plausible replacement for VB for most administrative
scripting work in the Windows environment.

Some good resources:
The Hey Scripting Guy ! Technet
blog is an execellent place to start as he focuses heavily on WPS.
PowerShell.com  WPS community (you can also sign
up for really good daily WPS tips sent straight to you email here, I
recommend these tips!)
TechNet Learning to Script
Center,
focuses on WPS.


Bill




On Sat, Aug 21, 2010 at 3:20 AM, Alan Gauld wrote:

> A recent windows update delivered the Windows Power Shell to my desktop.
>
> I'd heard of this but never used it till now. I've only started playing
> with it but
> it is essentially DOS on steroids. It brings Windows users a shell that
> seems
> to be very close to Unix shells like Bash in power and flexibility (and
> scripting
> language). It's very early days yet, but I wondered if any other tutor
> members
> had started using WPS?
>
> Some examples of the extra features include regex,  objects, process
> control,
> remote invocation, better loop structures etc...
>
> Regards,
>
> Alan G.
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] prime test problem

2010-08-21 Thread Evert Rol
> Hello,
>  
> I tried it with this simple programm 
>  
> def is_prime(n):
> x=2 
> while x <= int(n**0.5)+1:
> if n % x == 0: 
> return False

If your while condition is True, you get into the loop. 
Inside the loop, however, you never change anything that could change the 
condition (ie, neither x nor n get changed).
So the while condition stays the same, ie, True, and you loop indefinitely.


> x=x+1;

Ah, but here you actually change something so that the while condition could 
become False. Sadly, this statement is outside the loop (hint hint).


> 
> return True
>   
> x=is_prime(7)
> if x==True:
> print 7, "is een prime getal"
> else :
> print 7, "is geen prime getal"
>  
>  
> But this one gets in a indefinitive loop.

Try to mentally follow the logic of the loop. 
Or use print statements and some small, easily checkable, numbers (prime and 
non-prime) to see what happens. 
Print statements (functions, if you're on Python >= 3) are a simple, but at 
times extremely quick and useful way to debug scripts.

Ah yes, and either use "is een priemgetal" or "is a prime number", not a mix of 
both (sorry, couldn't help myself ;-).


  Evert



>  
> Roelof
> 
>  
> > From: st...@pearwood.info
> > To: tutor@python.org
> > Date: Sun, 22 Aug 2010 02:49:26 +1000
> > Subject: Re: [Tutor] prime test problem
> > 
> > On Sun, 22 Aug 2010 12:41:03 am Roelof Wobben wrote:
> > > Hello,
> > >
> > > I know.
> > > I have read them all I believe 
> > 
> > You've read all 64 million pages that Google finds? Wow, you must be a 
> > fast reader! Well done!
> > 
> > > but I can't see how I can convert the 
> > > algebra to a working programm.
> > 
> > Have you *tried*?
> > 
> > Perhaps you should try something a little bit less ambitious. Write a 
> > program to test whether a number is divisible by 3. Then write a 
> > program to test whether a number is divisible by 3 or 5. Then write a 
> > third program to test whether a number is divisible by 3, 5 or 7. 
> > 
> > Then generalise that third program.
> > 
> > Off you go. Come back when you have some code. Even if it isn't working 
> > code, at least try something.
> > 
> > 
> > 
> > -- 
> > Steven D'Aprano
> > ___
> > Tutor maillist - Tutor@python.org
> > To unsubscribe or change subscription options:
> > http://mail.python.org/mailman/listinfo/tutor
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] prime test problem

2010-08-21 Thread Roelof Wobben

Hello, 

 

 

Thank you.

It worked now.

 

x=x+1 must have the same indention als the if then and the return.

 

Roelof


 
> Subject: Re: [Tutor] prime test problem
> From: evert@gmail.com
> Date: Sat, 21 Aug 2010 19:24:51 +0200
> CC: tutor@python.org
> To: rwob...@hotmail.com
> 
> > Hello,
> > 
> > I tried it with this simple programm 
> > 
> > def is_prime(n):
> > x=2 
> > while x <= int(n**0.5)+1: 
> > if n % x == 0: 
> > return False 
> 
> If your while condition is True, you get into the loop. 
> Inside the loop, however, you never change anything that could change the 
> condition (ie, neither x nor n get changed).
> So the while condition stays the same, ie, True, and you loop indefinitely.
> 
> 
> > x=x+1;
> 
> Ah, but here you actually change something so that the while condition could 
> become False. Sadly, this statement is outside the loop (hint hint).
> 
> 
> > 
> > return True
> > 
> > x=is_prime(7)
> > if x==True:
> > print 7, "is een prime getal"
> > else :
> > print 7, "is geen prime getal"
> > 
> > 
> > But this one gets in a indefinitive loop.
> 
> Try to mentally follow the logic of the loop. 
> Or use print statements and some small, easily checkable, numbers (prime and 
> non-prime) to see what happens. 
> Print statements (functions, if you're on Python >= 3) are a simple, but at 
> times extremely quick and useful way to debug scripts.
> 
> Ah yes, and either use "is een priemgetal" or "is a prime number", not a mix 
> of both (sorry, couldn't help myself ;-).
> 
> 
> Evert
> 
> 
> 
> > 
> > Roelof
> > 
> > 
> > > From: st...@pearwood.info
> > > To: tutor@python.org
> > > Date: Sun, 22 Aug 2010 02:49:26 +1000
> > > Subject: Re: [Tutor] prime test problem
> > > 
> > > On Sun, 22 Aug 2010 12:41:03 am Roelof Wobben wrote:
> > > > Hello,
> > > >
> > > > I know.
> > > > I have read them all I believe 
> > > 
> > > You've read all 64 million pages that Google finds? Wow, you must be a 
> > > fast reader! Well done!
> > > 
> > > > but I can't see how I can convert the 
> > > > algebra to a working programm.
> > > 
> > > Have you *tried*?
> > > 
> > > Perhaps you should try something a little bit less ambitious. Write a 
> > > program to test whether a number is divisible by 3. Then write a 
> > > program to test whether a number is divisible by 3 or 5. Then write a 
> > > third program to test whether a number is divisible by 3, 5 or 7. 
> > > 
> > > Then generalise that third program.
> > > 
> > > Off you go. Come back when you have some code. Even if it isn't working 
> > > code, at least try something.
> > > 
> > > 
> > > 
> > > -- 
> > > Steven D'Aprano
> > > ___
> > > Tutor maillist - Tutor@python.org
> > > To unsubscribe or change subscription options:
> > > http://mail.python.org/mailman/listinfo/tutor
> > ___
> > Tutor maillist - Tutor@python.org
> > To unsubscribe or change subscription options:
> > http://mail.python.org/mailman/listinfo/tutor
> 
  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Windows Power Shell

2010-08-21 Thread Bill Allen
The very best first feature to learn is the "foreach" command.  It elegantly
solves the icky, age old, problem in DOS batch of reading in a list of
values from a file and doing something with them.   This alone sold me
completely on WPS.

For instance, here is and actual script I used to push an install source to
all my engineering systems at work, listed in computers.txt, by invoking
richcopy(also
a fantastic utility, Microsoft's modern replacement for robocopy.

get-content computers.txt | foreach -process {richcopy
"\\myNAS\NasShare\NX6\nx6028" "\\$_\c$\nx6028" /TD 10 /QA /QP
"c:\results.log" /US /UD /UE}

Sorry for taking up additional "non-Python" bandwidth, but I got excited
about being able to contribute something back to the group that I actual
know about!  ;-D

Bill

On Sat, Aug 21, 2010 at 12:21 PM, Bill Allen  wrote:

> Alan,
>
> I have used WPS 1.0 for some time at work to script software installs,
> etc.  It is very powerful and gives full .NET visibility to "DOS" level
> scripts.  In fact, it is a plausible replacement for VB for most
> administrative scripting work in the Windows environment.
>
> Some good resources:
> The Hey Scripting Guy !
> Technet blog is an execellent place to start as he focuses heavily on WPS.
> PowerShell.com  WPS community (you can also
> sign up for really good daily WPS tips sent straight to you email here, I
> recommend these tips!)
> TechNet Learning to Script 
> Center,
> focuses on WPS.
>
>
> Bill
>
>
>
>
>
> On Sat, Aug 21, 2010 at 3:20 AM, Alan Gauld wrote:
>
>> A recent windows update delivered the Windows Power Shell to my desktop.
>>
>> I'd heard of this but never used it till now. I've only started playing
>> with it but
>> it is essentially DOS on steroids. It brings Windows users a shell that
>> seems
>> to be very close to Unix shells like Bash in power and flexibility (and
>> scripting
>> language). It's very early days yet, but I wondered if any other tutor
>> members
>> had started using WPS?
>>
>> Some examples of the extra features include regex,  objects, process
>> control,
>> remote invocation, better loop structures etc...
>>
>> Regards,
>>
>> Alan G.
>>
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] prime test problem

2010-08-21 Thread Alan Gauld


"Roelof Wobben"  wrote 


It worked now.
x=x+1 must have the same indention als the if then and the return.


Or more specifically, it must be indented further than the while 
statement. The fact thatv the other commands inside the while 
loop happen to be an if/else is incidental


x=1
while x < 10:
   x = x+1

works as a loop too.

Alan G.


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] design of Point class

2010-08-21 Thread bob gailer

 On 8/20/2010 8:35 PM, bob gailer wrote:

After yet more thought it gets even more better: I even added a unit test.

class PointND(list):
  def __init__(self, *a_list):
super(PointND, self).__init__(a_list)

  def getSet(ix):
def chklen(self):
  if len(self) < ix + 1:
raise AttributeError
def get(self):
  chklen(self)
  return self[ix]
def set(self, value):
  chklen(self)
  self[ix] = value
return property(get, set)

  def set(self, ix):
return s

  x = getSet(0)
  y = getSet(1)
  z = getSet(2)

p = PointND(1,2,3)
assert (p.x, p.y, p.z) == (1, 2, 3)
p.x = 6; p.y = 9; p.z = 5
assert (p.x, p.y, p.z) == (6, 9, 5)

try:
  p = PointND(1,2)
  p.z = 3
except AttributeError:
  print 'Passed all tests'
except:
  print 'some other exception'



--
Bob Gailer
919-636-4239
Chapel Hill NC

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] newline problem

2010-08-21 Thread Roelof Wobben

Hello, 

 

I have this programm :

 

def print_digits(n):
"""
  >>> print_digits(13789)
  9 8 7 3 1
  >>> print_digits(39874613)
  3 1 6 4 7 8 9 3
  >>> print_digits(213141)
  1 4 1 3 1 2 """

count = 0
while n:
count = count + 1
n = n / 10
return count

getal=13789
x=print_digits(getal)
teller=1 
while teller <=x :
digit=float(getal/10.0)
digit2=float(digit-int(digit))*10
getal=digit
print int(digit2)
teller=teller+1



But now every digit is placed on another line.

How can I prevent this ?

 

Roelof

 
  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] newline problem

2010-08-21 Thread bob gailer

 On 8/21/2010 2:36 PM, Roelof Wobben wrote:

Hello,

I have this programm :

def print_digits(n):
"""
>>> print_digits(13789)
  9 8 7 3 1
>>> print_digits(39874613)
  3 1 6 4 7 8 9 3
>>> print_digits(213141)
  1 4 1 3 1 2 """

count = 0
while n:
count = count + 1
n = n / 10
return count
getal=13789
x=print_digits(getal)
teller=1
while teller <=x :
digit=float(getal/10.0)
digit2=float(digit-int(digit))*10
getal=digit
print int(digit2)
teller=teller+1


But now every digit is placed on another line.
How can I prevent this ?

print int(digit2), # add a comma

Your algorithm is unnecessarily complicated.
You don't need to count first.
You don't need float.

getal=13789
while getal > 0:
  getal, digit = divmod(getal, 10)
  print digit,


But it is easier to just:

print " ".join(str(getal))


--
Bob Gailer
919-636-4239
Chapel Hill NC

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] monodevelop 2.2

2010-08-21 Thread Thomas
I was wondering if someone could tell me if you can use the gui designer 
in monodevelop 2.2+ with python.


Thanks,
Thomas

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] monodevelop 2.2

2010-08-21 Thread Emile van Sebille

On 8/21/2010 12:37 PM Thomas said...

I was wondering if someone could tell me if you can use the gui designer
in monodevelop 2.2+ with python.



Searching groups.google.com with "use the gui designer in monodevelop 
2.2+ with python" yields a first hit that says "I just used MonoDevelop 
2.2 for Python under Ubuntu. IT'S AMAZING!"


http://www.daniweb.com/forums/thread286328.html

So, sounds like a yes to me.

Emile


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 78, Issue 97

2010-08-21 Thread Nick
  Were they trying to lead me down 
the path you're alluding to?  I'm not seeing the connection between that 
particular problem and finding primes.  I would appreciate more insight.  
Thanks everyone!  


--

Message: 3
Date: Sat, 21 Aug 2010 17:14:05 +
From: Roelof Wobben 
To: 
Subject: Re: [Tutor] prime test problem
Message-ID: 
Content-Type: text/plain; charset="iso-8859-1"


Hello,



I tried it with this simple programm



def is_prime(n):
x=2
while x <= int(n**0.5)+1:
if n % x == 0:
return False

x=x+1;
return True

x=is_prime(7)
if x==True:
print 7, "is een prime getal"
else :
print 7, "is geen prime getal"





But this one gets in a indefinitive loop.



Roelof



> From: st...@pearwood.info
> To: tutor@python.org
> Date: Sun, 22 Aug 2010 02:49:26 +1000
> Subject: Re: [Tutor] prime test problem
>
> On Sun, 22 Aug 2010 12:41:03 am Roelof Wobben wrote:
> > Hello,
> >
> > I know.
> > I have read them all I believe
>
> You've read all 64 million pages that Google finds? Wow, you must be a
> fast reader! Well done!
>
> > but I can't see how I can convert the
> > algebra to a working programm.
>
> Have you *tried*?
>
> Perhaps you should try something a little bit less ambitious. Write a
> program to test whether a number is divisible by 3. Then write a
> program to test whether a number is divisible by 3 or 5. Then write a
> third program to test whether a number is divisible by 3, 5 or 7.
>
> Then generalise that third program.
>
> Off you go. Come back when you have some code. Even if it isn't working
> code, at least try something.
>
>
>
> --
> Steven D'Aprano
> ___
> Tutor maillist - Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

-- next part --
An HTML attachment was scrubbed...
URL: 
<http://mail.python.org/pipermail/tutor/attachments/20100821/334000b2/attachment-0001.html>

--

Message: 4
Date: Sat, 21 Aug 2010 12:21:10 -0500
From: Bill Allen 
To: Alan Gauld 
Cc: tutor@python.org
Subject: Re: [Tutor] Windows Power Shell
Message-ID:

Content-Type: text/plain; charset="iso-8859-1"

Alan,

I have used WPS 1.0 for some time at work to script software installs, etc.
It is very powerful and gives full .NET visibility to "DOS" level scripts.
In fact, it is a plausible replacement for VB for most administrative
scripting work in the Windows environment.

Some good resources:
The Hey Scripting Guy <http://blogs.technet.com/b/heyscriptingguy/>! Technet
blog is an execellent place to start as he focuses heavily on WPS.
PowerShell.com <http://powershell.com/cs/> WPS community (you can also sign
up for really good daily WPS tips sent straight to you email here, I
recommend these tips!)
TechNet Learning to Script
Center<http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx>,
focuses on WPS.


Bill




On Sat, Aug 21, 2010 at 3:20 AM, Alan Gauld wrote:

> A recent windows update delivered the Windows Power Shell to my desktop.
>
> I'd heard of this but never used it till now. I've only started playing
> with it but
> it is essentially DOS on steroids. It brings Windows users a shell that
> seems
> to be very close to Unix shells like Bash in power and flexibility (and
> scripting
> language). It's very early days yet, but I wondered if any other tutor
> members
> had started using WPS?
>
> Some examples of the extra features include regex,  objects, process
> control,
> remote invocation, better loop structures etc...
>
> Regards,
>
> Alan G.
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-- next part --
An HTML attachment was scrubbed...
URL: 
<http://mail.python.org/pipermail/tutor/attachments/20100821/1172d8db/attachment-0001.html>

--

Message: 5
Date: Sat, 21 Aug 2010 19:24:51 +0200
From: Evert Rol 
To: Roelof Wobben 
Cc: tutor@python.org
Subject: Re: [Tutor] prime test problem
Message-ID: <89e5b3d1-3759-42e1-8e20-a3492545c...@gmail.com>
Content-Type: text/plain; charset=us-ascii

> Hello,
>
> I tried it with this simple programm
>
> def is_prime(n):
> x=2
> while x <= int(n**0.5)+1:
> if n % x == 0:
> return False

If your while condition is True, you get into the loop.
Inside the loop, however, you never change anything that could change the 
condition (ie, neither x n

Re: [Tutor] Tutor Digest, Vol 78, Issue 97

2010-08-21 Thread Steven D'Aprano
Nick,

You have replied to a mailing digest containing five different messages. 
I have NO IDEA what you have to say, because I'm not going to read 
through all five messages, over 400 lines of text, trying to determine 
which bits you are commenting on and which bits you are not.

You have a delete or backspace key on your keyboard. Please learn to use 
it by trimming the quoted text so that ONLY the parts you are replying 
to are there. Leave enough test quoted to establish context of what you 
are discussing, and delete everything else from your reply. We don't 
need to see the entire day's worth of emails again, we've already seen 
them.

Also, change the subject line to something meaningful, instead of the 
useless "Re: [Tutor] Tutor Digest...". Just like the digest already 
suggests:
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Tutor digest..."


If you're replying to a message, you should change the subject line to 
match the subject line of that message (plus "Re: " at the beginning, 
if it's not already there).

Personally, I find the digest setting of mailing lists to be worse than 
useless. I recommend that you change to individual mail, but of course 
that's up to you.


Regards,




-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 78, Issue 99 -- Prime numbers

2010-08-21 Thread Nick
"Perhaps you should try something a little bit less ambitious. Write a
program to test whether a number is divisible by 3. Then write a
program to test whether a number is divisible by 3 or 5. Then write a
third program to test whether a number is divisible by 3, 5 or 7.

Then generalise that third program.
Steven D'Aprano"

*** Sorry for posting like that to the list.  I thought I was doing it 
correctly, but really messed that one up.  I think I'm doing everything 
correctly now, but please tell me what I'm not-- if such is the case.

I was interested in this specific topic as I've been working some problems on 
project euler. 
I've been trying to generate prime numbers as a first step to go about solving 
a problem.  I don't have a particular question about solving the problem, but 
want to ask about a relationship.

The very first problem for projecteuler.net makes you write a program that 
finds all factors of 3 and 5 for n < 1000.  Were they trying to lead me down 
the path you're alluding to?  I'm not seeing the connection between that 
particular problem and finding primes.  I would appreciate more insight.  
Thanks everyone! 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] inserting variables into a docstring

2010-08-21 Thread Bill Allen
I am trying to build an HTML body text for an email using a docstring with
variables inserted a certain points.  It all works just fine, except that
the first instance of the variable "pecn" in the HTML link does not get
inserted into the text.  The second instance of "pecn" gets inserted ok into
that HTML link.   What is the right way to do this or is there a better way
in general?

This my code:

pecn = "5022543"
pecn_project = "F876540"
pdesc = "some sort of ECN for who knows what reason!"

newMail.HTMLBody = """

ECN number """+pecn+""" on Project """+pecn_project+""" has been closed.
Below is a link to the ECN on the intranet.
http://web_server_at_work.com/cgi-bin/part-url.cgi?ecn=";
"""+pecn+""" "">"""+pecn+"""
ECN Description:
"""+pdesc+"""

ECN Effect of Change Comments:
(still need to work on this part)

R&D Information:
(still need to work on this part)

"""
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] inserting variables into a docstring

2010-08-21 Thread Hugo Arts
On Sat, Aug 21, 2010 at 11:10 PM, Bill Allen  wrote:
> I am trying to build an HTML body text for an email using a docstring with
> variables inserted a certain points.  It all works just fine, except that
> the first instance of the variable "pecn" in the HTML link does not get
> inserted into the text.  The second instance of "pecn" gets inserted ok into
> that HTML link.   What is the right way to do this or is there a better way
> in general?
>

The variable is inserted just fine for me, though there's some
problems with the  tag, because you shouldn't surround GET
variables with quotation marks, e.g. you should do this:

http://example.com/?ecn=423434
NOT this:
http://example.com/?ecn="423434";

If you do this, the HTML parser will encounter the first ", think that
it's come to the end of the href="" attribute, and ignore the rest.
But that's an HTML issue, not a python one.

As an aside, consider using string formatting operations like so:

newMail.HTMLBody = """

ECN number {0} on Project {1} has been closed.
Below is a link to the ECN on the intranet.
http://web_server_at_work.com/cgi-bin/part-url.cgi?ecn={0}";>{0}
ECN Description:
{2}""".format(pecn, pecn_project, pdesc)

Hugo
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] inserting variables into a docstring

2010-08-21 Thread Bill Allen
On Sat, Aug 21, 2010 at 11:27 PM, Hugo Arts  wrote:

>
> The variable is inserted just fine for me, though there's some
> problems with the  tag, because you shouldn't surround GET
> variables with quotation marks, e.g. you should do this:
>
> http://example.com/?ecn=423434
> NOT this:
> http://example.com/?ecn="423434";
>
> If you do this, the HTML parser will encounter the first ", think that
> it's come to the end of the href="" attribute, and ignore the rest.
> But that's an HTML issue, not a python one.
>
> As an aside, consider using string formatting operations like so:
>
> newMail.HTMLBody = """
> 
> ECN number {0} on Project {1} has been closed.
> Below is a link to the ECN on the intranet.
>  href="http://web_server_at_work.com/cgi-bin/part-url.cgi?ecn={0}
> ">{0}
> ECN Description:
> {2}""".format(pecn, pecn_project, pdesc)
>
>
> Hugo,

Thank you for the reminder of the problem with the HTML.  I see that now.
You are right, however, using that format method works wonderfully and is
definitely the way to go!   I see how I can apply that in lots of ways.

Thanks for the help,
Bill
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 78, Issue 99 -- Prime numbers

2010-08-21 Thread Steven D'Aprano
On Sun, 22 Aug 2010 01:54:43 pm Nick wrote:

> I was interested in this specific topic as I've been working some
> problems on project euler. I've been trying to generate prime numbers
> as a first step to go about solving a problem.  I don't have a
> particular question about solving the problem, but want to ask about
> a relationship.
>
> The very first problem for projecteuler.net makes you write a program
> that finds all factors of 3 and 5 for n < 1000.  Were they trying to
> lead me down the path you're alluding to?  I'm not seeing the
> connection between that particular problem and finding primes.  I
> would appreciate more insight.  Thanks everyone!

Think about the definition of prime numbers. A number is prime if it has 
no factors other than itself and 1. (Except for 1, which is excluded by 
definition.)

So if you know that these numbers are divisible by 3:

3, 6, 9, 12, 15, 18, 21, ... 

and these numbers are divisible by 5:

5, 10, 15, 20, 25, 30, ... 

then you know that apart from the first number in each list (3 and 5) 
none of the other numbers could be prime. 12, for example, is not prime 
because it is divisible by 3 (also by 2), and 15 is not prime because 
it is divisible by 5 (also by 3).

More importantly, the code you write to detect *non-primes* will be 
quite similar to the code you write to detect primes. They're nearly 
checking the same thing:

If n is divisible by 3, then it goes in the "multiples of 3" list.
If n is divisible by 5, then it goes in the "multiples of 5" list.
If n is divisible by 7, then it goes in the "multiples of 7" list.
...

Compare that to primes:

If n is divisible by 2 (apart from 2 itself), then it is not a prime.
If n is divisible by 3 (apart from 3 itself), then it is not a prime.
If n is divisible by 4 (apart from 4 itself), then it is not a prime.
If n is divisible by 5 (apart from 5 itself), then it is not a prime.
If n is divisible by 6 (apart from 6 itself), then it is not a prime.
If n is divisible by 7 (apart from 7 itself), then it is not a prime.
...
Otherwise, if we get to n, and there are no other factors, then it is a 
prime.

But wait, this is doing too much work. If a number is divisible by 4, 
then it's also divisible by 2. Likewise if it is divisible by 6, 8, 10, 
12, ... any even number. So we eliminated all the even numbers (other 
than 2 itself) and there's no point in testing for divisibility by 4, 
6, 8, ...


If n is divisible by 2 (apart from 2 itself), then it is not a prime.
If n is divisible by 3 (apart from 3 itself), then it is not a prime.
If n is divisible by 5 (apart from 5 itself), then it is not a prime.
If n is divisible by 7 (apart from 7 itself), then it is not a prime.
...
Otherwise, if we get to n, and there are no other factors, then it is a 
prime.

We're still doing too much work. Why go all the way up to n? We know 
that (say) 93 can't possibly divide into 99, or 57 into 59. The largest 
number we need to check is the square root of n. The reason is a little 
subtle, so think about it, don't just take my word.

Hint: write down the factors of, say, 60: Can you see the pattern?

2*30, 3*10, 5*6, 6*5, 10*3, 30*2




-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 78, Issue 100 -- Prime numbers using square root of n

2010-08-21 Thread Nick
"We're still doing too much work. Why go all the way up to n? We know
that (say) 93 can't possibly divide into 99, or 57 into 59. The largest
number we need to check is the square root of n. The reason is a little
subtle, so think about it, don't just take my word.

Hint: write down the factors of, say, 60: Can you see the pattern?

2*30, 3*10, 5*6, 6*5, 10*3, 30*2
--
Steven D'Aprano"

Thanks so much Steven.  I get it all except I don't think I see the pattern.  I 
see that we're already covering those higher factors since they're divisble by 
2, 3, 5, etc.  I'm just not connecting the final dot with the square root of n. 
 It's almost there in my mind!  I just need a little more of a push!  Also I 
can write a program that tests whether the number is a factor of 2, 3, 5, 7, 
but I think you're trying to point to me that this is cumbersome and not 
necessary.  I just don't see the rest of the light quite yet.  I have been 
looking at code on the internet too, but there are lots of different ways to 
skin a cat and it seems people are taking advantage of that.  I keep hitting 
road blocks in code.  I want to understand!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 78, Issue 100 -- Prime numbers using square root of n

2010-08-21 Thread Steven D'Aprano
On Sun, 22 Aug 2010 03:47:21 pm Nick wrote:

> Thanks so much Steven.  I get it all except I don't think I see the
> pattern.  I see that we're already covering those higher factors
> since they're divisble by 2, 3, 5, etc.  I'm just not connecting the
> final dot with the square root of n.  It's almost there in my mind! 
> I just need a little more of a push!

Okay, let's consider an example: n = 100. We start checking factors of 
100 by dividing by 1, 2, 3, 4, ... and so on:

1 * 100 = 100
2 * 50 = 100
4 * 25 = 100
5 * 20 = 100
...

As the first factor increases, the second factor decreases 
proportionally. It has to, because the product is always 100. So, if 
you have one pattern going up (1, 2, 4, 5, ...), and another pattern 
going down (100, 50, 25, 20, ...), there must be a point where the two 
patterns cross! That point is at the square root:

10 * 10 = 100

Once you pass the square root, any factors you find will be the same as 
the factors you've already seen, only reversed:

20 * 5 = 100  # Seen this one before as 5*20.

Not all numbers have a whole number square root, but the principle still 
holds:


1 * 120 = 120
2 * 60 = 120
3 * 40 = 120
4 * 30 = 120
5 * 24 = 120
6 * 20 = 120
8 * 15 = 120
10 * 12 = 120
10.9545 * 10.9545 = 120
12 * 10 = 120  <=== already seen this one!



> Also I can write a program that 
> tests whether the number is a factor of 2, 3, 5, 7, but I think
> you're trying to point to me that this is cumbersome and not
> necessary.

Sort of. I'm not suggest that you create lists of multiples of 2, 3, 5, 
7 etc, and then cross off non-primes by checking to see if they are in 
one of those lists. That truly would be cumbersome and slow. But the 
two problems are similar. For simplicity, let's ignore 2 as a prime, 
and only think about odd numbers:

(1) For each odd number N between 3 and (say) 1000, check if it is a 
multiple of 3, 5, 7, 9, ... and IF SO, put it in the list "nonprimes".

versus:

(2) For each odd number N between 3 and (say) 1000, check if it is a 
multiple of 3, 5, 7, 9, ... and IF NOT, put it in the list "primes".


See, they are very nearly the same problem. The tricky bits are:

* dealing with 2 is a special case;

* you don't want to exclude (say) 3 from being a prime because it is 
divisible by 3; and

* you can stop testing at square-root of N.



-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor