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

  • SEARCH
  • Home
  • 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 8308987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:58:15+00:00 2026-06-08T18:58:15+00:00

This code is supposed to simplify fractions and convert decimals to fractions but when

  • 0

This code is supposed to simplify fractions and convert decimals to fractions but when I put in fractions with larger dividens (numbers more than 7 or 8 digits) it lags a great amount.

http://jsfiddle.net/SuperBoi45/vQjgx/

var fraction = {};

fraction.simplify = function( frac ) {
    if ( frac.indexOf('/') < 0 ) return frac;
    var numbers = frac.split('/'),
        factor = null,
        parsed = null;

    return (function run( nums ) {
        factor = fraction.factor( nums[0], nums[1] );

        if ( factor === 1 ) {
            parsed = [ Math.abs(nums[0]), Math.abs(nums[1]) ];

            if ( nums[1] === 1 ) return nums[0];
            else if ( nums[1] === -1 ) return -nums[0];
            else if ( nums[0] < 0 && nums[1] < 1 ) return parsed[0] + '/' + parsed[1];
            else if ( nums[0] < 0 || nums[1] < 0 ) return '-' + parsed[0] + '/' + parsed[1];
            else return nums[0] + '/' + nums[1];
        }

        return run( [ nums[0] / factor, nums[1] / factor ] );
    })( numbers );
};
fraction.convert = function( decimal ) {
    var j = decimal.length - 1,
        b = "1";

    if ( decimal.indexOf(".") >= 0 && decimal.length > 1 ) {

        while ( decimal.charAt( j ) != "." ) {
            b += "0";
            j--;
        }

        decimal *= b;
        decimal += "/" + b;

    }

    return decimal;

};
fraction.factor = (function() {

    var greater = function( a, b ) {
        return a > b ? a : b;
    };

    return function( x, y ) {
        x = Math.abs( x );
        y = Math.abs( y );

        var a = greater( x, y ),
            i = a,
            b = ( i === x ) ? y : x;

        for ( ; i >= 1; i-- ) {
            if ( a % i === 0 && b % i === 0 ) return i;
        }

        return 1;
    };

})();​

I’m trying to make it work like Wolfram Alpha because you can put in fractions with large dividens and it doesn’t freeze one bit when showing you its quick-rendered result.

http://wolframalpha.com/

Can anyone fix this code to work with larger numbers. I’d figure you’d have to use a different algorithm than mine. On the other hand, does anyone know WA’s algorithm or can direct me to a site where I can find out?

  • 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-08T18:58:16+00:00Added an answer on June 8, 2026 at 6:58 pm

    Replace fraction.factor() with this:

    function gcd(a, b) {
        if (b > a) return gcd(b, a);
        if (b === 0) return a;
        return gcd(b, a % b);
    };
    

    That’s Euclid’s algorithm, which can serve as a great introduction to Number Theory. It’ll run way faster than your iterative approach.

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

Sidebar

Related Questions

This code was supposed to convert text to image public void onCreate(Bundle savedInstanceState) {
this code is supposed to list recent calls with recent same nos skipped but
This code is supposed to be able to sort the items in self.array based
This code is supposed to read postfix problems from a file and and write
This piece of code is supposed to go through a list and preform some
i have this code in php and it's supposed to print me the image
I've created this code, it is supposed to use wordpress metabox functionality to save
I've had trouble writing this code. I'm supposed to make a function that can
I wrote this piece of code that is supposed to redirect something written on
this is part of my code which reads an http response. It's supposed to

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.