# Unraveling the Mysteries of Harshad Numbers: Algorithm and Python Programming Implementation

Mathematics is a fascinating subject that has captivated humanity since the ancient times. Its interesting to note that this subject has the potential to provide us with exciting puzzles and riddles to solve.

Through the years, mathematicians have developed various concepts and theories to unravel the mysteries of numbers. One such concept that has caught the attention of many math enthusiasts is the Harshad Number.

In this article, well take a closer look at what it is, how it works, and its practical applications.

Harshad Numbers are intriguing numbers that have a unique property. It is a positive integer, which is divisible by the sum of its digits.

In simpler terms, a number is said to be a Harshad Number if its divisible by the sum of its digits without any remainder. For example, take the number 18.

The sum of its digits is 1+8=9. Since 18 is divisible by 9, it is a Harshad Number.

Harshad Numbers were first discussed in the Indian Mathematical treatise, Sutra Sthana, and have since then been the subject of numerous mathematical studies.

Lets explore examples of some Harshad Numbers. Here are the first few:

1,2,3,4,5,6,7,8,9,10,12,18,20,21,24,27,30,36,40,42,45,48,50,54,60,63,70,72,80,81,84,90,100,102,108,110,111,112,114,117,120,126,132,133,135,140

As you can see, not all numbers are Harshad Numbers; however, they do appear frequently enough to be noteworthy.

### Algorithm to check for Harshad Number

To determine if a number is a Harshad Number, we need to follow a set of specific steps. The following algorithm can be used for this purpose.

### Steps to check for Harshad Number

1. Input a positive integer n
2. Copy n to another variable say result
3. Traverse through each digit in the integer n and increment a variable, say sum, by each digits value.
4. Divide the integer n by sum.
5. If the result of the division of n by sum is an integer, then it is a Harshad Number; otherwise, it is not.

Lets put this algorithm to the test with an example, say 1729. 1.

We input number n = 1729

We create a copy of the number, result = 1729

We traverse through each digit in the integer n, and increment the variable sum as shown below:

• sum+=1; //For the digit 1
• sum+=7; //For the digit 7
• sum+=2; //For the digit 2
• sum+=9; //For the digit 9

At the end, the variable sum becomes 19

Calculate n divided by sum.

• result = 1729/19 = 91

Since the result of 91 is an integer, 1729 is a Harshad Number

We can also represent the above algorithm in PseudoCode, as shown below.

``````{
Copy n to a variable named result
Set sum to zero
While n is greater than zero
{
Increment sum by n modulo 10
Divide n by 10 and take the floor value
}
If (result%sum == 0)
{
print result, is a Harshad Number
}
Else
{
print result, is not a Harshad Number
}
}
``````

In conclusion, Harshad Numbers are a fascinating concept in mathematics, and they have numerous practical applications. They have fascinated mathematicians for centuries and continue to do so today.

We hope that this article has given you a better understanding of what Harshad Numbers are, how they work, and their relevance in mathematics.

## Code Implementation for Harshad Number in Python

Now that we know what Harshad Numbers are, let us explore how we can implement a Python program to check if a given number is a Harshad Number or not.

### Initial variable setup

The first step in implementing the Harshad Number program is to take a positive integer as input. We store this input in a variable named n, which we will use to determine if it is a Harshad Number.

Next, we make a copy of this input, which we will use to calculate the sum of its digits. We can do this by using the Python built-in function str() to convert n into a string and then using the built-in function int() to convert it back to an integer.

Lastly, we initialize a variable named result to store the sum of the digits of the input number. Here’s what the initial variable setup code looks like:

``````n = int(input("Enter a positive integer: "))
copy_n = str(n)
result = 0
``````

### Traversing through the number and updating result

Once we have set up the initial variables, the next step is to traverse through each digit in the input number and update the value of the result variable by adding each digit’s value. We can do this by dividing the input number n by 10 using the modulus operator (%), which gives us the remainder of the division.

We then add this remainder to the result variable and update the value of n by using integer division (//) to remove the last digit. The loop continues until n is equal to zero, which indicates that we have processed all digits in the input number.

Here’s what the code to traverse through the input number and updating the result looks like:

``````while n > 0:
remainder = n % 10
result += remainder
n //= 10
``````

### Checking if the number is a Harshad Number or not

The last step in implementing the Harshad Number program is to check if the input number is a Harshad Number or not. We do this by checking if the input number n is divisible by the sum of its digits stored in the variable result.

If the input number is a Harshad Number, we print a message indicating that it is a Harshad Number. If it is not, we print a message stating that it is not a Harshad Number.

Here’s what the code to check if the number is a Harshad Number or not looks like:

``````if copy_n % str(result) == '0':
print(str(n) + " is a Harshad Number")
else:
print(str(n) + " is not a Harshad Number")
``````

### Output sample for Harshad Number:

Let us consider the number 156. Using our Harshad Number program, we can check if it is a Harshad number by running the following code:

``````Enter a positive integer: 156

``````

As we can see from the output, the number 156 is a Harshad Number. Output sample for non-Harshad Number:

Let us consider the number 121.

Using our Harshad Number program, we can check if it is a Harshad number by running the following code:

``````Enter a positive integer: 121

121 is not a Harshad Number
``````

As we can see from the output, the number 121 is not a Harshad Number.

## Conclusion

In conclusion, we have discussed how to implement a Python program to check if a given number is a Harshad Number or not. We set up initial variables, traversed through the input number, and updated the sum of its digits.

Finally, we checked if the number is a Harshad Number or not and printed the output accordingly. Learning about Harshad Numbers and their properties is a fascinating topic for math enthusiasts, and programming such concepts in Python can help to understand them better.

## Conclusion

In this article, we delved into the concept of Harshad Numbers and learned about their unique property of being divisible by the sum of their digits. We explained the algorithm to check for Harshad Numbers and went through the code implementation process in Python.

We started by introducing Harshad Numbers, their definition, and some examples. We then discussed the algorithm and pseudocode to check if a given number is a Harshad Number.

Next, we provided a step-by-step breakdown of the code implementation process in Python, which involved setting up variables, traversing through the input number, and checking if it is a Harshad Number or not. We also provided output examples for both Harshad and non-Harshad Numbers to show the practical applications of our program.

It is, therefore, safe to say that we have thoroughly covered the topic of Harshad Numbers, their algorithm, and programming implementation in Python.

## Recap

Overall, we can conclude that Harshad Numbers are a fascinating concept in mathematics. They not only have interesting properties but possess practical applications that are relevant to numbers and arithmetics.

We started by defining Harshad Numbers, their properties, and discussing some examples. We then moved on to explaining the algorithm and providing pseudocode to check if a given number is a Harshad Number.

Next, we went through the code implementation process and showed how we can program Harshad Numbers in Python. Lastly, we provided output samples for both Harshad and non-Harshad numbers to illustrate the practical applications of our program.

## Encouragement to continue learning

It is essential to continue reading and learning about such mathematical concepts since they have practical applications in our everyday lives. Moreover, a better understanding of such concepts can lead to the discovery of new phenomena, theories, or discoveries that can have a significant impact on society.

Mathematics is a subject that can open doors to various areas of research, innovation, and discovery. It can inspire and engage people of all ages to delve into the fascinating world of numbers, logic, and algorithms.

In conclusion, kudos to you for taking the initiative to explore the world of mathematics and Harshad Numbers. We hope that this article has provided you with valuable insights into numbers’ unique properties and their practical applications.

We would encourage readers to keep learning and exploring the fascinating world of math. In summary, this article has introduced readers to Harshad Numbers, their definition, properties, and practical applications.

We discussed the algorithm and pseudocode to check for Harshad Numbers, implemented the code in Python, and provided output samples. We encourage readers to continue learning and exploring the fascinating world of math since a better understanding of such concepts can lead to new discoveries that can have a significant impact on society.

Overall, understanding Harshad Numbers and their properties can help develop problem-solving skills and mathematical abilities that can benefit individuals in various areas of research, innovation, and discovery.