2015-08-09 19:43:47 +00:00
|
|
|
|
2015-08-10 15:05:58 +00:00
|
|
|
VAR res : INT;
|
2015-08-09 19:43:47 +00:00
|
|
|
|
2015-08-10 11:55:52 +00:00
|
|
|
PUB main(x : INT) → c : INT
|
2015-08-10 14:37:16 +00:00
|
|
|
BEGIN
|
2015-08-12 10:15:47 +00:00
|
|
|
# factorial(10) → res;
|
2015-08-12 12:13:49 +00:00
|
|
|
# fibonacci(7) → res;
|
|
|
|
problem1(1000) → res;
|
2015-08-10 15:05:58 +00:00
|
|
|
END
|
|
|
|
|
|
|
|
PUB factorial(number : INT) → result : INT
|
|
|
|
BEGIN
|
|
|
|
IF number > 1 THEN
|
|
|
|
number * factorial(number - 1) → result;
|
2015-08-12 10:15:47 +00:00
|
|
|
ELSE
|
|
|
|
1 → result;
|
|
|
|
END
|
|
|
|
|
|
|
|
# Recursive test
|
|
|
|
PUB fibonacci(n : INT) → f : INT
|
|
|
|
BEGIN
|
|
|
|
IF n = 0 THEN
|
|
|
|
0 → f;
|
|
|
|
ELSEIF n = 1 THEN
|
|
|
|
1 → f;
|
|
|
|
ELSE
|
|
|
|
fibonacci(n - 1) + fibonacci(n - 2) → f;
|
2015-08-10 14:37:16 +00:00
|
|
|
END
|
2015-08-12 12:13:49 +00:00
|
|
|
|
|
|
|
# If we list all the natural numbers below 10 that are
|
|
|
|
# multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of
|
|
|
|
# these multiples is 23.
|
|
|
|
# Find the sum of all the multiples of 3 or 5 below 1000.
|
|
|
|
PUB problem1(max : INT) → r : INT | iter : INT
|
|
|
|
BEGIN
|
|
|
|
1 → iter;
|
|
|
|
0 → r;
|
|
|
|
WHILE iter < max DO
|
2015-08-13 12:56:50 +00:00
|
|
|
# FOR iter FROM 1 TO max DO
|
2015-08-12 12:13:49 +00:00
|
|
|
BEGIN
|
|
|
|
IF (iter % 5) = 0 THEN
|
|
|
|
r + iter → r;
|
|
|
|
ELSEIF (iter % 3) = 0 THEN
|
|
|
|
r + iter → r;
|
|
|
|
iter + 1 → iter;
|
|
|
|
END
|
|
|
|
END
|