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:
Please note that 1 is not considered as a prime number ({Prime 1} == false) and that N >= 1.
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:
我的答案:评分为正确!
1
2
3
4
5
6
7
8
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
相关文章推荐
- Paradigms of Computer Programming,编程模式学习中递归调用的练习01
- 一步步学习java并发编程模式之Active Object模式(二)java实现异步调用
- jena->arq学习1--arq编程调用模式
- C/C++编程小练习 计算10亿之内的素数和(合数筛选算法实现)
- 递归调用输出元素 分类: python 小练习 python基础学习 2013-06-30 11:54 278人阅读 评论(0) 收藏
- C语言学习历程——编程练习2——02
- C语言学习历程——编程练习3——02
- python核心编程学习笔记-2016-07-23-02-切片索引
- unix 学习笔记-----Unix编程练习实例----读目录的实例_1
- TSS----linux内核学习第四章保护模式及其编程
- 算法学习笔记——函数调用、递归以及栈-part 1
- 【嵌入式系统学习记录】练习:通过编程实现,统计1~n有多少个9
- LeetCode编程练习 - Majority Element学习心得
- LeetCode编程练习 - Linked List Cycle学习心得
- 设计模式学习笔记02
- python编程练习---递归输出杨辉三角
- 马士兵java视频学习笔记第二章:递归调用
- 【学习】【保护模式编程、九】
- LeetCode编程练习 - Power of Three学习心得
- c#网络编程学习笔记02_Tcp编程(上)