重复十进制算法

我试图写一个简单的重复小数算法。 现在我非常接近有效的东西。

我试图使用这个算法:如何知道小数中的重复小数?

“一个非常简单的算法就是:实施长期分工,记录你所做的每一个中间分工,只要你看到一个与你之前完成的分工相同的分工,你就有重复的事情。”

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

对于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>

你已经发现了大部分,只有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/

    链接地址: http://www.djcxy.com/p/11759.html

    上一篇: Repeating Decimal Algorithm

    下一篇: How to cache android.graphics.path or Bitmap when using Overlays?