输入一个正整数n,输出所有和为n连续正数序列(例如:15输出 [ [1,2,3,4,5 ] , [ 4,5,6 ] , [ 7,8 ] ] )

连续正数序列:公差为1的等差数列 

    function get_value(value) {
        let num = value
        let arr = []
        let newnum = 0
        if (num % 2 == 1) {
            arr.push([(num - 1) / 2, (num + 1) / 2])
            newnum = num + 1
        }
        let arr1 = []
        for (let i = 1; i < newnum / 2; i++) {
            arr1 = []
            for (let i1 = i; i1 < newnum / 2; i1++) {
                arr1.push(i1)
                let sum = 0
                for (let i2 = 0; i2 < arr1.length; i2++) {
                    sum += Number(arr1[i2])
                    if (sum == num) {
                        arr.push(arr1)
                        i1 = newnum / 2
                    } else if (sum > num) {
                        i1 = newnum / 2
                    } else {
                    }
                }

            }
        }
        console.log(arr)
    }
    get_value(21)

 

你可能感兴趣的:(js,日常随笔)