Lucky Pairs
Write Java script program to count the number of lucky pairs.
Two numbers are said to be lucky pairs . if first number is divided by second number and second number is divided by first number. given an integer N . find all possible combinations of lucky pairs.
input:
2
output:
2
Explanation:
input is 2
possible combinations are [1,2],[2,1].here in [1,2]1 is divisible by 2, and in [2,1] 2 is divisible by 1 .so the number of lucky pairs are 2.
<spript>
const n; // length of the arr below
let arr;
let count = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i != j and i % j == 0) {
count += 1;
}
}
}
console.log(2*count);
</script>
Comments
Leave a comment