Re: TI-M: Even Square


[Prev][Next][Index][Thread]

Re: TI-M: Even Square




At 7:02 PM -0500 on 5/23/00, tfobrien wrote:
>Lets say someone enters a number N.  How do you find the next even perfect
>sqyare after that(using the calulator basic)?  If N were 8, the next even
>square would be 16.  Somebody mentioned a loop.

iPart(sqrt(n)+1)^2 gives the next square. So:

	iPart(sqrt(n)+1) -> x
	if (fPart(x/2)‚0):Then
		return (x+1)^2
	else
		return x^2
	end

Should work on any calculator (with possible replacement of 'return' with
suitable display function)

Now, for some fun. To remove the if (89/92 only):

	(ceiling(iPart(sqrt(n)+1)/2)*2)^2

But, now let's make it work everywhere. We need to get rid of that ceiling.
The 89, of course, will do this for us:

	4*(floor(-iPart(sqrt(n)+1)/2))^2

But 83's, for example, don't have a floor function, either. But rounding
down (in this case) is the same as ``int'', which they do have. So, you get:

	4*(int(-iPart(sqrt(n)+1)/2))^2

And thus, you now have one magical line of code that does it all. Now make
sure to put a nice comment by THAT one.

NOTE:
  ‚ is ``not equals''
  ceiling returns its argument, rounded up.
  floor returns its argument, rouded down.





Follow-Ups: References: