Posted on by Kalkicode
Code Number

# Generic root of a number

In mathematics, the generic root of a number is a concept that involves repeatedly summing up the digits of the number until a single-digit result is obtained. For example, the generic root of the number 786 would be calculated as follows: 7 + 8 + 6 = 21, and then 2 + 1 = 3. The final result, 3, is a single-digit number, and this is the generic root of 786.

## Problem Statement

The problem is to write a program to find the generic root of a given positive integer.

Example Explanation: Let's take a few examples to understand the process of finding the generic root of a number.

1. Input: 221 Calculation: 2 + 2 + 1 = 5 Output: The generic root of 221 is 5.

2. Input: 786 Calculation: 7 + 8 + 6 = 21, and then 2 + 1 = 3 Output: The generic root of 786 is 3.

3. Input: 19 Calculation: 1 + 9 = 10, and then 1 + 0 = 1 Output: The generic root of 19 is 1.

## Standard Pseudocode

``````function genericRoot(num):
n = absolute value of num
sum = 0

while n is not equal to 0:
sum += n % 10
n /= 10

if n is equal to 0 and sum is greater than 9:
n = sum
sum = 0

return sum
``````

## Algorithm Explanation

1. Start with the input number `num`.
2. If `num` is negative, take its absolute value and assign it to variable `n`.
3. Initialize a variable `sum` to store the running sum of digits.
4. Use a while loop to iterate until the value of `n` becomes 0.
5. Within the loop, extract the last digit of `n` using the modulo operator (%), and add it to `sum`.
6. Remove the last digit of `n` using integer division (n /= 10).
7. If `n` becomes 0 and `sum` is greater than 9, it means `sum` is not a single-digit number. In this case, update the value of `n` with `sum`, and reset `sum` to 0.
8. After the loop finishes, the value of `sum` will be the generic root of the input number `num`.
9. Return the value of `sum`.

## Code Solution

Here given code implementation process.

``````//  C program
//  Generic root of a number
#include <stdio.h>

// Find generic root of a given number
void genericRoot(int num)
{
int n = num;
if (n < 0)
{
n = -n;
}
int sum = 0;
// Execute loop until when the value of n is not zero
while (n != 0)
{
sum += n % 10;
// Remove last digit
n /= 10;
if (n == 0 && sum > 9)
{
//  When n is zero but sum is not a form of single digit
//  Assign sum value to n
n = sum;
sum = 0;
}
}
// Display given number
printf("\n Number : %d", num);
printf("\n Result : %d", sum);
}
int main(int argc, char
const *argv[])
{
// Test case
// 221  (2+2+1) => 5
genericRoot(221);
// 786  (7+8+6) => 21 (2+1) => 3
genericRoot(786);
// 19   (1+9)   => 10 (1+0) => 1
genericRoot(19);
return 0;
}``````

#### Output

`````` Number : 221
Result : 5
Number : 786
Result : 3
Number : 19
Result : 1``````
``````/*
Java program
Generic root of a number
*/
public class Simplification
{
// Find generic root of a given number
public void genericRoot(int num)
{
int n = num;
if (n < 0)
{
n = -n;
}
int sum = 0;
// Execute loop until when the value of n is not zero
while (n != 0)
{
sum += n % 10;
// Remove last digit
n /= 10;
if (n == 0 && sum > 9)
{
//  When n is zero but sum is not a form of single digit
//  Assign sum value to n
n = sum;
sum = 0;
}
}
// Display given number
System.out.print("\n Number : " + num);
System.out.print("\n Result : " + sum);
}
public static void main(String[] args)
{
// Test case
// 221  (2+2+1) => 5
// 786  (7+8+6) => 21 (2+1) => 3
// 19   (1+9)   => 10 (1+0) => 1
}
}``````

#### Output

`````` Number : 221
Result : 5
Number : 786
Result : 3
Number : 19
Result : 1``````
``````// Include header file
#include <iostream>
using namespace std;

/*
C++ program
Generic root of a number
*/

class Simplification
{
public:
// Find generic root of a given number
void genericRoot(int num)
{
int n = num;
if (n < 0)
{
n = -n;
}
int sum = 0;
// Execute loop until when the value of n is not zero
while (n != 0)
{
sum += n % 10;
// Remove last digit
n /= 10;
if (n == 0 && sum > 9)
{
//  When n is zero but sum is not a form of single digit
//  Assign sum value to n
n = sum;
sum = 0;
}
}
// Display given number
cout << "\n Number : " << num;
cout << "\n Result : " << sum;
}
};
int main()
{
// Test case
// 221  (2+2+1) => 5
// 786  (7+8+6) => 21 (2+1) => 3
// 19   (1+9)   => 10 (1+0) => 1
return 0;
}``````

#### Output

`````` Number : 221
Result : 5
Number : 786
Result : 3
Number : 19
Result : 1``````
``````// Include namespace system
using System;
/*
C# program
Generic root of a number
*/
public class Simplification
{
// Find generic root of a given number
public void genericRoot(int num)
{
int n = num;
if (n < 0)
{
n = -n;
}
int sum = 0;
// Execute loop until when the value of n is not zero
while (n != 0)
{
sum += n % 10;
// Remove last digit
n /= 10;
if (n == 0 && sum > 9)
{
//  When n is zero but sum is not a form of single digit
//  Assign sum value to n
n = sum;
sum = 0;
}
}
// Display given number
Console.Write("\n Number : " + num);
Console.Write("\n Result : " + sum);
}
public static void Main(String[] args)
{
// Test case
// 221  (2+2+1) => 5
// 786  (7+8+6) => 21 (2+1) => 3
// 19   (1+9)   => 10 (1+0) => 1
}
}``````

#### Output

`````` Number : 221
Result : 5
Number : 786
Result : 3
Number : 19
Result : 1``````
``````<?php
/*
Php program
Generic root of a number
*/
class Simplification
{
// Find generic root of a given number
public	function genericRoot(\$num)
{
\$n = \$num;
if (\$n < 0)
{
\$n = -\$n;
}
\$sum = 0;
// Execute loop until when the value of n is not zero
while (\$n != 0)
{
\$sum += \$n % 10;
// Remove last digit
\$n = intval(\$n / 10);
if (\$n == 0 && \$sum > 9)
{
//  When n is zero but sum is not a form of single digit
//  Assign sum value to n
\$n = \$sum;
\$sum = 0;
}
}
// Display given number
echo "\n Number : ". \$num;
echo "\n Result : ". \$sum;
}
}

function main()
{
// Test case
// 221  (2+2+1) => 5
// 786  (7+8+6) => 21 (2+1) => 3
// 19   (1+9)   => 10 (1+0) => 1
}
main();``````

#### Output

`````` Number : 221
Result : 5
Number : 786
Result : 3
Number : 19
Result : 1``````
``````/*
Node Js program
Generic root of a number
*/
class Simplification
{
// Find generic root of a given number
genericRoot(num)
{
var n = num;
if (n < 0)
{
n = -n;
}
var sum = 0;
// Execute loop until when the value of n is not zero
while (n != 0)
{
sum += n % 10;
// Remove last digit
n = parseInt(n / 10);
if (n == 0 && sum > 9)
{
//  When n is zero but sum is not a form of single digit
//  Assign sum value to n
n = sum;
sum = 0;
}
}
// Display given number
process.stdout.write("\n Number : " + num);
process.stdout.write("\n Result : " + sum);
}
}

function main()
{
// Test case
// 221  (2+2+1) => 5
// 786  (7+8+6) => 21 (2+1) => 3
// 19   (1+9)   => 10 (1+0) => 1
}
main();``````

#### Output

`````` Number : 221
Result : 5
Number : 786
Result : 3
Number : 19
Result : 1``````
``````#   Python 3 program
#   Generic root of a number

class Simplification :
#  Find generic root of a given number
def genericRoot(self, num) :
n = num
if (n < 0) :
n = -n

sum = 0
#  Execute loop until when the value of n is not zero
while (n != 0) :
sum += n % 10
#  Remove last digit
n = int(n / 10)
if (n == 0 and sum > 9) :
#   When n is zero but sum is not a form of single digit
#   Assign sum value to n
n = sum
sum = 0

#  Display given number
print("\n Number : ", num, end = "")
print("\n Result : ", sum, end = "")

def main() :
#  Test case
#  221  (2+2+1) => 5
#  786  (7+8+6) => 21 (2+1) => 3
#  19   (1+9)   => 10 (1+0) => 1

if __name__ == "__main__": main()``````

#### Output

`````` Number :  221
Result :  5
Number :  786
Result :  3
Number :  19
Result :  1``````
``````#   Ruby program
#   Generic root of a number

class Simplification
#  Find generic root of a given number
def genericRoot(num)
n = num
if (n < 0)
n = -n
end

sum = 0
#  Execute loop until when the value of n is not zero
while (n != 0)
sum += n % 10
#  Remove last digit
n /= 10
if (n == 0 && sum > 9)
#   When n is zero but sum is not a form of single digit
#   Assign sum value to n
n = sum
sum = 0
end

end

#  Display given number
print("\n Number : ", num)
print("\n Result : ", sum)
end

end

def main()
#  Test case
#  221  (2+2+1) => 5
#  786  (7+8+6) => 21 (2+1) => 3
#  19   (1+9)   => 10 (1+0) => 1
end

main()``````

#### Output

`````` Number : 221
Result : 5
Number : 786
Result : 3
Number : 19
Result : 1``````
``````/*
Scala program
Generic root of a number
*/
class Simplification
{
// Find generic root of a given number
def genericRoot(num: Int): Unit = {
var n: Int = num;
if (n < 0)
{
n = -n;
}
var sum: Int = 0;
// Execute loop until when the value of n is not zero
while (n != 0)
{
sum += n % 10;
// Remove last digit
n = (n / 10).toInt;
if (n == 0 && sum > 9)
{
//  When n is zero but sum is not a form of single digit
//  Assign sum value to n
n = sum;
sum = 0;
}
}
// Display given number
print("\n Number : " + num);
print("\n Result : " + sum);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Simplification = new Simplification();
// Test case
// 221  (2+2+1) => 5
// 786  (7+8+6) => 21 (2+1) => 3
// 19   (1+9)   => 10 (1+0) => 1
}
}``````

#### Output

`````` Number : 221
Result : 5
Number : 786
Result : 3
Number : 19
Result : 1``````
``````/*
Swift 4 program
Generic root of a number
*/
class Simplification
{
// Find generic root of a given number
func genericRoot(_ num: Int)
{
var n: Int = num;
if (n < 0)
{
n = -n;
}
var sum: Int = 0;
// Execute loop until when the value of n is not zero
while (n  != 0)
{
sum += n % 10;
// Remove last digit
n /= 10;
if (n == 0 && sum > 9)
{
//  When n is zero but sum is not a form of single digit
//  Assign sum value to n
n = sum;
sum = 0;
}
}
// Display given number
print("\n Number : ", num, terminator: "");
print("\n Result : ", sum, terminator: "");
}
}
func main()
{
// Test case
// 221  (2+2+1) => 5
// 786  (7+8+6) => 21 (2+1) => 3
// 19   (1+9)   => 10 (1+0) => 1
}
main();``````

#### Output

`````` Number :  221
Result :  5
Number :  786
Result :  3
Number :  19
Result :  1``````
``````/*
Kotlin program
Generic root of a number
*/
class Simplification
{
// Find generic root of a given number
fun genericRoot(num: Int): Unit
{
var n: Int = num;
if (n < 0)
{
n = -n;
}
var sum: Int = 0;
// Execute loop until when the value of n is not zero
while (n != 0)
{
sum += n % 10;
// Remove last digit
n /= 10;
if (n == 0 && sum > 9)
{
//  When n is zero but sum is not a form of single digit
//  Assign sum value to n
n = sum;
sum = 0;
}
}
// Display given number
print("\n Number : " + num);
print("\n Result : " + sum);
}
}
fun main(args: Array < String > ): Unit
{
// Test case
// 221  (2+2+1) => 5
// 786  (7+8+6) => 21 (2+1) => 3
// 19   (1+9)   => 10 (1+0) => 1
}``````

#### Output

`````` Number : 221
Result : 5
Number : 786
Result : 3
Number : 19
Result : 1``````

## Resultant Output Explanation

The program is tested with three different inputs: 221, 786, and 19.

1. For the input 221, the generic root is calculated as 2 + 2 + 1 = 5. Hence, the output is displayed as:

``````Number: 221
Result: 5
``````
2. For the input 786, the generic root is calculated as 7 + 8 + 6 = 21, and then 2 + 1 = 3. Hence, the output is displayed as:

``````Number: 786
Result: 3
``````
3. For the input 19, the generic root is calculated as 1 + 9 = 10, and then 1 + 0 = 1. Hence, the output is displayed as:

``````Number: 19
Result: 1
``````

## Time Complexity

The time complexity of the genericRoot function primarily depends on the number of digits in the input `num`. Let's denote the number of digits in `num` as `d`. In the worst case, the while loop will iterate `d` times to sum up all the digits, and in each iteration, constant-time operations are performed. Therefore, the time complexity of the algorithm is O(d), where `d` is the number of digits in the input number `num`. Since the number of digits in a number `num` is proportional to log10(num), the time complexity can be approximated as O(log10(num)).

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post