Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.
Given a very large number N, the task is to find the Digital Root of large Integers using Recursion in Python.
Examples:
Example1:
Input:
Given Large Number =64829048194591378492648546725
Output:
The Digital root of the given number 64829048194591378492648546725 is [ 6 ]
Example2:
Input:
Given Large Number =587199248368256143942
Output:
The Digital root of the given number 587199248368256143942 is [ 7 ]
Program to Find Digital Root of Large Integers using Recursion in Python
Below are the ways to find the Digital root of large Integers using Recursion in Python.
The digital root of an integer can be determined by adding all of the digits of a given integer until only one digit remains. The digital root of the provided number is this single-digit integer. If the sum of all the digits of a given integer yields a two or three-digit integer, repeat the sum until only a single-digit integer remains.
Method #1: Using Recursion (Static Input)
Approach:
Give the number(Very Large) as static input and store it in a variable.
Create a function digitalRootRecursion which accepts the given number as an argument and returns the digital root of the given number.
Pass the given number as an argument to digitalRootRecursion() function.
Then determine whether the integer is greater than or less than 10. If the integer is less than 10, return the number immediately and terminate the function. If the number is higher than 10, use the above function to recursively compute the sum of the digits of the given integer.
The method will be repeated until we obtain the sum of all the digits of the specified integer. Then, by comparing our final number to 10, we may determine if it is a single digit or not. If the number is a single digit, it should be returned. The number returned is our digital root. If the number is not a single digit, repeat the operation until only a single digit remains.
So, using Recursion in Python, we can get the digital root of huge integers using this approach.
Print the Digital Root.
The Exit of the Program.
Below is the implementation:
# Create a function digitalRootRecursion which accepts the given number as an argument # and returns the digital root of the given number. def digitalRootRecursion(largenumbr): # Then determine whether the integer is greater than or less than 10. # If the integer is less than 10, return the number immediately # and terminate the function if(largenumbr < 10): return largenumbr # If the number is higher than 10, use the above function to recursively # compute the sum of the digits of the given integer. largenumbr = largenumbr % 10+digitalRootRecursion(largenumbr//10) return digitalRootRecursion(largenumbr) # Give the number(Very Large) as static input and store it in a variable. lrgenumbr = 64829048194591378492648546725 # Pass the given number as an argument to digitalRootRecursion() function. rsdigitalroot = digitalRootRecursion(lrgenumbr) # print the result digital root print('The Digital root of the given number', lrgenumbr, 'is [', rsdigitalroot, ']')
Output:
The Digital root of the given number 64829048194591378492648546725 is [ 6 ]
Method #2: Using Recursion (User Input)
Approach:
Give the number(Very Large) as user input using int(input()) function and store it in a variable.
Create a function digitalRootRecursion which accepts the given number as an argument and returns the digital root of the given number.
Pass the given number as an argument to digitalRootRecursion() function.
Then determine whether the integer is greater than or less than 10. If the integer is less than 10, return the number immediately and terminate the function. If the number is higher than 10, use the above function to recursively compute the sum of the digits of the given integer.
The method will be repeated until we obtain the sum of all the digits of the specified integer. Then, by comparing our final number to 10, we may determine if it is a single digit or not. If the number is a single digit, it should be returned. The number returned is our digital root. If the number is not a single digit, repeat the operation until only a single digit remains.
So, using Recursion in Python, we can get the digital root of huge integers using this approach.
Print the Digital Root.
The Exit of the Program.
Below is the implementation:
# Create a function digitalRootRecursion which accepts the given number as an argument # and returns the digital root of the given number. def digitalRootRecursion(largenumbr): # Then determine whether the integer is greater than or less than 10. # If the integer is less than 10, return the number immediately # and terminate the function if(largenumbr < 10): return largenumbr # If the number is higher than 10, use the above function to recursively # compute the sum of the digits of the given integer. largenumbr = largenumbr % 10+digitalRootRecursion(largenumbr//10) return digitalRootRecursion(largenumbr) # Give the number(Very Large) as user input using int(input()) # function and store it in a variable. lrgenumbr = int(input('Enter some random large number = ')) # Pass the given number as an argument to digitalRootRecursion() function. rsdigitalroot = digitalRootRecursion(lrgenumbr) # print the result digital root print('The Digital root of the given number', lrgenumbr, 'is [', rsdigitalroot, ']')
Output:
The Digital root of the given number 9956782345098712347865490832469987 is [ 7 ]
Related Programs:
- python program to find the power of a number using recursion
- python program to find the product of two numbers using recursion
- python program to find the total sum of a nested list using recursion
- python program to find the lcm of two numbers using recursion
- python program to find the length of a list using recursion
- python program to find the fibonacci series using recursion
- python program to find if a number is prime or not prime using recursion