spoiler 48g solution Re: mini-challenge Message #5 Posted by Allen on 24 Feb 2008, 5:51 p.m., in response to message #1 by Don Shepherd
Don, Thanks for an interesting challenge. I think this may be smaller on the 42S, but I had a 48GX on the desk, so here is an unoptimized 48g solution.
I use a 'corner and sides' approach whereby the FLOOR and CEIL of the Square roots are used to generate the corners and then determine from the max corner which side the input resides on.
NOTE: Assumes 1 and 2 are trivial cases not to be calculated. e.g. if input=1 then {2 4 6 8} and likewise for 2. The program (not optimized for either size or speed):
Bytes: 255
Checksum:#64353d
Variables used: 1 Local, 0 Global
Flags used: 01
<< 1 CF -> N ; set up FLAG1 as odd/even increment for Floor/Ceil loop
<< 2 2 ; This skips 1 and 2 as input ( see assumptions)
DO DUP SQRT 1 ; Main loop to determine the corners ( 7,10,13,17,21...)
IF FS? ; Test for first or second corner between
THEN CIEL 1 CF ; perfect squares.
ELSE FLOOR 1 SF ; Corner numbers are alternate
END ; cv2=cv1+floor(sqrt(cv1)) OR
+ ; cv2=cv1+ ceil(sqrt(cv1))
SWAP 1 + SWAP ; increment corner counter
UNTIL DUP N >= ; Stop if you are at the corner or side
END ; gives side number(s) and corner value
IF DUP N == ; if the input IS the corner number
THEN SWAP 2 * 3 + ; use corner formula
DUP 2 + ->LIST ; {2*s+3 2*s+5}
ELSE SWAP 4 - ; else use side formula:
{-2 2} * {-3 11} ADD ; {-3-2*(s-4) 11+2*(s-4)}
END
{-1 1} + ; All solutions have N+1 and N-1
DUP DUP / N * ADD ; Adding N to all list values {-1 1 s1 s2)
SWAP DROP SORT ; Clean stack and sort
>>
>>
Example (sorted) results:
4 {1 3 5 15}
5 {4 6 16 18}
9 {2 8 10 24}
35 {16 34 36 62}
36 {17 35 37 63}
151 {106 150 152 204}
edited to add comments
Edited: 24 Feb 2008, 6:17 p.m.
|