Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9191753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:45:42+00:00 2026-06-17T20:45:42+00:00

I’m trying to write a simple repeating decimal algorithm. Right now I’m pretty close

  • 0

I’m trying to write a simple repeating decimal algorithm. Right now I’m pretty close to having something that works.

I tried to use this algorithm: How to know the repeating decimal in a fraction?

“A very simple algorithm is this: implement long division. Record
every intermediate division you do. As soon as you see a division
identical to the one you’ve done before, you have what’s being
repeated.”

I was able to do all of the above except for detecting the repeating decimal pattern and putting it in brackets.

For the fraction 7/13 my output should be 0.[538461] right now it’s 0,5,3,8,4,6,1,5,3,8,4,6,1,5,3,8,4,6,1,5.

Any suggestions on how to implement detecting the repeating decimal pattern and putting it in brackets using the algorithm I mentioned above? I know there’s similar questions but I would like to implement it with my current code using the algorithm I mentioned above.

<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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:45:43+00:00Added an answer on June 17, 2026 at 8:45 pm

    You’ve got most of it figured out, only 2 parts are missing:

    1. Check that the numerator of long division is repeating and halt the loop. When numerator repeats, it means that we have found the repeating decimal.

    2. Convert array to string without commas, which is easily achievable by using join('').

    Here’s the relevant part of your code with the above 2 points implemented:

    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 with the complete code: http://jsfiddle.net/49Xks/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
this is what i have right now Drawing an RSS feed into the php,
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.