View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Given a number and the task is to find sum of digits of this number in Python.
Examples:
Input : n = 87
Output : 15
Input : n = 111
Output : 3
Below are the methods to sum of the digits.
Method-1: Using str[] and int[] methods.: The str[] method is used to convert the number to string. The int[] method is used to convert the string digit to an integer.
Convert the number to string and iterate over each digit in the string and after converting each digit to integer and add to the sum of the digits in each iteration.
Python3
def
getSum[n]:
sum
=
0
for
digit
in
str
[n]:
sum
+
=
int
[digit]
return
sum
n
=
12345
print
[getSum[n]]
Output:
15
Method-2: Using sum[] methods.: The sum[] method is used to sum of numbers in the list.
Convert the number to string using str[] and strip the string and convert to list of number using strip[] and map[] method resp. Then find the sum using the sum[] method.
Python3
def
getSum[n]:
strr
=
str
[n]
list_of_number
=
list
[
map
[
int
, strr.strip[]]]
return
sum
[list_of_number]
n
=
12345
print
[getSum[n]]
Output:
15
Method-3: Using General Approach:
- Get the number
- Declare a variable to store the sum and set it to 0
- Repeat the next two steps till the number is not 0
- Get the rightmost digit of the number with help of remainder ‘%’ operator by dividing it with 10 and add it to sum.
- Divide the number by 10 with help of ‘//’ operator
- Print or return the sum
A. Iterative Approach:
Python3
def
getSum[n]:
sum
=
0
while
[n !
=
0
]:
sum
=
sum
+
[n
%
10
]
n
=
n
/
/
10
return
sum
n
=
12345
print
[getSum[n]]
Output:
15
B. Recursive Approach:
Python3
def
sumDigits[no]:
return
0
if
no
=
=
0
else
int
[no
%
10
]
+
sumDigits[
int
[no
/
10
]]
n
=
12345
print
[sumDigits[n]]
Output:
15
Why is the highest rated answer 3.70x slower than this ?
% echo; [ time [nice echo 33785139853861968123689586196851968365819658395186596815968159826259681256852169852986 \
| mawk2 'gsub[//,[$_][$_][$_]]+gsub[//,[$_]]+1' | pvE0 \
| mawk2 '
function __[_,___,____,_____] {
____=gsub["[^1-9]+","",_]~""
___=10
while[[+____ ] 0% ETA 0:00:00
out9: 0.00 B 0:00:51 [0.00 B/s] [0.00 B/s] [ ]
in0: 2.82GiB 0:00:51 [56.6MiB/s] [56.6MiB/s] [=============================>] 100%
out9: 11.0 B 0:00:51 [ 219miB/s] [ 219miB/s] [ ]
[ pvE 0.1 in0 < testcases_more108.txt | python3 -c | pvE 0.1 out9; ]
48.07s user 3.57s system 100% cpu 51.278 total
1 3031397722
Even the smaller test case managed a 1.42x speed up :
echo; [ time [nice echo 33785139853861968123689586196851968365819658395186596815968159826259681256852169852986 \
| mawk2 'gsub[//,[$_][$_]$_]+gsub[//,$_]+1' ORS='' | pvE0 | python3 -c 'import re, sys; print[sum[[ sum[int[_]*re.subn[_,"",__][1] for _ in [r"1",r"2", r"3",r"4",r"5",r"6",r"7",r"8",r"9"]] for __ in sys.stdin ]]]' | pvE9 ]] |gcat -n | ggXy3 | lgp3
in0: 484MiB 0:00:00 [2.02GiB/s] [2.02GiB/s] [ ]
out9: 11.0 B 0:00:24 [ 451miB/s] [ 451miB/s] [ ]
[ nice echo | mawk2 'gsub[//,[$_][$_]$_]+gsub[//,$_]+1' ORS='' | pvE 0.1 in0]
20.04s user 5.10s system 100% cpu 24.988 total
1 2822068024