Euler #9

For the discussion of Perl, Python, Ruby, and PHP and other interpreted languages.

Euler #9

Hey everyone, I am on problem 9 of project Euler and I seem to be at a wall. I keep getting the wrong answer, but when I looked at my code, i can't find the flaw that causes the wrong answer. could anyone hint to me what it is?

Code: Select all
`max = 1000def main():   for a in xrange(1,max):      for b in xrange(1,max):         for c in xrange(1,max):            if triplit(a,b,c):               if (a+b+c) == max:                  return (a*b*c)def triplit(a,b,c):   if a<b and b<c:      if (a^2)+(b^2) == (c^2):         return 1   return 0print main()`
I study Gotafu.
Reason7194
Poster

Posts: 215
Joined: Fri Jan 07, 2011 5:01 pm
Blog: View Blog (0)

Re: Euler #9

Reason7194 wrote:Hey everyone, I am on problem 9 of project Euler and I seem to be at a wall. I keep getting the wrong answer, but when I looked at my code, i can't find the flaw that causes the wrong answer. could anyone hint to me what it is?<br><br>

How about you tell us what you're trying to even do? That might be a little helpful.

For those about to rock.

mShred
Administrator

Posts: 1477
Joined: Tue Jun 22, 2010 4:22 pm
Blog: View Blog (2)

Re: Euler #9

http://projecteuler.net/index.php?section=problems&id=9

I am trying to find the product of a,b, and c. a, b and c added must equal 1000 and a, b squared must equal 1000. a,b and c must also follow the Pythagorean triplet law of a < b < c.
I study Gotafu.
Reason7194
Poster

Posts: 215
Joined: Fri Jan 07, 2011 5:01 pm
Blog: View Blog (0)

Re: Euler #9

i think ^ doesn't work that way.

Code: Select all
`max = 1000def main():    for a in xrange(1,max):        print a;        for b in xrange(1,max):            for c in xrange(1,max):                if triplit(a,b,c):                    if (a+b+c) == max:                        print a;                        print b;                        print c;                        return (a*b*c)def triplit(a,b,c):    if a<b and b<c:        if (a*a)+(b*b) == (c*c):            return 1    return 0print main()`

waiting for answer.....

got it:
200
375
425
31875000

took 1 minute to find a problem
r-ID
Poster

Posts: 171
Joined: Mon Dec 29, 2008 6:04 pm
Blog: View Blog (0)

Re: Euler #9

Reason7194 wrote:
Code: Select all
`def triplit(a,b,c):   if a<b and b<c:      if (a^2)+(b^2) == (c^2):         return 1   return 0print main()`

It looks like you're trying to use ^ as an exponential operator, right? r-ID is right, that's not what ^ does (I believe it's the xor operator), his answer is also correct.

However, for future reference, the exponential operator is ** (two asterisks). There's also the pow function. Example
Code: Select all
`print 2 ** 3print pow(2, 3)`
"Well it isn't my fault. I shouldn't have been allowed to do something to crash it." "No, you shouldn't have been allowed to buy a computer in the first place"
Phantom Wolf
Poster

Posts: 274
Joined: Wed Mar 03, 2010 8:45 pm
Blog: View Blog (0)

Re: Euler #9

Reason7194 wrote:http://projecteuler.net/index.php?section=problems&id=9

I am trying to find the product of a,b, and c. a, b and c added must equal 1000 and a, b squared must equal 1000. a,b and c must also follow the Pythagorean triplet law of a < b < c.

I think i may know what you're doing wrong. a squared plus b squared must equal c squared, not 1000.

For those about to rock.

mShred
Administrator

Posts: 1477
Joined: Tue Jun 22, 2010 4:22 pm
Blog: View Blog (2)

Re: Euler #9

r-ID wrote:i think ^ doesn't work that way.

Code: Select all
`max = 1000def main():    for a in xrange(1,max):        print a;        for b in xrange(1,max):            for c in xrange(1,max):                if triplit(a,b,c):                    if (a+b+c) == max:                        print a;                        print b;                        print c;                        return (a*b*c)def triplit(a,b,c):    if a<b and b<c:        if (a*a)+(b*b) == (c*c):            return 1    return 0print main()`

waiting for answer.....

got it:
200
375
425
31875000

took 1 minute to find a problem

why did you do this: " if (a*a)+(b*b) == (c*c):" ?
I study Gotafu.
Reason7194
Poster

Posts: 215
Joined: Fri Jan 07, 2011 5:01 pm
Blog: View Blog (0)

Re: Euler #9

Reason7194 wrote:why did you do this: " if (a*a)+(b*b) == (c*c):" ?

Because, like i said, that is a conditional requirement for a Pythagorean triplet. a squared (a*a) plus b squared (b*b) must equal c squared (c*c).

For those about to rock.

mShred
Administrator

Posts: 1477
Joined: Tue Jun 22, 2010 4:22 pm
Blog: View Blog (2)

Re: Euler #9

So, a^2 + b^2 = c^2 wont work, but the weird a*a + b*b = c*c will? How does 'a*a' translate to 'a to the power of 2' ?

-- Wed Aug 03, 2011 5:02 pm --

OOOOO! Nevermind haha. I get it now
I study Gotafu.
Reason7194
Poster

Posts: 215
Joined: Fri Jan 07, 2011 5:01 pm
Blog: View Blog (0)

Return to Interpreted Languages

Who is online

Users browsing this forum: No registered users and 0 guests