Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

PHP Program to Check Smith Number

Posted in PHP Tutorial
Updated on Oct 31, 2024
By Mari Selvan
πŸ‘οΈ 39 - Views
⏳ 4 mins
πŸ’¬ 1 Comment
PHP 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 PHP program that checks whether a given number is a Smith Number.

πŸ“„ Example

Let's take a look at the PHP code that checks whether a given number is a Smith Number.

isSmithNumber.php
Copied
Copy To Clipboard
<?php
// Function to calculate the sum of digits in a number
function sumOfDigits($num)
{
  $sum = 0;
  while ($num > 0)
  {
    $sum += $num % 10;
    $num = (int)($num / 10);
  }
  return $sum;
}

// Function to calculate the sum of digits in the prime factorization of a number
function sumOfPrimeFactors($num)
{
  $i = 2;
  $sum = 0;

  while ($num > 1)
  {
    while ($num % $i == 0)
    {
      $sum += sumOfDigits($i);
      $num = (int)($num / $i);
    }
    $i++;
  }

  return $sum;
}

// Function to check if a number is a Smith Number
function isSmithNumber($num)
{
  return sumOfDigits($num) == sumOfPrimeFactors($num);
}

// Replace this value with your desired number
$number = 85;

// Check if the number is a Smith Number
if (isSmithNumber($number))
{
  echo "$number is a Smith Number.\n";
}
else
{
  echo "$number is not a Smith Number.\n";
}

?>

πŸ’» Testing the Program

To test the program with different numbers, replace the value of $number.

Output
85 is a Smith Number.

Run the script to check whether the given number is a Smith Number.

🧠 How the Program Works

  1. 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.
  2. The value of $number is replaced with the desired number, and the program checks if it is a Smith Number.
  3. The program utilizes loops 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 php code that checks for Smith Numbers in the specified range.

isSmithNumber.php
Copied
Copy To Clipboard
<?php
// Function to calculate the sum of digits
function sumOfDigits($n)
{
  $sum = 0;
  while ($n > 0)
  {
    $sum += $n % 10;
    $n = (int)($n / 10);
  }
  return $sum;
}

// Function to calculate the sum of prime factors
function sumOfPrimeFactors($n)
{
  $sum = 0;
  for ($i = 2;$i <= $n;++$i)
  {
    while ($n % $i == 0)
    {
      $sum += sumOfDigits($i);
      $n = (int)($n / $i);
    }
  }
  return $sum;
}

// Function to check if a number is prime
function isPrime($n)
{
  if ($n <= 1)
  {
    return false;
  }
  for ($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);
}

// Check numbers from 1 to 100
echo "Smith Numbers in the Range 1 to 100:\n";
for ($i = 1;$i <= 100;++$i)
{
  if (isSmithNumber($i))
  {
    echo "$i ";
  }
}
echo "\n";

?>

πŸ’» Testing the Program

Output
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

  1. The program defines a function isPrime to check if a number is prime.
  2. 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.
  3. 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:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
πŸ‘‹ Hey, I'm Mari Selvan

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

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
10 months ago

If you have any doubts regarding this article (PHP Program to Check Smith Number), please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy