Skip to content

Instantly share code, notes, and snippets.

@5war00p
Created December 25, 2021 16:06
Show Gist options
  • Save 5war00p/d6ddd2c6eddda317f3122e2e5eacf002 to your computer and use it in GitHub Desktop.
Save 5war00p/d6ddd2c6eddda317f3122e2e5eacf002 to your computer and use it in GitHub Desktop.
easy_multplication.py contains a function which finds multiplication of two numbers using a digit by digit mulitplication (single digit multiplication) of a multiplier. Again the multiplier will be decided based on the number of digits of both numbers, the number with less digits will be selected as multiplier. This method works better when we d…
def easy_mul(a,b):
'''
params: int a
int b
return: int
description: It finds multiplications of 2 numbers
using the digit by digit of a multipler.
'''
# finding lens
a_len = len(str(a))
b_len = len(str(b))
# base condition
if a_len == 1 or b_len:
return a*b
if a_len > b_len:
lastdig_b = b % 10
new_b = b // 10
return (a*lastdig_b) + easy_mul(a, new_b)
else:
lastdig_a = a % 10
new_a = a // 10
return (lastdig_a * b) + easy_mul(new_a, b)
if __name__ == '__main__':
# taking inputs
a = int(input('Enter number-1: '))
b = int(input('Enter number-2: '))
result = easy_mul(a, b)
print(f'\n{a} * {b} = {result}\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment