9

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

我尝试使用此算法:如何知道分数中的重复小数?

“一个非常简单的算法是这样的:实现长除法。记录你所做的每个中间除法。一旦你看到一个与你以前做过的相同的除法,你就有了重复的东西。”

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

对于分数 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>
4

1 回答 1

4

你已经弄清楚了大部分,只有两个部分丢失:

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

  2. 将数组转换为不带逗号的字符串,这很容易通过使用join('').

这是实现上述两点的代码的相关部分:

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;
}

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

于 2013-01-26T06:56:14.493 回答