Euler #9

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

Euler #9

Post by Reason7194 on Wed Aug 03, 2011 3:21 am
([msg=60482]see Euler #9[/msg])

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 = 1000
def 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 0

print main()
I study Gotafu.
Reason7194
Poster
Poster
 
Posts: 215
Joined: Fri Jan 07, 2011 5:01 pm
Blog: View Blog (0)


Re: Euler #9

Post by mShred on Wed Aug 03, 2011 3:25 am
([msg=60483]see Re: Euler #9[/msg])

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.
Image

For those about to rock.
User avatar
mShred
Administrator
Administrator
 
Posts: 1747
Joined: Tue Jun 22, 2010 4:22 pm
Blog: View Blog (2)


Re: Euler #9

Post by Reason7194 on Wed Aug 03, 2011 5:17 am
([msg=60485]see Re: Euler #9[/msg])

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
Poster
 
Posts: 215
Joined: Fri Jan 07, 2011 5:01 pm
Blog: View Blog (0)


Re: Euler #9

Post by r-ID on Wed Aug 03, 2011 6:42 am
([msg=60486]see Re: Euler #9[/msg])

i think ^ doesn't work that way.

Code: Select all
max = 1000
def 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 0

print main()


waiting for answer.....

got it:
200
375
425
31875000

took 1 minute to find a problem
r-ID
Poster
Poster
 
Posts: 172
Joined: Mon Dec 29, 2008 6:04 pm
Blog: View Blog (0)


Re: Euler #9

Post by Phantom Wolf on Wed Aug 03, 2011 12:06 pm
([msg=60491]see Re: Euler #9[/msg])

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 0

print 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 ** 3
print 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
Poster
 
Posts: 271
Joined: Wed Mar 03, 2010 8:45 pm
Blog: View Blog (0)


Re: Euler #9

Post by mShred on Wed Aug 03, 2011 2:01 pm
([msg=60499]see Re: Euler #9[/msg])

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.
Image

For those about to rock.
User avatar
mShred
Administrator
Administrator
 
Posts: 1747
Joined: Tue Jun 22, 2010 4:22 pm
Blog: View Blog (2)


Re: Euler #9

Post by Reason7194 on Wed Aug 03, 2011 3:53 pm
([msg=60506]see Re: Euler #9[/msg])

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

Code: Select all
max = 1000
def 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 0

print 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
Poster
 
Posts: 215
Joined: Fri Jan 07, 2011 5:01 pm
Blog: View Blog (0)


Re: Euler #9

Post by mShred on Wed Aug 03, 2011 4:05 pm
([msg=60507]see Re: Euler #9[/msg])

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).
Image

For those about to rock.
User avatar
mShred
Administrator
Administrator
 
Posts: 1747
Joined: Tue Jun 22, 2010 4:22 pm
Blog: View Blog (2)


Re: Euler #9

Post by Reason7194 on Wed Aug 03, 2011 4:58 pm
([msg=60511]see Re: Euler #9[/msg])

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
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