学校要求我创建一个函数,找出在用户输入的范围内哪个整数产生最长的链。 “链”包括:
- 向用户询问号码。
- 如果是偶数,除以2。
- 如果是奇数乘以3加1。
- 继续这样做,直到达到 1。
例如如果您从 3 开始,则序列将为 3-10-5-16-8-4-2-1。该函数将打印出产生最长链的数字和该链中的实际整数(链的长度)。
这就是我目前所拥有的。我想不通。
function chain2()
var startNumber = prompt("Enter starting number of range", "4");
var endNumber = prompt ("Enter ending number of range", "9")
var separator="-";
while (currentNumber>1){
if (currentNumber%2==1){
currentNumber=currentNumber*3+1;
chain+=separator+currentNumber;
}
else if (currentNumber%2==0){
currentNumber=currentNumber/2;
chain+=separator+currentNumber;
}
}
return chain;
}
document.write(chain());
最佳答案
作为一项家庭作业,我应该插入你而不是仅仅给你答案。如果我要给你一个单一的建议,那就是你应该首先将你的工作分成易于理解的功能。编写一个可以完成所有工作的大函数是灾难的根源。你现在拥有的功能
- 给出两个用户输入提示
- 基于用户输入的循环
- 确定偶数和奇数
- 根据 Collatz 链程序执行基本算术
将其分解成单独的部分将使您更容易推理您的代码。我从我希望能够运行的顶层函数开始,然后从那里开始往下进行。
function FindMaxCollatzInRange(x,y) {
// max = ...
// ...
return max;
}
如果您发现自己需要其他功能,那是完全正常的。根据需要编写它们。
你的基本算法应该是这样的:
- 对于
min
和max
(含)之间的数字 ... - 计算并记录每个数字的 collatz 链长度
- 根据记录的长度,返回长度最大的数
So here's how I would do this. This is way beyond the scope of a JavaScript homework assignment, but I found it fun to work on.
I'm OK posting this code because I know it can't directly be copy/pasted as an answer for your assignment. My hope is that there will be some things here that will encourage you to explore some of the techniques used.
// compute the collatz chain for a given number, x
const collatz = Y (f=> y=> x=> {
if (x === 1) return append (x) (y);
if (isEven(x)) return f (append (x) (y)) (x/2);
else return f (append (x) (y)) (x*3 + 1);
}) ([]);
// compute the length of a collatz chain
const collatzLen = comp (len) (collatz);
// create a range from minimum, n, to maximum, m
const range = Y (f=> r=> n=> m=> {
if (n > m) return r;
else return f (append (n) (r)) (n+1) (m);
}) ([]);
// compute the maximum collatz chain length for a given range
const maxCollatz = (n,m) =>
comp (foldl (([x,xlen])=> ([y,ylen])=> ylen > xlen ? [y,ylen] : [x,xlen]) ([0,1]))
(map (n=> [n,collatzLen (n)]))
(range (n) (m));
这是函数的一些示例输出,因此您可以看到发生了什么
collatz (3)
//=> [ 3, 10, 5, 16, 8, 4, 2, 1 ]
collatzLen (3)
//=> 8
range (3) (10)
//=> [ 3, 4, 5, 6, 7, 8, 9, 10 ]
maxCollatz (3,10)
//=> [ 9, 20 ]
读取最后一个输出 [ 9, 20 ]
的方法是,“数字 9 具有最长的 Collatz 链长度,长度为 20”
简单地说,每个函数只负责做一件事。回答您问题的函数是 maxCollatz
函数,它使用 collatzLen
为 range
中的每个元素计算链长度列表。最后,减少列表以确定哪个数字具有最长的链。
这里是通用依赖
const comp = f=> g=> x=> f (g (x));
const mod = x=> y=> y % x;
const eq = x=> y=> y === x;
const isEven = comp (eq (0)) (mod (2));
const prop = x=> y=> y [x];
const len = prop ('length');
const concat = x=> xs=> xs .concat (x);
const append = x=> concat ([x]);
const foldl = f=> y=> xs=> xs .reduce ((x,y)=> f (x) (y), y);
const map = f=> xs=> xs .map (x=> f(x));
// U-combinator
const U = f=> f (f);
// Y-combinator
const Y = U (h=> f=> f (x=> h (h) (f) (x)));
补充阅读:
- > https://en.wikipedia.org/wiki/Functional_programming
- > https://en.wikipedia.org/wiki/Combinatory_logic
- > https://en.wikipedia.org/wiki/Function_composition
- > https://en.wikipedia.org/wiki/Fixed-point_combinator
- > https://en.wikipedia.org/wiki/Map_(higher-order_function)
- > https://en.wikipedia.org/wiki/Fold_(higher-order_function)
关于javascript - 如何创建特定的数字链,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36831125/