Find square difference: Difference between revisions

→‎{{header|ALGOL 68}}: Tweak to make it more obvious that a loop is not being used to find the least square...
(→‎{{header|ALGOL 68}}: Tweak to make it more obvious that a loop is not being used to find the least square...)
Line 43:
Also shows the least positive integer where the difference between n^2 and (n-1)^2 is greater than 32 000 and 2 000 000 000.
<syntaxhighlight lang="algol68">BEGIN # find the lowest positive n where the difference between n^2 and (n-1)^2 is > 1000 #
# shows the smalled square n where n^2 - (n-1)^2 is greater than required difference #
[]INT test = ( 1 000, 32 000, 2 000 000 000 );
# # n^2 - ( n - 1 )^2 is n^2 - n^2 + 2n - 1, i.e. 2n - 1 #
FOR i FROM LWB test TO UPB test DO
# INT so 2n - 1 > required difference =or n > ( required difference + 1 ) / 2 test[ i ];#
PROC show least square = ( INT required difference )VOID:
# n^2 - ( n - 1 )^2 is n^2 - n^2 + 2n - 1, i.e. 2n - 1 #
# so 2n - 1 > required difference or n > ( required difference + 1 ) / 2 #
print( ( "Smallest n where n^2 - (n-1)^2 is > ", whole( required difference, -12 )
, " is: ", whole( ( ( required difference + 1 ) OVER 2 ) + 1, -12 )
, newline
)
);
show least square( 1 000 );
OD
show least square( 32 000 );
END</syntaxhighlight>
[]INTshow test =least square( 1 000, 32 000, 2 000 000 000 );
END
END</syntaxhighlight>
{{out}}
<pre>
3,021

edits