重复十进制算法

时间:2013-01-26 02:26:36

标签: javascript

我正在尝试编写一个简单的重复十进制算法。现在我非常接近有一些有用的东西。

我尝试使用此算法:How to know the repeating decimal in a fraction?

  

“一个非常简单的算法就是:实现长除法。记录   你做的每个中级部门。只要你看到一个师   与你以前做过的一样,你就拥有了   重复“。

除了检测重复的十进制模式并将其放在括号中之外,我能够完成上述所有操作。

对于7/13分数,我的输出应为0. [538461]现在它是0,5,3,8,4,6,1,5,3,8,4,6,1,5,3,8,4,6,1,5。

有关如何使用上面提到的算法实现检测重复小数模式并将其放入括号的任何建议?我知道有类似的问题,但我想使用我上面提到的算法用我当前的代码实现它。

<script>
// All the prime numbers under 1,000
var primeNumbers = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997];

// Finds all the prime factors of a non-zero integer
// a = integer
function primeFactors(a) {
    var primeFactors = new Array(); 

    // Trial division algorithm
    for (var i = 0, p = primeNumbers[i]; i < primeNumbers.length && p * p <= a; i++, p = primeNumbers[i]) {
        while (a % p == 0) {         
                primeFactors.push(p);

                a /= p;
        }
    }

    if (a > 1) {
        primeFactors.push(a);
    }

    return primeFactors;
}

// Converts a fraction to a decimal
// i = number
// n = numerator
// d = denominator
function fractionToDecimal(n, d) {
    var pFS = primeFactors(d);

    for (var i = 0; i < pFS.length; i++) { // Go through each of the denominators prime factors

        if (pFS[i] !== 2 && pFS[i] !== 5) { // We have a repeating decimal

            var output = new Array();

            // Let's find the repeating decimal
            // Repeating decimal algorithm - uses long division
            for (var i = 0; i < 20; i++) { // For now find 20 spots, ideally this should stop after it finds the repeating decimal

                // How many times does the denominator go into the numerator evenly
                var temp2 = parseInt(n / d);

                output.push(temp2);

                var n = n % d;

                n += "0";
            }

            return "Repeating decimal: " + output;
        }
    }

    // Terminating decimal
    return "Terminating decimal: " + n / d;
}

document.write(fractionToDecimal(7, 13));
</script>

1 个答案:

答案 0 :(得分:3)

你已经弄明白了,只有两部分缺失:

  1. 检查长除法的分子是否重复并停止循环。当分子重复时,意味着我们找到了重复的小数。

  2. 将数组转换为不带逗号的字符串,这可以通过join('')轻松实现。

  3. 以下是您的代码的相关部分,其中实现了上述两点:

    function fractionToDecimal(n, d) {
        var pFS = primeFactors(d);
        for (var i = 0; i < pFS.length; i++) { // Go through each of the denominators prime factors
    
            if (pFS[i] !== 2 && pFS[i] !== 5) { // We have a repeating decimal
    
                var output = new Array();
                var ns = new Array();
    
                // Let's find the repeating decimal
                // Repeating decimal algorithm - uses long division
                for (var i = 0; i < 20; i++) { // For now find 20 spots, ideally this should stop after it finds the repeating decimal
                    // How many times does the denominator go into the numerator evenly
                    var temp2 = parseInt(n / d);
    
                    if (ns[n] === undefined) {
                        ns[n] = i;
                    } else {
                        return "Repeating decimal: " + 
                            output.slice(0, 1).join('') +
                            '.' +
                            output.slice(1, ns[n]).join('') +
                            '[' + output.slice(ns[n]).join('') + ']'
                        ;
                    }
    
                    output.push(temp2);
                    var n = n % d;
                    n += "0";
                }           
                return "Repeating decimal: " + output;
            }
        }
    
        // Terminating decimal
        return "Terminating decimal: " + n / d;
    }
    

    js完整代码:http://jsfiddle.net/49Xks/