Sum of Square Digits
easySave
Math
Write a function that takes a non-negative integer n as input and returns the sum of the squares of its digits.
For example, given the number 23, the function should return 4 + 9 = 13, as 2 squared is 4 and 3 squared is 9.
Example 1
Input
[ 23 ]
Output
13
Explanation. 2 squared is 4 and 3 squared is 9, and their sum is 13.
Example 2
Input
[ 45 ]
Output
41
Explanation. 4 squared is 16, 5 squared is 25; 16 + 25 = 41.
Example 3
Input
[ 111 ]
Output
3
Explanation. Each digit is 1, and squared is still 1. With three digits, the sum is 3.
Example 4
Input
[ 999 ]
Output
243
Explanation. 9 squared is 81, and with three nines, the sum is 3 * 81 = 243.
Example 5
Input
[ 0 ]
Output
0
Explanation. 0 squared is 0.
Follow-up: Can you solve the problem in a single pass through the input number?
Constraints:
The input number will be a non-negative integer.
- Views
- 1