[Coco] Project Euler and the Coco

Arthur Flexser flexser at fiu.edu
Sat Nov 15 06:10:19 EST 2008


I think the limits aren't right.  Here's how I figure it:

Since we want a <= b < c, let b=a+d1 and c=b+d2, where d1 >= 0 and d2 > 0.  
Then a+b+c=1000 is equivalent to a+(a+d1)+((a+d1)+d2)=1000, or 3a+2d1+d2=1000.  
The minimum value of d1, the difference between b and a, is 0;  what is its
maximum?  For a given value of a, the maximum of d1 occurs when d2 assumes its
minimum value of one, giving d1=(1000-3a-1)/2.  So, I'd start with

 10 FOR A=1 TO 333 (why is A=1 excluded in the original?) 
 20 BH=A+INT((1000-3*A-1)/2): A2=A*A 
 30 FOR B=A TO BH 

etc.

Since this upper limit for B guarantees that C will exceed A and B, the check in
line 40 may be omitted.

I see no apparent reason why B must be in the range 334 to 667, as John
assumed.  Why exclude, for example, the abc combination 200, 300, 500, which
properly adds to 1000, from testing?

Art

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%...
> 
> I hope everyone has some fun with these,
> John
> 
> 
> --
> Coco mailing list
> Coco at maltedmedia.com
> http://five.pairlist.net/mailman/listinfo/coco
> 






More information about the Coco mailing list