Advertisement
zvoulgaris

Handicapped Multiplication

Feb 6th, 2020
2,660
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 0.64 KB | None | 0 0
  1. # Handicapped Multiplication
  2. # This is a drill, just to cultivate creativity in programming practices, particularly algorithmic design.
  3. # There are more efficient ways to do multiplication in any language, so this function is not designed to be used elsewhere.
  4.  
  5. # Objective: create a method for multiplying two integers using only the operators / and +
  6.  
  7. function hm(x::Int64, y::Int64)
  8.     s = 0
  9.  
  10.     while x > 0
  11.         if isodd(x); s += y; end
  12.         x = div(x, 2)
  13.         y += y
  14.     end
  15.  
  16.     return s
  17. end
  18.  
  19. # Alternatively, the challenge could be modified so that the operators /n and *n are used, where n is an integer greater than 1.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement