To perform multiplication using only subtraction in python, we can simply create a function that takes two arguments, a and b, and iteratively subtracts the value of a from the product of a and b until the value of b becomes zero. The result of the function is the final value of a, which is the product of the original values of a and b.
Here's the code:
main.py127 chars7 lines
We can test the function using the following code:
main.py87 chars5 lines
This will output:
main.py11 chars2 lines
Note that while this approach works, it is not the most efficient way to perform multiplication in python. It is generally better to use the built-in *
operator or the numpy
library for more complex operations.
gistlibby LogSnag