Task description
A prime is a positive integer X that has exactly two distinct divisors: 1 and X. The first few prime integers are 2, 3, 5, 7, 11 and 13.
A prime D is called a prime divisor of a positive integer P if there exists a positive integer K such that D * K = P. For example, 2 and 5 are prime divisors of 20.
You are given two positive integers N and M. The goal is to check whether the sets of prime divisors of integers N and M are exactly the same.
For example, given:
- N = 15 and M = 75, the prime divisors are the same: {3, 5};
- N = 10 and M = 30, the prime divisors aren't the same: {2, 5} is not equal to {2, 3, 5};
- N = 9 and M = 5, the prime divisors aren't the same: {3} is not equal to {5}.
Write a function:
function solution(A, B);
that, given two non-empty arrays A and B of Z integers, returns the number of positions K for which the prime divisors of A[K] and B[K] are exactly the same.
For example, given:
A[0] = 15 B[0] = 75 A[1] = 10 B[1] = 30 A[2] = 3 B[2] = 5the function should return 1, because only one pair (15, 75) has the same set of prime divisors.
Write an efficient algorithm for the following assumptions:
- Z is an integer within the range [1..6,000];
- each element of arrays A and B is an integer within the range [1..2,147,483,647].
Task timeline
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function gcd(a, b) {
if (a % b === 0) {
return b;
} else {
return gcd(b, a % b);
}
}
function solution(A, B) {
const Z = A.length;
let count = 0;
for (let i = 0; i < Z; i++) {
const a = A[i];
const b
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function gcd(a, b) {
if (a % b === 0) {
return b;
} else {
return gcd(b, a % b);
}
}
function solution(A, B) {
const Z = A.length;
let count = 0;
for (let i = 0; i < Z; i++) {
const a = A[i];
const b = B[i];
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function gcd(a, b) {
if (a % b === 0) {
return b;
} else {
return gcd(b, a % b);
}
}
function solution(A, B) {
const Z = A.length;
let count = 0;
for (let i = 0; i < Z; i++) {
const a = A[i];
const b = B[i];
conso
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function gcd(a, b) {
if (a % b === 0) {
return b;
} else {
return gcd(b, a % b);
}
}
function solution(A, B) {
const Z = A.length;
let count = 0;
for (let i = 0; i < Z; i++) {
const a = A[i];
const b = B[i];
console.log(gcd(a, b));
}
}
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
15 10 1
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function gcd(a, b) {
if (a % b === 0) {
return b;
} else {
return gcd(b, a % b);
}
}
function solution(A, B) {
const Z = A.length;
let count = 0;
for (let i = 0; i < Z; i++) {
const a = A[i];
const b = B[i];
cons
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function gcd(a, b) {
if (a % b === 0) {
return b;
} else {
return gcd(b, a % b);
}
}
function solution(A, B) {
const Z = A.length;
let count = 0;
for (let i = 0; i < Z; i++) {
const a = A[i];
const b = B[i];
const g = gcd(a, b);
whl
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function gcd(a, b) {
if (a % b === 0) {
return b;
} else {
return gcd(b, a % b);
}
}
function solution(A, B) {
const Z = A.length;
let count = 0;
for (let i = 0; i < Z; i++) {
const a = A[i];
const b = B[i];
const g = gcd(a, b);
wh
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function gcd(a, b) {
if (a % b === 0) {
return b;
} else {
return gcd(b, a % b);
}
}
function solution(A, B) {
const Z = A.length;
let count = 0;
for (let i = 0; i < Z; i++) {
const a = A[i];
const b = B[i];
const g = gcd(a, b);
while (true) {
}
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function gcd(a, b) {
if (a % b === 0) {
return b;
} else {
return gcd(b, a % b);
}
}
function solution(A, B) {
const Z = A.length;
let count = 0;
for (let i = 0; i < Z; i++) {
const a = A[i];
const b = B[i];
const g = gcd(a, b);
while (true) {
const d = gcd(a, d);
if (d === 1) break
a
}
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function gcd(a, b) {
if (a % b === 0) {
return b;
} else {
return gcd(b, a % b);
}
}
function solution(A, B) {
const Z = A.length;
let count = 0;
for (let i = 0; i < Z; i++) {
let a = A[i];
let b = B[i];
const g = gcd(a, b);
while (true) {
const d = gcd(a, d);
if (d === 1) break
a = a / d;
}
while (true) {
const d = gcd(b, d);
if (d === 1) break
a = a / d;
}
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function gcd(a, b) {
if (a % b === 0) {
return b;
} else {
return gcd(b, a % b);
}
}
function solution(A, B) {
const Z = A.length;
let count = 0;
for (let i = 0; i < Z; i++) {
let a = A[i];
let b = B[i];
const g = gcd(a, b);
while (true) {
const d = gcd(a, d);
if (d === 1) break
a = a / d;
}
while (true) {
const d = gcd(b, d);
if (d === 1) break
b = b / d;
}
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function gcd(a, b) {
if (a % b === 0) {
return b;
} else {
return gcd(b, a % b);
}
}
function solution(A, B) {
const Z = A.length;
let count = 0;
for (let i = 0; i < Z; i++) {
let a = A[i];
let b = B[i];
const g = gcd(a, b);
while (true) {
const d = gcd(a, d);
if (d === 1) break
a = a / d;
}
while (true) {
const d = gcd(b, d);
if (d === 1) break
b = b / d;
}
if (a === 1 && b === 1) {
count++;
}
}
}
ReferenceError: d is not defined at solution (solution.js:23:30) at solutionWrapper (/tmp/exec.js:412:28) at Promise.resolve.then (/tmp/exec.js:438:24) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function gcd(a, b) {
if (a % b === 0) {
return b;
} else {
return gcd(b, a % b);
}
}
function solution(A, B) {
const Z = A.length;
let count = 0;
for (let i = 0; i < Z; i++) {
let a = A[i];
let b = B[i];
const g = gcd(a, b);
while (true) {
const d = gcd(a, g);
if (d === 1) break
a = a / d;
}
while (true) {
const d = gcd(b, d);
if (d === 1) break
b = b / d;
}
if (a === 1 && b === 1) {
count++;
}
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function gcd(a, b) {
if (a % b === 0) {
return b;
} else {
return gcd(b, a % b);
}
}
function solution(A, B) {
const Z = A.length;
let count = 0;
for (let i = 0; i < Z; i++) {
let a = A[i];
let b = B[i];
const g = gcd(a, b);
while (true) {
const d = gcd(a, g);
if (d === 1) break
a = a / d;
}
while (true) {
const d = gcd(b, g);
if (d === 1) break
b = b / d;
}
if (a === 1 && b === 1) {
count++;
}
}
}
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function gcd(a, b) {
if (a % b === 0) {
return b;
} else {
return gcd(b, a % b);
}
}
function solution(A, B) {
const Z = A.length;
let count = 0;
for (let i = 0; i < Z; i++) {
let a = A[i];
let b = B[i];
const g = gcd(a, b);
while (true) {
const d = gcd(a, g);
if (d === 1) break
a = a / d;
}
while (true) {
const d = gcd(b, g);
if (d === 1) break
b = b / d;
}
if (a === 1 && b === 1) {
count++;
}
}
return count;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function gcd(a, b) {
if (a % b === 0) {
return b;
} else {
return gcd(b, a % b);
}
}
function solution(A, B) {
const Z = A.length;
let count = 0;
for (let i = 0; i < Z; i++) {
let a = A[i];
let b = B[i];
const g = gcd(a, b);
while (true) {
const d = gcd(a, g);
if (d === 1) break
a = a / d;
}
while (true) {
const d = gcd(b, g);
if (d === 1) break
b = b / d;
}
if (a === 1 && b === 1) {
count++;
}
}
return count;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function gcd(a, b) {
if (a % b === 0) {
return b;
} else {
return gcd(b, a % b);
}
}
function solution(A, B) {
const Z = A.length;
let count = 0;
for (let i = 0; i < Z; i++) {
let a = A[i];
let b = B[i];
const g = gcd(a, b);
while (true) {
const d = gcd(a, g);
if (d === 1) break
a = a / d;
}
while (true) {
const d = gcd(b, g);
if (d === 1) break
b = b / d;
}
if (a === 1 && b === 1) {
count++;
}
}
return count;
}
The solution obtained perfect score.