I was trying to figure out how to calculate the lcm and gcd (mainly the greatest common factor) in javascript, or, if it's easier, jquery. I've searched the internet, but the best I've found is this:
function MCD(){
if (arguments.length<2) return false;
if (arguments.length==2)return (arguments[1]==0?arguments[0]:MCD(arguments[1],arguments[0]%arguments[1]));
var arr=[].splice.call(arguments,0);
arr.splice(0,2,MCD(arr[0],arr[1]));
return MCD.apply(window,arr);
}
function mcm(a,b){
if (arguments.length<2) return false;
if (arguments.length==2)return arguments[0]*arguments[1]/MCD(arguments[0],arguments[1]);
var arr=[].splice.call(arguments,0);
arr.splice(0,2,mcm(arr[0],arr[1]));
return mcm.apply(window,arr);
}
console.log(MCD(36, 12, 96));//Esto es lo que quiero cambiar
console.log(mcm.apply(window,[36, 12, 96]));//Esto es lo que quiero cambiar
console.log(MCD(
36, 12, 96));
Edit: "Basically, I want the numbers 36, 12, 96 to be taken from an input, but I have tried with document.getElementById().value and it doesn't work" Thanks in advance. ^-^
As I told you in my comment,
input
HTML elements store a type valuestring
in their propertyvalue
. Therefore you must do a series of operations to convert the data you get from theinput
.ISSUE
A single type element is available
input
to enter 1, 2, 3 or more numeric values separated by commas. These values will be sent as an argument to the Least Common Multiple and the Least Common Divisor functions.SOLUTION
We must create a function that allows us to extract the numbers entered in the element
input
. We will use a loopfor
and the Javascript methodsubstring()
, as well as theparseInt()
. We will also use the methodapply
to pass the arguments to the functionsMCD
andmcm
.In the HTML code I have added a tag
required
to the input, as well as a tagpattern
with a regular expression that verifies that the input only contains digits from 0 to 9 and commas, also it only allows numbers at the beginning and at the end of the string, this is for don't break the logic of the function that extracts the numbers.CONCLUSION
Using the Javascript
apply
object methodFunction
, we can use an array to pass arguments to our function.This is a very handy way of using arguments dynamically. The example presented here demonstrates the versatility of the method, since we can pass a large list of numbers separated by commas without having to write a call for each change in the number of elements.
References:
Function.prototype.apply()
HTML pattern Attribute
THEORY: With the help of the GCD (greatest common divider), we can obtain the LCM (least common multiple) through the Euclidean Formula: (a * b) / gcd(a,b) = lcm
It should be remembered that to find the GCF, you have to search for the largest of the two numbers, in which case both modules are Zero. (num1%i==0 && num2%i==0), Looping only to the lesser of both numbers (6, 10) i=1, i<6.
Next:
If you have to find the LCM of more than two numbers, the above methods can be adjusted.
For example, to find the LCM of 16, 20, and 32 you might start by finding the LCM of 16 and 20 (which is 80) and then find the LCM of 80 and 32, which turns out to be 160.
This code works both individually and by entering an Array with Numbers.
Basically, it asks the User how many numbers he wants to search for and then asks for them by keyboard in the form of an Array.
User Input: 8 Top Numbers, Fill in 8 Numbers. (User Choice)
Output:
The Least Common Multiple (LCM) of:
Array(8) [ 8, 16, 36, 56, 76, 80, 92, 124 ] is: 68276880
Check: https://es.symbolab.com/solver/lcm-calculator/mcm%208%2C%2016%2C%2036%2C%2056%2C%2076%2C%2080%2C%2092%2C%20124? or=input
I think you should take the 2 max smallest values of the set and find the gcd, making use of the recursive function:
function gcd(a,b){
}