您的位置:首页 > 编程语言

Paradigms of Computer Programming,编程模式学习中递归调用的练习02-计算素数

2014-03-25 17:03 387 查看
This exercise is focused on the use of accumulators. Using accumulators allows you to keep a stack constant size during the recursion calls.

In this exercise, you are asked to use accumulators in order to determine if a number is prime. A prime number is a number that can be divided only by 1 and by itself (euclidean division without remainder).

Consider your code in the following template:
fun {Prime N}
[YOUR CODE]
end
{Prime N}


Please note that 1 is not considered as a prime number ({Prime 1} == false) and that N >= 1.

PRIME

(1/1 point)

You are asked to provide the body of the Prime function.

Write the Prime function, which returns true if N is a prime number and false otherwise.

Consider the following signature:

我的答案:评分为正确!
fun {Prime N}


1

if N==1 then false


2

elseif N>1 then J N1 in


3

for J in 2..N/2 collect:C do


4

if  N mod J == 0 then


5

N1 = N/J


6

end


7

end


8

end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: