简体   繁体   English

使用 javascript 的数字的不同阶乘

[英]Different factorial of a number using javascript

I'm trying to come up with a function that gives me a "factorial" of a number but in a different way.我正在尝试提出一个 function,它以不同的方式给我一个数字的“阶乘”。 For example:例如:

15. = 15*14/13+12-11*10/9... and so on. 15. = 15*14/13+12-11*10/9...等等。

I tried doing this by first creating an array and, then, a loop to get to the result.我尝试通过首先创建一个数组,然后创建一个循环来获得结果来做到这一点。 But it doesn't work as expected.但它没有按预期工作。 The result expected from the code below is 13.下面代码的预期结果是 13。

function createList(n) {
    let input = n
    let list = [input]

    for(i = input-1; i >= 1; i--) {
        list.push(i)
    }
    return list
}



function factorialDiff() {
    let elements = createList(12)
    var res = elements.shift()
    while(elements.length >= 1) {
    if(elements.length == 0) {
        return res
    }
    res *= elements.shift()
    if(elements.length == 0) {
        return res
    }
    res /= elements.shift()
    if(elements.length == 0) {
        return res
    }
    res += elements.shift()
    if(elements.length == 0) {
        return res
    }
    res -= elements.shift()
    if(elementos.length == 0) {
        return res
    }
    }
    return res 
}
console.log(factorialDiff())
``



`

This is what your code is doing:这就是您的代码正在执行的操作:
console.log(((((12*11)/10+9-8)*7/6+5-4)*3)/2+1);

In math the convention is to solve multiplication/division first and plus/minus second in this order.在数学中,惯例是先解决乘法/除法,然后按此顺序解决加/减问题。 However, the code you did is solving the operations from left to right.但是,您所做的代码是从左到右解决操作。

For example:例如:

console.log(12*11/10+9-8*7/6+5-4*3/2+1); // returns 12.86

console.log(((((12*11)/10+9-8)*7/6+5-4)*3)/2+1); // returns 27.35 just like your function does

If your expected behavior is the first line, the following function should solve your problem:如果您的预期行为是第一行,则以下 function 应该可以解决您的问题:

function factorialDiff() {
    const elements = createList(12);
    let res1 = elements.shift();
    let res2 = elements.shift();
    let res3 = elements.shift();
    let res = (res1*res2/res3)
    while(elements.length >= 1) {
        res += elements.shift();
        if(elements.length == 0) {
            return res;
        }
        res1 = elements.shift();
        if(elements.length == 0) {
            return res*res1;
        }
        res2 = elements.shift();
        if(elements.length == 0) {
            return res*res1/res2;
        }
        res3 = elements.shift();
        res -= (res1*res2/res3);
        if(elements.length == 0) {
            return res;
        }        
    }
    return res;
}

Is this what you looking for?这是你要找的吗?

function myFactorial(num) {
        var result = num;
        operatorNum = 0;
        for(i = num - 1; i >= 1; i--){
            if(operatorNum % 4 == 0){ // "*"
                result = result * i;
            }else if (operatorNum % 4 == 1){  // "/"
                result = result / i;
            }else if (operatorNum % 4 == 2){ // "+"
                result = result + i;
            }else{ // "-"
                result = result - i;
            }
            operatorNum++;
        }
        return result;
    }
    console.log(myFactorial(4)); // logs 7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM