Python Code Example: calculate cross sum (iterative and recursive)

Iterative version

This is a Python function crossSum that calculates the cross sum of a given number n. The cross sum is calculated as the sum of its digits.

Code Example

def crossSum(n):
    total = 0
    while 0 != n:
        total = int(total + (n % 10))
        n = n / 10

    return total


print(crossSum(123))

Output

6

Code Explanation

This code snippet defines a function to calculate the cross sum (also known as the digit sum) of a given integer. Here’s a detailed explanation of how the code works:

Function crossSum(n)

def crossSum(n):
    total = 0
    while 0 != n:
        total = int(total + (n % 10))
        n = n / 10

    return total
  • Purpose: This function calculates the sum of the digits of the integer n.
  • Parameter: The function takes one parameter n, which is the integer whose digits are to be summed.
  • Initialization:
    • total is initialized to 0. This variable will hold the running total of the sum of the digits.
  • While Loop:
    • The loop continues as long as n is not equal to 0.
    • Inside the loop:
      • n % 10 gives the last digit of n. For example, if n is 123, n % 10 will be 3.
      • This last digit is added to total. The int function ensures that total remains an integer.
      • n = n / 10 removes the last digit from n. However, this operation should be n = n // 10 to ensure that n becomes an integer after the division. Using n = n / 10 converts n to a float, which can cause issues in subsequent iterations. This is likely a bug in the code.
  • Return Value: After the loop completes, the function returns the value of total, which is the sum of the digits of the original n.

Main Program

print(crossSum(123))
  • Function Call and Output:
    • The crossSum function is called with the argument 123.
    • The result, which is the sum of the digits of 123 (i.e., 1 + 2 + 3 = 6), is printed.

Corrected Version

To ensure that the function works correctly with integer division, the n = n / 10 statement should be corrected to n = n // 10. Here is the corrected version:

def crossSum(n):
    total = 0
    while 0 != n:
        total = int(total + (n % 10))
        n = n // 10  # Use integer division to avoid converting to float

    return total

print(crossSum(123))  # Output should be 6

Recursive version

This is a Python function crossSum that calculates the cross sum of a given number n. The cross sum is calculated as the sum of its digits.

Code Example

def crossSum(n):
    return n if n <= 9 else (n % 10) + int(crossSum(n / 10))


print(crossSum(555))

Output

15

Code Explanation

Function crossSum(n)

def crossSum(n):
    return n if n <= 9 else (n % 10) + int(crossSum(n / 10))
  • Purpose: This function calculates the sum of the digits of the integer n using recursion.
  • Parameter: The function takes one parameter n, which is the integer whose digits are to be summed.
  • Base Case:
    • The function checks if n is less than or equal to 9 (i.e., n is a single-digit number).
    • If n is a single-digit number, it simply returns n.
  • Recursive Case:
    • If n is greater than 9, the function performs two main operations:
      • It calculates n % 10, which gives the last digit of n.
      • It makes a recursive call to crossSum with the integer division result of n / 10 (removing the last digit of n). Note: The use of n / 10 here should be n // 10 to ensure integer division.
      • The sum of these two values is returned. The recursive call continues until n is reduced to a single-digit number.
  • Return Value: The function returns the sum of the digits of n.

Main Program

print(crossSum(555))
  • Function Call and Output:
    • The crossSum function is called with the argument 555.
    • The result, which is the sum of the digits of 555 (i.e., 5 + 5 + 5 = 15), is printed.

Corrected Version

To ensure correct integer division, the n / 10 statement should be corrected to n // 10. Here is the corrected version:

def crossSum(n):
    return n if n <= 9 else (n % 10) + int(crossSum(n // 10))

print(crossSum(555))  # Output should be 15

Explanation with Example

Let’s break down the recursive calls for crossSum(555):

  1. First Call: crossSum(555)
    • 555 is greater than 9, so it computes (555 % 10) + crossSum(555 // 10).
    • 555 % 10 is 5.
    • Recursive call: crossSum(555 // 10) which is crossSum(55).
  2. Second Call: crossSum(55)
    • 55 is greater than 9, so it computes (55 % 10) + crossSum(55 // 10).
    • 55 % 10 is 5.
    • Recursive call: crossSum(55 // 10) which is crossSum(5).
  3. Third Call: crossSum(5)
    • 5 is less than or equal to 9, so it returns 5.

Combining the results of these calls:

  • crossSum(555) = 5 + crossSum(55)
  • crossSum(55) = 5 + crossSum(5)
  • crossSum(5) = 5

Thus, the total sum is 5 + 5 + 5 = 15.

The function will correctly print 15 for crossSum(555).