• Resolved mevabien22

    (@mevabien22)


    Hello,

    Just as there is the gcd (x, y) function to calculate the greatest common factor between two numbers …

    Is there a function to calculate the least common multiple between two or more numbers?

    For example, the least common multiple between 5 and 6 is 30.

    Thank you

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author codepeople

    (@codepeople)

    Hello @mevabien22

    The process is very simple, you can find the solution in the following link: Least common multiple (LCM) of two numbers

    * Insert a “HTML Content” field in the form with the following piece of code as its content:

    
    <script>
    function lcm_two_numbers(x, y) {
       if ((typeof x !== 'number') || (typeof y !== 'number')) 
        return false;
      return (!x || !y) ? 0 : Math.abs((x * y) / gcd_two_numbers(x, y));
    }
    
    function gcd_two_numbers(x, y) {
      x = Math.abs(x);
      y = Math.abs(y);
      while(y) {
        var t = y;
        y = x % y;
        x = t;
      }
      return x;
    }
    </script>
    

    And then, you simply should to call the operation lcm_two_numbers as part of the equation, for example:

    lcm_two_numbers(6,5)

    or if there are two number fields in the form (fieldname1 and fieldname2), and you want get the least common multiple between them, the equation associated to the calculated field could be:

    
    lcm_two_numbers(fieldname1, fieldname2)
    

    Best regards.

    Thread Starter mevabien22

    (@mevabien22)

    Thank you very much for the quick response, it works without problems!

    Hi @codepeople – I am trying to do something very similar to what you described here but can’t quite figure it out. I’m trying to convert a decimal to a fraction.

    I see that the Javascript code can be found here: https://jsfiddle.net/5QrhQ/5/
    Stackoverflow discussion here: https://stackoverflow.com/questions/23575218/convert-decimal-number-to-fraction-in-javascript-or-closest-fraction

    Could you help? I’m so close and would love to figure it out! Thanks

    Plugin Author codepeople

    (@codepeople)

    Hello @dirk93

    The process is very simple:

    First, insert a “HTML Content” field in the form, and enter as its content the following piece of code:

    
    <script>
    function getFraction(fraction)
    {
    	var gcd = function (a, b) {
    		if (b < 0.0000001) return a;
    		return gcd(b, Math.floor(a % b));
    	};
    	
    	var len = fraction.toString().length - 2;
    	var denominator = Math.pow(10, len);
    	var numerator = fraction * denominator;
    	var divisor = gcd(numerator, denominator);
    	numerator /= divisor;
    	denominator /= divisor;
    	return Math.floor(numerator) + '/' + Math.floor(denominator);
    }
    </script>
    

    Now you can call the new operation: getFraction passing as parameter the decimal number, for example: getFraction(0.3435)

    I’m sorry, but the support service does not cover the implementation of the users’ projects (form and formulas), and your question is not exactly about our plugin, you are requesting the code of your project. If you need additional help implementing your project, please, contact me through my private website: Customization

    Best regards.

    @codepeople this worked great!! Thank you so much.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Is there a function to calculate the least common multiple between two or more n’ is closed to new replies.