[Tutor] where I am going wrong?
Dave Angel
d at davea.name
Wed Dec 14 16:39:53 CET 2011
On 12/14/2011 09:29 AM, surya k wrote:
>
>
> ________________________________
>> From: waynejwerner at gmail.com
>> Date: Wed, 14 Dec 2011 08:25:53 -0600
>> Subject: Re: [Tutor] where I am going wrong?
>> To: suryak at live.com
>> CC: tutor at python.org
>>
>> On Wed, Dec 14, 2011 at 8:13 AM, surya k
>> <suryak at live.com<mailto:suryak at live.com>> wrote:
>>
>> This is a project Euler
>> puzzle. http://projecteuler.net/problem=30<http://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*10000)+ (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*10000)+ (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 at 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*10000)+ (q*1000) + (r*100) + (s*10) + (t*1)
if n == p**exp + q**exp + r**exp + s**exp + t**exp and p>1:
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
More information about the Tutor
mailing list