Skip to content

Instantly share code, notes, and snippets.

@Plotist
Last active December 17, 2020 02:51
Show Gist options
  • Save Plotist/16a3a8000b6c7b98152a6646e1b62e9b to your computer and use it in GitHub Desktop.
Save Plotist/16a3a8000b6c7b98152a6646e1b62e9b to your computer and use it in GitHub Desktop.
Natural numbers recursive multiplication
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