Last active
December 17, 2020 02:51
-
-
Save Plotist/16a3a8000b6c7b98152a6646e1b62e9b to your computer and use it in GitHub Desktop.
Natural numbers recursive multiplication
This file contains hidden or 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
def mult(a,b) | |
return 0 if a == 0 || b == 0 | |
if a == 1 | |
b | |
else | |
b+mult(a-1,b) | |
end | |
end | |
puts mult(7,7) # 49 | |
puts mult(0,7) # 0 | |
puts mult(7,0) # 0 | |
puts mult(4,7) # 28 | |
puts mult(9,2) # 18 | |
puts mult(4,6) # 24 | |
puts mult(6,1) # 6 | |
puts mult(1, 6) # 6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment