Last active
January 20, 2019 04:35
-
-
Save DeeJayhX/a6866fc465a37bb327128a6a849ef69e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Super inefficient adding algorithm | |
### By: Derek Steindel | |
#set variables | |
##STEP 1 | |
carry = 0 | |
##STEP 2 | |
i = 0 | |
result = "" | |
###define functions | |
#function to reverse numbers since addition | |
#works from right to left | |
def reverse(num): | |
return num[::-1] | |
first = input('Enter the first number to add: ') | |
second = input('Enter the second number to add: ') | |
#define seperate variables for reversed integers | |
#so we can reuse the regular integers for output | |
#at the end of the script | |
firstr = reverse(first) | |
#print(first) | |
secondr = reverse(second) | |
#print(second) | |
#figure out which number is bigger, and set i's | |
#length to stop there | |
if len(firstr) > len(secondr): | |
length = len(firstr) | |
else: | |
length = len(secondr) | |
##STEP 3 | |
while i <= length - 1: | |
mod = 10 * ( i + 1 ) | |
#make sure the digit exists, or substitute 0 | |
#this is incase we put in numbers of different | |
#lengths to add, such as 24 and 5240 | |
try: | |
a = int(firstr[i]) | |
except: | |
a = 0 | |
try: | |
b = int(secondr[i]) | |
except: | |
b = 0 | |
##STEP 4 | |
bit = a+b+carry | |
##STEP 5 | |
carry = 0 | |
if bit >= 10: | |
carry = 1 | |
bit = bit % 10 | |
##STEP 6 | |
i += 1 | |
##STEP 7-ish | |
result = str(bit) + str(result) | |
##STEP 8 | |
print(first + " + " + second + " = " +result) | |
##STEP 9 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment