Uncovering Divisors in Numeric Digits

Uncovering Divisors in Numeric Digits

Introduction:

In this blog post let us explore the task of finding and counting the divisors within an integer. So, let us delve into a coding problem in HackerRank website problem solving section and understand the problem statement, explore its constraints, and provide a code solution to address it.

Understanding the Problem:

The problem statement is that we are given an integer, and our objective is to determine whether each digit that makes up the integer is a divisor of that integer. In mathematical terms, an integer 'divv' is considered a divisor of another integer 'n' if the remainder of the division of 'n' by 'divv' is equal to zero. Our goal is to count the number of divisors of the integer occurring within the integer.

Link to the problem: click here

Problem Constraints:

Before we dive into the solution, let us understand the constraints of this problem:

  • The integer 'n' can be any positive integer.

  • The integer 'n' may contain multiple digits.

  • The integer 'n' can have leading zeros, but they should be ignored as they do not affect whether a digit is a divisor.

  • Our task is to count the number of divisors and return their count.

Code Solution in C language:

Code Explanation:

. int findDigits(int n) is the signature of the function which takes an integer n as input.

. We define three integer variables: "o" which holds the initial value of the input integer "n", "divv" representing each digit of the input "n" and updated in every iteration of the while loop, and "count", which keeps track of the number of divisors found for the input and may increase with each iteration.

. Iteration in while loop:

  1. while(n != 0): This line initiates a while loop that continues to execute as long as the variable n is not equal to zero. In each iteration of the loop, the code inside the loop will execute.

  2. divv = n % 10: This line calculates the last digit of n by taking the remainder when dividing n by 10. This gives you the rightmost digit in the number. The result is stored in the variable divv.

  3. n = n - divv: Here, the last digit (divv) is subtracted from the input number n. This is done to remove the rightmost digit and move to the next digit in the next iteration.

  4. n = n / 10: After removing the last digit, the code divides n by 10 to shift to the next digit to the left. This prepares n for the next iteration of the loop.

  5. if (divv != 0): This if statement checks whether the last digit (divv) is not equal to zero. It's necessary to ensure we don't divide by zero in the next conditional statement.

  6. if (o % divv == 0): Inside this nested if statement, the code checks if the last digit (divv) is a divisor of the original number o. It does this by calculating the remainder of dividing o by divv. If the remainder is zero, it means that divv is a divisor of o, and the code inside this if statement is executed.

  7. count = count + 1: If the last digit is indeed a divisor of the original number o, the count variable is incremented by 1. This variable keeps track of how many divisors have been found in the original number.

The loop continues to iterate through the digits of the original number until there are no more digits left (when n becomes zero). The count variable keeps track of how many divisors were found during this process.

. return count: indicates the total count of divisors found within the input integer.

Sample Input:

1012

Sample Output:

3

Execution steps:

Conclusion:

In this blog post, Our primary goal was to count the number of divisors present within the integer, all while navigating through the constraints set by the problem.