This question already has an answer here:
这个问题在这里已有答案:
- Calling a javascript function recursively 5 answers
以递归方式调用javascript函数5个答案
projectEuler3(600851475143);
var projectEuler3 = function (composite) {
var result = 2;
while(composite > 1)
{
if (composite % result)
{
result++
} else {
composite = composite / result;
console.log(result);
}
}
};
3 个解决方案
#1
Hopefully in the spirit both of Project Euler (learn-by-doing) and SO (learn-by-asking), here's a fairly generic template for making a loop into tail-call recursion:
希望在项目Euler(从实践中学习)和SO(通过询问学习)的精神中,这是一个相当通用的模板,用于循环到尾调用递归:
var projectEuler3 = function(n) {
var pE3_inner = function(n, i) {
// magic happens here
// if neither n nor i has changed, this is an
// infinite recursion (and usually a stack overflow)
pE3_inner(n, i);
}
pE3_inner(n, 2);
}
#2
EDIT: Turns out this question is designed to be a how to change an algorithm (in general) from iterative to recursive...not, as I assumed, how to get an answer to a particular problem.
编辑:原来这个问题的目的是如何将算法(通常)从迭代更改为递归...而不是像我假设的那样,如何得到特定问题的答案。
This is for Project Euler! Project Euler is purely for your own benefit...if you're going to have someone else do the work for you, you could probably just look up the answer on google somewhere and type it in.
这是项目欧拉!项目Euler纯粹是为了你自己的利益...如果你打算让其他人为你做这项工作,你可能只是在谷歌的某个地方查找答案并输入。
I can only guess you've decided that your program is correct, but that it is too slow, and you're hoping to speed it up. Making your function recursive won't help.
我只能猜测你已经确定你的程序是正确的,但它太慢了,你希望加快它的速度。使函数递归无济于事。
The site points out that if your program doesn't solve the question quickly, there's a better approach...try a different tact.
该网站指出,如果你的程序没有快速解决问题,那么有一个更好的方法...尝试不同的机智。
#3
Structure and Interpretation of Computer Programs introduces the difference between iterative and recursive algorithms, how to identify which is which (in some languages it's not so obvious!), and how to convert from one to the other.
计算机程序的结构和解释介绍了迭代算法和递归算法之间的区别,如何识别哪些(在某些语言中它不是那么明显!),以及如何从一个转换为另一个。
In addition to being an excellent textbook.
除了是一本优秀的教科书。
#1
Hopefully in the spirit both of Project Euler (learn-by-doing) and SO (learn-by-asking), here's a fairly generic template for making a loop into tail-call recursion:
希望在项目Euler(从实践中学习)和SO(通过询问学习)的精神中,这是一个相当通用的模板,用于循环到尾调用递归:
var projectEuler3 = function(n) {
var pE3_inner = function(n, i) {
// magic happens here
// if neither n nor i has changed, this is an
// infinite recursion (and usually a stack overflow)
pE3_inner(n, i);
}
pE3_inner(n, 2);
}
#2
EDIT: Turns out this question is designed to be a how to change an algorithm (in general) from iterative to recursive...not, as I assumed, how to get an answer to a particular problem.
编辑:原来这个问题的目的是如何将算法(通常)从迭代更改为递归...而不是像我假设的那样,如何得到特定问题的答案。
This is for Project Euler! Project Euler is purely for your own benefit...if you're going to have someone else do the work for you, you could probably just look up the answer on google somewhere and type it in.
这是项目欧拉!项目Euler纯粹是为了你自己的利益...如果你打算让其他人为你做这项工作,你可能只是在谷歌的某个地方查找答案并输入。
I can only guess you've decided that your program is correct, but that it is too slow, and you're hoping to speed it up. Making your function recursive won't help.
我只能猜测你已经确定你的程序是正确的,但它太慢了,你希望加快它的速度。使函数递归无济于事。
The site points out that if your program doesn't solve the question quickly, there's a better approach...try a different tact.
该网站指出,如果你的程序没有快速解决问题,那么有一个更好的方法...尝试不同的机智。
#3
Structure and Interpretation of Computer Programs introduces the difference between iterative and recursive algorithms, how to identify which is which (in some languages it's not so obvious!), and how to convert from one to the other.
计算机程序的结构和解释介绍了迭代算法和递归算法之间的区别,如何识别哪些(在某些语言中它不是那么明显!),以及如何从一个转换为另一个。
In addition to being an excellent textbook.
除了是一本优秀的教科书。