forked from codehouseindia/Python-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSum_very_large_numbers
More file actions
47 lines (28 loc) · 846 Bytes
/
Sum_very_large_numbers
File metadata and controls
47 lines (28 loc) · 846 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
def findSum(str1, str2):
if (len(str1) > len(str2)):
t = str1;
str1 = str2;
str2 = t;
str = "";
n1 = len(str1);
n2 = len(str2);
# Reverse both of strings
str1 = str1[::-1];
str2 = str2[::-1];
carry = 0;
for i in range(n1):
sum = ((ord(str1[i]) - 48) +
((ord(str2[i]) - 48) + carry));
str += chr(sum % 10 + 48);
carry = int(sum / 10);
for i in range(n1, n2):
sum = ((ord(str2[i]) - 48) + carry);
str += chr(sum % 10 + 48);
carry = (int)(sum / 10);
if (carry):
str += chr(carry + 48);
str = str[::-1];
return str;
str1 = "12";
str2 = "198111";
print(findSum(str1, str2));