# Multiply by 9 without multiplying by 9, using vedic math

# digit containing 9 has to be greater or equal in length

mul9 = { str(i): str(9-i) for i in range(10)}

print(f"{mul9}\n")

number1 = int(input("Enter 9s': "))
len1 = len(str(number1))
number2 = int(input(f"Enter (0 - {str(9)*len1}): "))

if len(str(number1)) < len(str(number2)):
print("This trick won't work")

else:
res = str(number2 - 1)
print(f"{number2} - 1 = {res} ")

end = ''

for i in res:
end += mul9[i]
print(f"{i} needs {mul9[i]} to become 9")

res += (len(str(number1)) - len(str(number2))) * "9" + end # This accounts for adding the invisible 0s at the start of 'res' in the video. I've simply added the correct number of 9s and avoided but avoided looping through them in the for loop.

print(res)


gives

{'0': '9', '1': '8', '2': '7', '3': '6', '4': '5', '5': '4', '6': '3', '7': '2', '8': '1', '9': '0'}

Enter 9s': 99
Enter (0 - 99): 99
99 - 1 = 98
9 needs 0 to become 9
8 needs 1 to become 9
9801

[Program finished]


The objective of the code is to demo students how we can multiply large numbers in smallest amount of time. I am new to python so not that aware of syntax. Works as expected. Can you please point out at lines which can be reduced.

• Introduce some validation loops, rather than just bailing when the trick won't work
• Rephrase your outputs to be real equations - there's no reason not to
• Rearrange your digit storage order so that first things are added to a list first, and at the end call join
• Beware your use of parens in your first prompt; this suggests an open interval when I think you want a closed interval. Your original meaning aside, I don't think that your (0 - 99) is actually supported - 0 crashes, so I'll suggest that you instead ask for a minimum of 1.
• Storing mul9 as a dictionary is more effort than it's worth. Just calculate the difference for each digit.
• You might as well include a test to verify that your answer is correct.
• Explain how you get to the number of middle-9 digits.

## Suggested

while True:
number1 = input('Enter a number whose digits are all 9: ')
if set(number1) == {'9'}:
break
len1 = len(number1)
number1 = int(number1)

while True:
number2 = input(f'Enter [1-{number1}]: ')
len2 = len(number2)
try:
number2 = int(number2)
except ValueError:
continue
if 0 < number2 <= number1:
break

res = number2 - 1
digits = [str(res), '9'*(len1 - len2)]
print(
f'{number2} - 1 = {res}\n'
f'The difference of the input lengths '
f'{len1}-{len2}={len1-len2} is the number '
f'of "9" digits to insert in the middle'
)

for str_i in str(res):
i = int(str_i)
mul9 = 9 - i
digits += str(mul9)
print(f'{i} + {mul9} = 9')

ans = int(''.join(digits))
if number1 * number2 != ans:
raise ValueError('Unexpected algorithm failure')
print(f'{number1} * {number2} = {ans}')


## Output

Enter a number whose digits are all 9: 9999
Enter [1-9999]: 526
526 - 1 = 525
The difference of the input lengths 4-3=1 is the number of "9" digits to insert in the middle
5 + 4 = 9
2 + 7 = 9
5 + 4 = 9
9999 * 526 = 5259474

• my code misses how 9 becomes part of final answer, so is urs.... i will debug that and thanks for suggestion Jul 13, 2021 at 17:49
• @Subham done, though obviously this one isn't as easy to phrase in the form of an equation. Jul 13, 2021 at 18:04