[Coco] Project Euler and the Coco

John johnguin at hotmail.com
Sun Nov 16 00:44:31 EST 2008


I had the loops step down by 1:
FOR A = 333 to 2 STEP -1
FOR B = 667 TO 334 STEP -1

That led to the total iterations until the answer was found dropping from
40736 to ~24K because the A loop only runs 133 times instead of 198.  Other
people on this list have greatly optimized my brute force approach which is
great to see!

I was a little delayed answering since I started thinking about what you
wrote in an earlier:

"One formula for generating Pythagorean triangles is to use as sides 2XY,
(X^2-Y^2), and (X^2+Y^2), where X and Y can be any two integers with X>Y."

That led to some relatively simple algebra:
2xy + (x^2 - y^2) + (x^2 + y^2) =1000
2xy +2x^2 = 1000
x^2 + y = 500
x(x+y) = 500

Since x>y, it's pretty easy to look through the factors of 500 and come up
with x=20, y=5.

So the answers are
2xy = 2*20*5=200
x^2 - y^2 = 400-25 = 375 
x^2 + y^2 = 400+25 =  425

I felt really good about this until I read through the message board for
this particular problem and the fourth poster had algebraically solved it as
well - more than a year ago.

Still, it's a lot of fun to roll out BASIC programs to solve these problems.

John


-----Original Message-----
From: coco-bounces at maltedmedia.com [mailto:coco-bounces at maltedmedia.com] On
Behalf Of Art Flexser
Sent: Saturday, November 15, 2008 8:29 PM
To: CoCoList for Color Computer Enthusiasts
Subject: Re: [Coco] Project Euler and the Coco

On Fri, 14 Nov 2008, John wrote:

> Hello all,
> 
> I'm sure by now many of you have seen Project Euler"at
> http://projecteuler.net/.
> 
> If not, they have math problems to solve.  One example is the 9th easiest:
> Find the product of a,b,c such that a^2 + b^2 = c^2 and a+b+b=1000.
> 
> I used my Coco2 to solve this problem in 11 and a half minutes using the
> high speed poke.  At the risk of flooding the list with BASIC code, I hope
> my short program here will spur comments and optimizations:
> 
> 10 FOR A = 2 TO 333
> 20 FOR B = 334 TO 667
> 30 C = 1000 -A -B
> 40 IF C<A OR C<B THEN 60
> 50 IF A*A + B*B = C*C THEN 100
> 60 NEXT B,A
> 100 PRINT "A="A
> 110 PRINT "B="B
> 120 PRINT "C="C
> 130 PRINT "A  X  B  X  C  ="A*B*C
> 
> For example, one optimization became clear after I had the answer.  As a
> hint, I could cut run time by about 40%...
> 

So, what was the 40%-saving hint?

Art



--
Coco mailing list
Coco at maltedmedia.com
http://five.pairlist.net/mailman/listinfo/coco




More information about the Coco mailing list