Re: Programming exercise Message #9 Posted by Gerson W. Barbosa on 23 Aug 2012, 12:30 p.m., in response to message #8 by C.Ret
When I see this and other approaches here, mine looks quite silly! It could be done within two loops only once, instead I used two nested loops twice. Anyway, here it is, just for the records:
%%HP: T(3)A(R)F(,);
\<< 3, XROOT IP \-> r1
\<< { } 1, r1
FOR a a r1
FOR b a SQ a * b SQ b * + +
NEXT
NEXT OBJ\-> \->ARRY r1 SQ r1 + 2, / 1, r1 1, - \-> n e1 s1
\<< 1, 1, n 1, -
FOR i GETI UNROT i r1 1, + ==
IF
THEN s1 'r1' STO+ 1, 'e1' STO+ 's1' 1, STO-
END e1 r1 s1 \-> e2 r2 s2
\<< OVER i 1, + DUP n
FOR j GETI j r2 1, + ==
IF
THEN s2 'r2' STO+ 1, 'e2' STO+ 's2' 1, STO-
END 6, PICK ==
IF
THEN 5, PICK 1, IQUOT \->STR "=" + e1 1, IQUOT \->STR + "^3+" + 6, PICK e1 3, ^ - 3, XROOT 1, IQUOT \->STR + "^3=" + e2 1,IQUOT \->STR + "^3+" + 6, PICK e2 3, ^ - 3, XROOT 1, IQUOT \->STR + "^3" + 6, ROLLD
END
NEXT
\>> DROP2 ROT DROP
NEXT DROP
\>>
\>> DROP
\>>
40000 TN --> 7: "1729=1^3+12^3=9^3+10^3"
6: "4104=2^3+16^3=9^3+15^3"
5: "13832=2^3+24^3=18^3+20^3"
4: "39312=2^3+34^3=15^3+33^3"
3: "32832=4^3+32^3=18^3+30^3"
2: "40033=9^3+34^3=16^3+33^3"
1: "20683=10^3+27^3=19^3+24^3"
(after whopping 154 seconds on the emulator!)
|