Triangle tools from the text book Programming Processors in Java.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
Triangle-Tools/programs/functions.tri

29 lines
497 B

! Print powers of integers.
let
func even (n : Integer) : Boolean ~
(n // 2) = 0;
func sqr (n : Integer) : Integer ~
n * n;
func power (b : Integer, n : Integer) : Integer ~
! assume n >= 0
if n = 0
then 1
else
if even (n)
then sqr (power (b, n/2))
else sqr (power (b, n/2)) * b;
var x : Integer;
var m : Integer
in
begin
getint (var x); getint (var m);
putint (x); put ('^'); putint (m);
put (' '); put ('='); put (' ');
putint (power (x, m));
puteol ()
end