JS Basic
JS Interview Programs
- JS Interview Programs
- JS Abundant Number
- JS Amicable Number
- JS Armstrong Number
- JS Average of N Numbers
- JS Automorphic Number
- JS Biggest of three numbers
- JS Binary to Decimal
- JS Common Divisors
- JS Composite Number
- JS Condense a Number
- JS Cube Number
- JS Decimal to Binary
- JS Decimal to Octal
- JS Disarium Number
- JS Even Number
- JS Evil Number
- JS Factorial of a Number
- JS Fibonacci Series
- JS GCD
- JS Happy Number
- JS Harshad Number
- JS LCM
- JS Leap Year
- JS Magic Number
- JS Matrix Addition
- JS Matrix Division
- JS Matrix Multiplication
- JS Matrix Subtraction
- JS Matrix Transpose
- JS Maximum Value of an Array
- JS Minimum Value of an Array
- JS Multiplication Table
- JS Natural Number
- JS Number Combination
- JS Odd Number
- JS Palindrome Number
- JS Pascalβs Triangle
- JS Perfect Number
- JS Perfect Square
- JS Power of 2
- JS Power of 3
- JS Pronic Number
- JS Prime Factor
- JS Prime Number
- JS Smith Number
- JS Strong Number
- JS Sum of Array
- JS Sum of Digits
- JS Swap Two Numbers
- JS Triangular Number
JavaScript Program to Check Smith Number
Photo Credit to CodeToFun
π Introduction
In the realm of programming, exploring number properties is a common and fascinating endeavor. One interesting type of number is the Smith Number.
A Smith Number is a composite number for which the sum of its digits is equal to the sum of the digits in its prime factorization.
In this tutorial, we'll delve into a JavaScript program that checks whether a given number is a Smith Number.
π Example
Let's take a look at the JavaScript code that checks whether a given number is a Smith Number.
// Function to calculate the sum of digits in a number
function sumOfDigits(num) {
return num.toString().split('').reduce((sum, digit) => sum + parseInt(digit), 0);
}
// Function to calculate the sum of digits in the prime factorization of a number
function sumOfPrimeFactors(num) {
let i = 2,
sum = 0;
while (num > 1) {
while (num % i === 0) {
sum += sumOfDigits(i);
num /= i;
}
i++;
}
return sum;
}
// Function to check if a number is a Smith Number
function isSmithNumber(num) {
return sumOfDigits(num) === sumOfPrimeFactors(num);
}
// Driver program
// Replace this value with your desired number
const number = 85;
// Check if the number is a Smith Number
if (isSmithNumber(number)) {
console.log(`${number} is a Smith Number.`);
} else {
console.log(`${number} is not a Smith Number.`);
}
π» Testing the Program
To test the program with different numbers, replace the value of number in the code.
85 is a Smith Number.
Run the script to check whether the given number is a Smith Number.
π§ How the Program Works
- The program defines three functions: sumOfDigits to calculate the sum of digits in a number, sumOfPrimeFactors to calculate the sum of digits in the prime factorization, and isSmithNumber to check if a number is a Smith Number.
- The number variable is replaced with the desired number, and the program checks if it is a Smith Number.
- The program utilizes array manipulation and arithmetic operations to calculate the sum of digits and the sum of digits in the prime factorization.
π Between the Given Range
Let's take a look at the javascript code that checks for Smith Numbers in the specified range.
// Function to calculate the sum of digits
function sumOfDigits(n) {
let sum = 0;
while (n > 0) {
sum += n % 10;
n = Math.floor(n / 10);
}
return sum;
}
// Function to calculate the sum of prime factors
function sumOfPrimeFactors(n) {
let sum = 0;
for (let i = 2; i <= n; ++i) {
while (n % i === 0) {
sum += sumOfDigits(i);
n /= i;
}
}
return sum;
}
// Function to check if a number is prime
function isPrime(n) {
if (n <= 1) {
return false;
}
for (let i = 2; i * i <= n; ++i) {
if (n % i === 0) {
return false;
}
}
return true;
}
// Function to check if a number is a Smith Number
function isSmithNumber(n) {
return isPrime(n) ? false : sumOfDigits(n) === sumOfPrimeFactors(n);
}
// Main program
console.log("Smith Numbers in the Range 1 to 100:");
// Check numbers from 1 to 100
for (let i = 1; i <= 100; ++i) {
if (isSmithNumber(i)) {
process.stdout.write(i + " ");
}
}
π» Testing the Program
Smith Numbers in the Range 1 to 100: 4 22 27 58 85 94
Run the program to see the Smith Numbers in the specified range.
π§ How the Program Works
- The program defines a function isPrime to check if a number is prime.
- The isSmithNumber function checks if a number is a Smith Number by comparing the sum of its digits with the sum of the digits in its prime factorization.
- The main function checks and prints Smith Numbers in the range from 1 to 100.
π§ Understanding the Concept of Smith Numbers
Before delving into the code, it's crucial to understand the concept of Smith Numbers. These numbers exhibit a unique property where the sum of their digits is equal to the sum of the digits in their prime factorization.
For example, 4, 22, 27, and 85 are Smith Numbers.
π’ Optimizing the Program
While the provided program is effective, consider exploring optimizations for larger numbers. You can further refine the code for improved efficiency or adapt it to your specific requirements.
Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (JavaScript Program to Check Smith Number), please comment here. I will help you immediately.