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

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值