I have the following javascript code to calculate the factors of numbers.
var some = [];
var main = "";
var final = "";
function text[data] {
spliter = data.split[","];
var k = 0;
while [k < spliter.length] {
var meethi = 0;;
main = spliter[k];
var datas = "";
for [var i = 1; i max] {
max = high;
res += max + ':';
}
}
}
document.getElementById['demo'].innerHTML = text['124,20'];
My program gets the factors with two values. How do I identify the common factor of both values,only the highest common value?
example like ['124,20'] output --> 4
I tried the code with my own knowledge. If you have any other suggestion for code please tell me and correct my code with my desired result.
my fiddle
JavaScript Math: Exercise-8 with Solution
Write a JavaScript function to get the greatest common divisor [gcd] of two integers.
Note:
According to Wikipedia - In mathematics, the greatest common divisor [gcd] of two or more integers, when at least one of them is not
zero, is the largest positive integer that divides the numbers without a remainder. For example, the GCD of 8 and 12 is 4.
Test Data :
console.log[gcd_two_numbers[12, 13]];
console.log[gcd_two_numbers[9, 3]];
Output :
1
3
Pictorial Presentation:
Sample Solution-1:
JavaScript Code:
function gcd_two_numbers[x, y] {
if [[typeof x !== 'number'] || [typeof y !== 'number']]
return false;
x = Math.abs[x];
y = Math.abs[y];
while[y] {
var t = y;
y = x % y;
x = t;
}
return x;
}
console.log[gcd_two_numbers[12, 13]];
console.log[gcd_two_numbers[9, 3]];
Sample Output:
1 3
Flowchart:
Sample Solution-2:
Calculates the greatest common divisor between two or more numbers/arrays.
- The inner _gcd function uses recursion.
- Base case is when y equals 0. In this case, return x.
- Otherwise, return the GCD of y and the remainder of the division x/y.
JavaScript Code:
//Source://bit.ly/3hEZdCl
//gcd
const gcd = [...arr] => {
const _gcd = [x, y] => [!y ? x : gcd[y, x % y]];
return [...arr].reduce[[a, b] => _gcd[a, b]];
};
console.log[gcd[12, 13]];
console.log[gcd[9, 3]];
console.log[gcd[8, 36]];
console.log[gcd[...[12, 8, 32]]];
Sample Output:
1 3 4 4
Flowchart:
Live Demo:
See the Pen javascript-math-exercise-8 by w3resource [@w3resource] on CodePen.
Contribute your code and comments through Disqus.
Previous: Write a JavaScript function to find the lowest value in an array.
Next: Write a JavaScript function to find the GCD [greatest common divisor] of more than 2 integers.
JavaScript: Tips of the Day
Property name
const { name: myName } = { name: 'Owen' }; console.log[name];
When we unpack the property name from the object on the right-hand side, we assign its value "Owen" to a variable with the name myName.
With { name: myName }, we tell JavaScript that we want to create a new variable called myName with the value of the name property on the right-hand side.
Since we try to log name, a variable that is not defined, a ReferenceError gets thrown.
Ref: //bit.ly/3jFRBje
Given two integer numbers, the task is to find count of all common divisors of given numbers?
Examples :
Input : a = 12, b = 24 Output: 6 // all common divisors are 1, 2, 3, // 4, 6 and 12 Input : a = 3, b = 17 Output: 1 // all common divisors are 1 Input : a = 20, b = 36 Output: 3 // all common divisors are 1, 2, 4
It is recommended to refer all divisors of a given number as a prerequisite of this article.
Naive Solution
A simple solution is to first find all divisors of first number and store them in an array or
hash. Then find common divisors of second number and store them. Finally print common elements of two stored arrays or hash. The key is that the magnitude of powers of prime factors of a divisor should be equal to the minimum power of two prime factors of a and b.
- Find the prime factors of a using prime factorization.
- Find the count of each prime factor of a and store it in a Hashmap.
- Prime factorize b using distinct prime factors of a.
- Then the total number of divisors would be equal to the product of [count + 1]
of each factor. - count is the minimum of counts of each prime factors of a and b.
- This gives the count of all divisors of a and b.
C++
#include
using
namespace
std;
map ma;
void
primeFactorize[
int
a]
{
for
[
int
i = 2; i * i 1]
{
ma[a] = 1;
}
}
int
commDiv[
int
a,
int
b]
{
primeFactorize[a];
int
res = 1;
for
[
auto
m = ma.begin[];
m != ma.end[]; m++]
{
int
cnt = 0;
int
key = m->first;
int
value = m->second;
while
[b % key == 0]
{
b /= key;
cnt++;
}
res *= [min[cnt, value] + 1];
}
return
res;
}
int
main[]
{
int
a = 12, b = 24;
cout {
let cnt = 0;
let key = keys;
let value = values;
while
[b % key == 0]
{
b = parseInt[b / key, 10];
cnt++;
}
res *= [Math.min[cnt, value] + 1];
}]
return
res;
}
let a = 12, b = 24;
document.write[commDiv[a, b]];
Output:
6
Time Complexity: O[√n log n]
Auxiliary Space: O[n]
Efficient Solution –
A better solution is to calculate the
greatest common divisor [gcd] of given two numbers, and then count divisors of that gcd.
C++
#include
using
namespace
std;
int
gcd[
int
a,
int
b]
{
if
[a == 0]
return
b;
return
gcd[b % a, a];
}
int
commDiv[
int
a,
int
b]
{
int
n = gcd[a, b];
int
result = 0;
for
[
int
i = 1; i
Javascript
function
gcd[a, b]
{
if
[a == 0]
return
b;
return
gcd[b % a, a];
}
function
commDiv[a, b]
{
let n = gcd[a, b];
let result = 0;
for
[let i = 1; i