Re: [Tutor] where I am going wrong?

2011-12-15 Thread bob gailer

Suggestions for potentially simpler and more efficient code.

Create a ruple of the 5th powers of the 10 digits and look them up 
(should be faster than recomputing the 5th power each time) (0, 1, 16, ... )


Instead of trying all permutations of digits, use combinations. 12345 
will yield the same sum-of-5th-powers  as 23154. Compute the sum then 
see if it is composed of the source digits.


--
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] where I am going wrong?

2011-12-14 Thread surya k

This is a project Euler puzzle. http://projecteuler.net/problem=30
I applied brute force way and here is my code
k=0for p in range(1,10):    for q in range(0,10):        for r in range(0,10):  
          for s in range(0,10):                for t in range(0,10):            
     n = (p*1)+ (q*1000) + (r*100) + (s*10) + (t*1)                 if n == 
\                     p**5 + q**5 + r**5 + s**5 + t**5:                        
print n                        k+=nprint k
My answer: 240559
But its showing the answer as wrong!!.
I used the same method on the example puzzle and it worked. 
  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] where I am going wrong?

2011-12-14 Thread Wayne Werner
On Wed, Dec 14, 2011 at 8:13 AM, surya k sur...@live.com wrote:


 This is a project Euler puzzle. http://projecteuler.net/problem=30
 I applied brute force way and here is my code
 k=0for p in range(1,10):for q in range(0,10):for r in
 range(0,10):for s in range(0,10):for t in
 range(0,10): n = (p*1)+ (q*1000) + (r*100) + (s*10) +
 (t*1) if n == \ p**5 + q**5 + r**5 +
 s**5 + t**5:print n
 k+=nprint k
 My answer: 240559
 But its showing the answer as wrong!!.
 I used the same method on the example puzzle and it worked.


Your email client broke the formatting - you should probably use plain-text
or a pastebin.

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


Re: [Tutor] where I am going wrong?

2011-12-14 Thread surya k




 From: waynejwer...@gmail.com 
 Date: Wed, 14 Dec 2011 08:25:53 -0600 
 Subject: Re: [Tutor] where I am going wrong? 
 To: sur...@live.com 
 CC: tutor@python.org 
  
 On Wed, Dec 14, 2011 at 8:13 AM, surya k  
 sur...@live.commailto:sur...@live.com wrote: 
  
 This is a project Euler  
 puzzle. 
 http://projecteuler.net/problem=30http://projecteuler.net/problem%3d30 
 I applied brute force way and here is my code 
 k=0for p in range(1,10):for q in range(0,10):for r in  
 range(0,10):for s in range(0,10):for t in  
 range(0,10): n = (p*1)+ (q*1000) + (r*100) + (s*10)  
 + (t*1) if n == \ p**5 + q**5 +  
 r**5 + s**5 + t**5:print n   
k+=nprint k 
 My answer: 240559 
 But its showing the answer as wrong!!. 
 I used the same method on the example puzzle and it worked. 
  
 Your email client broke the formatting - you should probably use  
 plain-text or a pastebin. 
  
 -Wayne 

This is a project Euler puzzle. http://projecteuler.net/problem=30
I applied brute force way and here is my codek=0for p in range(1,10):        
for q in range(0,10):                  for r in range(0,10):                    
         for s in range(0,10):                                         for t in 
range(0,10):                                                  n = (p*1)+ 
(q*1000) + (r*100) + (s*10) + (t*1)                                             
     if n == p**5 + q**5 + r**5 + s**5 + t**5:                                  
                        k+=nprint kMy answer: 240559But its showing the answer 
as wrong!!.
I used the same method on the example puzzle and it worked. 
  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] where I am going wrong?

2011-12-14 Thread Peter Otten
surya k wrote:

 This is a project Euler puzzle. http://projecteuler.net/problem=30
 I applied brute force way and here is my codek=0for p in range(1,10): 
   for q in range(0,10):  for r in range(0,10):
 for s in range(0,10): 
for t in range(0,10): 
 n = (p*1)+ (q*1000) + (r*100) + (s*10) + (t*1)
  if n == p**5 + q**5 + r**5 + s**5 + t**5:
  k+=nprint kMy answer:

Suraya, you did it again. Your python code is formatted into a complete 
mess. If you don't follow Wayne's advice and post readable plaintext you'll 
consume a lot of goodwill quickly.

 240559But its showing the answer as wrong!!. I used the same method on the
 example puzzle and it worked.

Hint: you assume that all partial results have 5 digits, but they may have 
fewer or more.

For example you are missing

 4**5+1**5+5**5+0**5
4150


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


Re: [Tutor] where I am going wrong?

2011-12-14 Thread surya k




 From: sur...@live.com
 To: waynejwer...@gmail.com
 CC: tutor@python.org
 Subject: RE: [Tutor] where I am going wrong?
 Date: Wed, 14 Dec 2011 19:59:24 +0530




 
  From: waynejwer...@gmail.com
  Date: Wed, 14 Dec 2011 08:25:53 -0600
  Subject: Re: [Tutor] where I am going wrong?
  To: sur...@live.com
  CC: tutor@python.org
 
  On Wed, Dec 14, 2011 at 8:13 AM, surya k
  sur...@live.commailto:sur...@live.com wrote:
 
  This is a project Euler
  puzzle. 
  http://projecteuler.net/problem=30http://projecteuler.net/problem%3d30
  I applied brute force way and here is my code
  k=0for p in range(1,10): for q in range(0,10): for r in
  range(0,10): for s in range(0,10): for t in
  range(0,10): n = (p*1)+ (q*1000) + (r*100) + (s*10)
  + (t*1) if n == \ p**5 + q**5 +
  r**5 + s**5 + t**5: print n
  k+=nprint k
  My answer: 240559
  But its showing the answer as wrong!!.
  I used the same method on the example puzzle and it worked.
 
  Your email client broke the formatting - you should probably use
  plain-text or a pastebin.
 
  -Wayne


This is a project Euler puzzle. http://projecteuler.net/problem=30I applied 
brute force way and here is my codek=0for p in range(1,10):for q in 
range(0,10): for r in range(0,10):
for s in range(0,10):   for t in 
range(0,10): n = (p*1)+ 
(q*1000) + (r*100) + (s*10) + (t*1) 
   if n ==  p**5 + q**5 + r**5 + s**5 + t**5:   
  k+=n
print kMy answer: 240559But its showing the answer as wrong!!.I used the same 
method on the example puzzle and it worked. 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] where I am going wrong?

2011-12-14 Thread Dave Angel

On 12/14/2011 09:29 AM, surya k wrote:





From: waynejwer...@gmail.com
Date: Wed, 14 Dec 2011 08:25:53 -0600
Subject: Re: [Tutor] where I am going wrong?
To: sur...@live.com
CC: tutor@python.org

On Wed, Dec 14, 2011 at 8:13 AM, surya k
sur...@live.commailto:sur...@live.com  wrote:

This is a project Euler
puzzle. http://projecteuler.net/problem=30http://projecteuler.net/problem%3d30
I applied brute force way and here is my code
k=0for p in range(1,10):for q in range(0,10):for r in
range(0,10):for s in range(0,10):for t in
range(0,10): n = (p*1)+ (q*1000) + (r*100) + (s*10)
+ (t*1) if n == \ p**5 + q**5 +
r**5 + s**5 + t**5:print n
k+=nprint k
My answer: 240559
But its showing the answer as wrong!!.
I used the same method on the example puzzle and it worked.

Your email client broke the formatting - you should probably use
plain-text or a pastebin.

-Wayne

This is a project Euler puzzle. http://projecteuler.net/problem=30
I applied brute force way and here is my codek=0for p in range(1,10):
for q in range(0,10):  for r in range(0,10):
 for s in range(0,10): for t in 
range(0,10):  n = (p*1)+ 
(q*1000) + (r*100) + (s*10) + (t*1) 
 if n == p**5 + q**5 + r**5 + s**5 + t**5:  
k+=nprint kMy answer: 240559But its showing the answer 
as wrong!!.
I used the same method on the example puzzle and it worked. 

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

The new copy you posted was just as bad.  Please tell your email program 
not to reformat your text.  With Thunderbird,  I do that  by telling it 
to use plain-text format for any message that goes to the domain: 
python.org.  It's under File-preferences-Composition-General-Send 
Options.  First section Convert the message to plain text and second 
section, under PlainTextDomain, add  python.org


I tried to reflow the source.  But 59 columns of indentation is pretty 
wild.  So I also changed indentation, and maybe got it right.


Your code assumes all the numbers it will find will be 5 digit numbers, which 
is a strange assumption.  When I try it with **4, I get zero as the result.  
All the numbers for that case happen to be exactly 4 digits, while you have 5 
nested loops.  There's no reason to presuppose the number of digits.
I therefore changed the p range to 0,10, and the if statement to exclude the 
solution for 0 and 1.
I also added a global called exp, so we can run it unchanged on either 4 or 5.



exp = 4
k=0
for p in range(0,10):
  for q in range(0,10):
for r in range(0,10):
for s in range(0,10):
for t in range(0,10):
n = (p*1)+ (q*1000) + (r*100) + (s*10) + (t*1)
if n == p**exp + q**exp + r**exp + s**exp + t**exp and p1:
k+=n
print one part, n
print k

This still doesn't get the right answer, because it misses at least one 
number that has more than 5 digits.


If you just want to get on with it, you could nest several more 
for-loops. But you can readily put a coarse upper limit on the largest 
match, by trying something like  10* 9**5.  See how big that is, and if 
it's less than 10 digits long, you have an upper limit. Now you know how 
many loops you need.


There are other approaches that don't require you to make a large number 
of nested loops.  One is to use recursion.  Another is to just iterate 
through the integers (n), then use str() and list() to turn it into a 
bunch of digits, which you then raise to the appropriate power in a 
simple loop.



--

DaveA

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


Re: [Tutor] where I am going wrong?

2011-12-14 Thread Robert Sjoblom
surya k wrote:
 This is a project Euler puzzle. http://projecteuler.net/problem=30
 I applied brute force way and here is my codek=0for p in range(1,10):
 for q in range(0,10):  for r in range(0,10):  
for s in range(0,10): for 
 t in range(0,10):  n = 
 (p*1)+ (q*1000) + (r*100) + (s*10) + (t*1)
   if n == p**5 + q**5 + r**5 + s**5 + t**5:   
k+=nprint kMy answer: 240559But its 
 showing the answer as wrong!!.
 I used the same method on the example puzzle and it worked.

Your formatting is still broken, here's how I suspect you want it to look:

k=0
for p in range(1,10):
for q in range(0,10):
for r in range(0,10):
for s in range(0,10):
for t in range(0,10):
n = (p*1)+ (q*1000) + (r*100) + (s*10) + t
if n == p**5 + q**5 + r**5 + s**5 + t**5:
#print(n)
k += n
print(k)

What Peter said is very important too. I very much doubt you'll find
the solution with those loops.

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


Re: [Tutor] where I am going wrong?

2011-12-14 Thread surya k



 Date: Wed, 14 Dec 2011 10:39:53 -0500
 From: d...@davea.name
 To: sur...@live.com
 CC: waynejwer...@gmail.com; tutor@python.org
 Subject: Re: [Tutor] where I am going wrong?
 
 On 12/14/2011 09:29 AM, surya k wrote:
 
 
  
  From: waynejwer...@gmail.com
  Date: Wed, 14 Dec 2011 08:25:53 -0600
  Subject: Re: [Tutor] where I am going wrong?
  To: sur...@live.com
  CC: tutor@python.org
 
  On Wed, Dec 14, 2011 at 8:13 AM, surya k
  sur...@live.commailto:sur...@live.com  wrote:
 
  This is a project Euler
  puzzle. 
  http://projecteuler.net/problem=30http://projecteuler.net/problem%3d30
  I applied brute force way and here is my code
  k=0for p in range(1,10):for q in range(0,10):for r in
  range(0,10):for s in range(0,10):for t in
  range(0,10): n = (p*1)+ (q*1000) + (r*100) + (s*10)
  + (t*1) if n == \ p**5 + q**5 +
  r**5 + s**5 + t**5:print n
  k+=nprint k
  My answer: 240559
  But its showing the answer as wrong!!.
  I used the same method on the example puzzle and it worked.
 
  Your email client broke the formatting - you should probably use
  plain-text or a pastebin.
 
  -Wayne
  This is a project Euler puzzle. http://projecteuler.net/problem=30
  I applied brute force way and here is my codek=0for p in range(1,10):   
   for q in range(0,10):  for r in range(0,10):   
for s in range(0,10): 
  for t in range(0,10):  n = 
  (p*1)+ (q*1000) + (r*100) + (s*10) + (t*1)  
  if n == p**5 + q**5 + r**5 + s**5 + t**5:   
 k+=nprint kMy answer: 240559But 
  its showing the answer as wrong!!.
  I used the same method on the example puzzle and it worked. 
  
  ___
  Tutor maillist  -  Tutor@python.org
  To unsubscribe or change subscription options:
  http://mail.python.org/mailman/listinfo/tutor
 
 The new copy you posted was just as bad.  Please tell your email program 
 not to reformat your text.  With Thunderbird,  I do that  by telling it 
 to use plain-text format for any message that goes to the domain: 
 python.org.  It's under File-preferences-Composition-General-Send 
 Options.  First section Convert the message to plain text and second 
 section, under PlainTextDomain, add  python.org
 
 I tried to reflow the source.  But 59 columns of indentation is pretty 
 wild.  So I also changed indentation, and maybe got it right.
 
 Your code assumes all the numbers it will find will be 5 digit numbers, which 
 is a strange assumption.  When I try it with **4, I get zero as the result.  
 All the numbers for that case happen to be exactly 4 digits, while you have 5 
 nested loops.  There's no reason to presuppose the number of digits.
 I therefore changed the p range to 0,10, and the if statement to exclude the 
 solution for 0 and 1.
 I also added a global called exp, so we can run it unchanged on either 4 or 5.
 
 
 
 exp = 4
 k=0
 for p in range(0,10):
for q in range(0,10):
  for r in range(0,10):
  for s in range(0,10):
  for t in range(0,10):
  n = (p*1)+ (q*1000) + (r*100) + (s*10) + (t*1)
  if n == p**exp + q**exp + r**exp + s**exp + t**exp and 
 p1:
  k+=n
  print one part, n
 print k
 
 This still doesn't get the right answer, because it misses at least one 
 number that has more than 5 digits.
 
 If you just want to get on with it, you could nest several more 
 for-loops. But you can readily put a coarse upper limit on the largest 
 match, by trying something like  10* 9**5.  See how big that is, and if 
 it's less than 10 digits long, you have an upper limit. Now you know how 
 many loops you need.
 
 There are other approaches that don't require you to make a large number 
 of nested loops.  One is to use recursion.  Another is to just iterate 
 through the integers (n), then use str() and list() to turn it into a 
 bunch of digits, which you then raise to the appropriate power in a 
 simple loop.
 
 
 -- 
 
 DaveA
 

Thanks Dave for putting that much effort.Actually, I was using plane text 
format.. I wonder what went wrong!
So, regarding the program, you assumed it correctly. Let me do it again as you 
said.. I'll let you know if there is any problem
Thanks___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor