beautypg.com

HP Prime Graphing Calculator User Manual

Page 533

background image

Programming

529

FOR DOWN STEP

Syntax: FOR var FROM start DOWNTO finish [STEP

increment] DO commands END;
Sets variable var to start, and for as long as this variable
is more than or equal to finish, executes the sequence of
commands, and then subtract increment from var.

WHILE

Syntax: WHILE test DO commands END;
Evaluate test. If result is true (not 0), executes the
commands, and repeat.
Example: A perfect number is one that is equal to the sum
of all its proper divisors. For example, 6 is a perfect
number because 6 = 1+2+3. The example below returns
true when its argument is a perfect number.

EXPORT ISPERFECT(n)
BEGIN
LOCAL d, sum;
2

d;

1

sum;

WHILE sum <= n AND d < n DO
IF irem(n,d)==0 THEN
sum+d

sum;

END;
d+1

d;

END;
RETURN sum==n;
END;

The following program displays all the perfect numbers up
to 1000:

EXPORT PERFECTNUMS()
BEGIN
LOCAL k;
FOR k FROM 2 TO 1000 DO
IF ISPERFECT(k) THEN
MSGBOX(k+" is perfect, press OK");
END;
END;
END;