Sum of Squares of Digits of a number in Python

In this Python tutorial, we will be discussing various methods to calculate the sum of squares of digits of a number in Python.

The sum of squares of digits of a number is a mathematical concept that can be useful in various programming applications. In Python, you can calculate the sum of squares of digits of a number using a simple algorithm. From using a while loop, recursion, list comprehension, map and lambda function, a list of digits.

There are several ways to compute the squares of digits of a number in Python. Here is the list.

  • Sum of Squares of Digits of a number in Python using while loop
  • Sum of Squares of Digits of a number in Python Using recursion
  • Sum of Squares of Digits of a number in Python Using list comprehension
  • Sum of Squares of Digits of a number in Python Using list of digits
  • Sum of Squares of Digits of a number in Python Using map and lambda function

Sum of Squares of Digits of a number in Python using while loop

This method uses a while loop to iterate through each digit of the number. In each iteration, the last digit of the number is obtained using the modulo operator, squared, and added to a variable that keeps track of the sum of the squares of digits.

The last digit of the number is then removed using the floor division operator, and the loop continues until the number becomes 0.

Here is an example.

  • Define a function called sum_of_squares_of_digits(num) that takes in a number as its argument.
  • Initialize a variable sum_of_squares to 0, which will keep track of the sum of the squares of the digits.
  • Use a while loop to iterate through each digit of the number.
  • In each iteration, use the modulo operator (%) to get the last digit of the number.
  • Square the digit and add it to the sum_of_squares variable.
  • Use the floor division operator (//) to remove the last digit of the number.
  • Continue the loop until the number becomes 0.
  • Return the sum_of_squares variable as the final result.
def sum_of_squares_of_digits(num):
    sum_of_squares = 0
    while num > 0:
        digit = num % 10 #get the last digit
        sum_of_squares += digit ** 2
        num = num // 10 #remove the last digit
    return sum_of_squares

print(sum_of_squares_of_digits(123))
print(sum_of_squares_of_digits(456))
Sum of Squares of Digits of a number in Python using while loop
Sum of Squares of Digits of a number in Python using while loop

Read: Python program to find the sum of n numbers

Sum of Squares of Digits of a number in Python Using recursion

This method uses recursion to calculate the sum of squares of digits of a number. The base case for the recursion is when the number is 0, in which case the function returns 0.

In each recursive call, the last digit of the number is obtained using the modulo operator, squared, and added to the sum of squares of digits of the remaining number after removing the last digit using the floor division operator.

The function is called recursively with the remaining number as the argument, and the recursion continues until the base case is reached.

Here is an example.

  • Define a function called sum_of_squares_of_digits(num) that takes in a number as its argument.
  • Set the base case for the recursion as when the number is 0, in which case the function returns 0.
  • In each recursive call, use the modulo operator (%) to get the last digit of the number.
  • Square the digit and add it to the sum of squares of digits of the remaining number after removing the last digit by using the floor division operator (//).
  • Call the function recursively with the remaining number as the argument.
  • Continue the recursion until the base case is reached.
  • Return the sum of squares as the final result.
Sum of Squares of Digits of a number in Python using recursion
Sum of Squares of Digits of a number in Python using recursion

Read: Sum of even digits of a number in Python

Sum of Squares of Digits of a number in Python Using list comprehension

This method uses list comprehension to convert the number to a list of digits and then uses python’s built-in function sum() to add all the elements of the list after squaring them.

Here is an example.

  • Define a function called sum_of_squares_of_digits(num) that takes in a number as its argument.
  • Convert the number to a string using str(num).
  • Use a list comprehension to get a list of digits of the number and square each element of the list.
  • Use the python built-in function sum() to add all the elements of the list.
  • Return the sum as the final result.
Sum of Squares of Digits of a number in Python using list comprenshion
Sum of Squares of Digits of a number in Python using list comprehension

Read: How to find the sum of digits of a number in Python

Sum of Squares of Digits of a number in Python Using map and lambda function

We use the built-in map() function to apply a lambda function (an anonymous function) to each digit of the number, which squares the digit. The sum() function is then used to add up the squared digits.

Here is an example.

  • Define a function called sum_of_squares_of_digits(num) that takes in a number as its argument.
  • Convert the number to a string using str(num).
  • Use the built-in map() function to apply a lambda function (an anonymous function) to each digit of the number, which squares the digit.
  • Use the built-in sum() function to add up the squared digits.
  • Return the sum as the final result.
def sum_of_squares_of_digits(num):
    return sum(map(lambda x: int(x) ** 2, str(num)))

print(sum_of_squares_of_digits(123))
print(sum_of_squares_of_digits(456))
Sum of Squares of Digits of a number in Python Using map and lambda function
Sum of Squares of Digits of a number in Python Using map and lambda function

Read: How to print factorial of a number in Python

Sum of Squares of Digits of a number in Python Using list of digits

In this method, we first convert the number to a list of digits using the map() function and the int() method. Then, we use a list comprehension to square each digit and sum them up using the sum() function.

Here is an example.

  • Define a function called sum_of_squares_of_digits(num) that takes in a number as its argument.
  • Convert the number to a list of digits using the map() function and the int() method.
  • Use a list comprehension to square each digit of the list.
  • Use the sum() function to add all the elements of the list.
  • Return the sum as the final result,
def sum_of_squares_of_digits(num):
    digits = list(map(int, str(num)))
    return sum([digit ** 2 for digit in digits])

print(sum_of_squares_of_digits(123))
print(sum_of_squares_of_digits(456))
Sum of Squares of Digits of a number in Python Using list of digits
Sum of Squares of Digits of a number in Python Using list of digits

You may also like to read the following Python tutorials.

Conclusion

In conclusion, there are multiple ways to calculate the sum of squares of digits of a number in Python. Each method has its own advantages and disadvantages, and it’s essential to choose the one that best suits your specific needs and requirements.

We have discussed 5 different ways of calculating the sum of squares of digits of a number in Python, using a while loop, recursion, list comprehension, map and lambda function, and a list of digits.

We hope that this Python tutorial has helped you understand the different ways to calculate the sum of squares or digits of a number in Python and that you can now confidently implement these methods in your programming projects.

  • Sum of Squares of Digits of a number in Python using while loop
  • Sum of Squares of Digits of a number in Python Using recursion
  • Sum of Squares of Digits of a number in Python Using list comprehension
  • Sum of Squares of Digits of a number in Python Using list of digits
  • Sum of Squares of Digits of a number in Python Using map and lambda function