0% found this document useful (0 votes)
16 views6 pages

Integer question

The document contains a collection of JavaScript functions that perform various integer-related operations, including counting digits, reversing integers, checking for palindromes, and finding missing numbers in an array. It also includes functions for calculating the greatest common divisor, checking for prime numbers, generating prime numbers, and converting between decimal and binary. Additional functionalities include calculating factorials, counting trailing zeros, and identifying non-repeating digits.

Uploaded by

realvikram7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views6 pages

Integer question

The document contains a collection of JavaScript functions that perform various integer-related operations, including counting digits, reversing integers, checking for palindromes, and finding missing numbers in an array. It also includes functions for calculating the greatest common divisor, checking for prime numbers, generating prime numbers, and converting between decimal and binary. Additional functionalities include calculating factorials, counting trailing zeros, and identifying non-repeating digits.

Uploaded by

realvikram7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Integer question

// 1. Count Digits in a Number


function countDigits(n) {
return n === 0 ? 1 : Math.floor(Math.log10(Math.abs(n))) + 1;
}

// 2. Reverse an Integer
function reverseInteger(n) {
let reversed = 0;
let num = Math.abs(n);
while (num > 0) {
reversed = reversed * 10 + (num % 10);
num = Math.floor(num / 10);
}
return n < 0 ? -reversed : reversed;
}

// 3. Check if a Number is a Palindrome


function isPalindrome(n) {
return n === reverseInteger(n);
}

// 4. Sum of Digits till one digit (Digital Root)


function digitalRoot(n) {
return 1 + (n - 1) % 9;
}

// 5. Find the Missing Number in an Array


function findMissingNumber(arr) {
let n = arr.length;
let total = (n * (n + 1)) / 2;
return total - arr.reduce((sum, num) => sum + num, 0);
}
// 6. Check for Armstrong Number
function isArmstrong(n) {
let numStr = n.toString();
let power = numStr.length;
let sum = numStr.split('').reduce((acc, digit) => acc +
Math.pow(parseInt(digit), power), 0);
return sum === n;
}

// 7. Count the Number of Set Bits (Hamming Weight)


function countSetBits(n) {
let count = 0;
while (n) {
n &= (n - 1); // Clear the least significant bit set
count++;
}
return count;
}

// 8. Greatest Common Divisor (GCD) and Least Common Multiple


(LCM)
function gcd(a, b) {
while (b) {
[a, b] = [b, a % b];
}
return a;
}

function lcm(a, b) {
return (a * b) / gcd(a, b);
}
// 9. Check if a Number is Prime
function isPrime(n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 === 0 || n % 3 === 0) return false;
for (let i = 5; i * i <= n; i += 6) {
if (n % i === 0 || n % (i + 2) === 0) return false;
}
return true;
}

// 10. Generate All Prime Numbers up to N (Sieve of Eratosthenes)


function sieveOfEratosthenes(n) {
let primes = Array(n + 1).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i * i <= n; i++) {
if (primes[i]) {
for (let j = i * i; j <= n; j += i) {
primes[j] = false;
}
}
}
return primes.map((isPrime, num) => isPrime ? num : null).filter(num
=> num !== null);
}

// 11. Factorial of a Number


function factorial(n) {
if(n<=1 ) return 1;
return n*factorial(n-1);
}
// 12. Count Trailing Zeros in Factorial
function countTrailingZeros(n) {
let count = 0;
for (let i = 5; n / i >= 1; i *= 5) {
count += Math.floor(n / i);
}
return count;
}

// 13. Check if Two Numbers are Co-prime


function areCoprime(a, b) {
return gcd(a, b) === 1;
}

// 14. Convert Decimal to Binary (and Vice Versa)


function decimalToBinary(n) {
return n.toString(2);
}

function binaryToDecimal(b) {
return parseInt(b, 2);
}
// 15. Find the First Non-Repeating Digit
function firstNonRepeatingDigit(n) {
let numStr = n.toString();
let countMap = {};

for (let digit of numStr) {


countMap[digit] = (countMap[digit] || 0) + 1;
}

for (let digit of numStr) {


if (countMap[digit] === 1) {
return parseInt(digit);
}
}

return -1; // If all digits repeat


}
// Example Usage
console.log(countDigits(12345)); // 5
console.log(reverseInteger(-123)); // -321
console.log(isPalindrome(121)); // true
console.log(digitalRoot(9875)); // 2
console.log(findMissingNumber([0, 1, 3])); // 2
console.log(isArmstrong(153)); // true
console.log(countSetBits(9)); // 2
console.log(gcd(54, 24)); // 6
console.log(lcm(4, 5)); // 20
console.log(isPrime(29)); // true
console.log(sieveOfEratosthenes(30)); // [2, 3, 5, 7, 11, 13, 17, 19,
23, 29]
console.log(factorial(5)); // 120
console.log(countTrailingZeros(100)); // 24
console.log(areCoprime(14, 15)); // true
console.log(decimalToBinary(10)); // "1010"
console.log(binaryToDecimal("1010")); // 10
console.log(firstNonRepeatingDigit(123231)); // 1

You might also like