Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiply without + or *

I'm working my way through How to Design Programs on my own. I haven't quite grasped complex linear recursion, so I need a little help.

The problem: Define multiply, which consumes two natural numbers, n and x, and produces n * x without using Scheme's *. Eliminate + from this definition, too.

Straightforward with the + sign:

(define (multiply n m)
  (cond
    [(zero? m) 0]
    [else (+ n (multiply n (sub1 m)))]))

(= (multiply 3 3) 9)

I know to use add1, but I can't it the recursion right.

Thanks.

like image 670
KaizenSoze Avatar asked Nov 25 '11 14:11

KaizenSoze


People also ask

Is * used for multiplication?

The multiplication sign, also known as the times sign or the dimension sign, is the symbol ×, used in mathematics to denote the multiplication operation and its resulting product.

How do you multiply two numbers without the * operand?

1. First Method: Using Recursion to multiply two numbers without using *. We can easily calculate the product of two numbers using recursion without using * multiplication operator. // If second number is zero then the product is zero.

Is * used as a symbol for multiplication?

multiplication sign (×)

What happens if you multiply 0 * 0?

The multiplication property of zero: Regardless of what the other number is, multiplying by zero always results in an answer of zero.

Does or mean to multiply or add?

The best way to learn when to add and when to multiply is to work out as many probability problems as you can. But, in general: If you have “or” in the wording, add the probabilities. If you have “and” in the wording, multiply the probabilities.


2 Answers

Split the problem in two functions. First, you need a function (add m n) which adds m to n. What is the base case? when n is zero, return m. What is the recursive step? add one to the result of calling add again, but decrementing n. You guessed it, add1 and sub1 will be useful.

The other function, (mul m n) is similar. What is the base case? if either m or n are zero, return 0. What is the recursive step? add (using the previously defined function) m to the result of calling mul again, but decrementing n. And that's it!

like image 71
Óscar López Avatar answered Oct 22 '22 23:10

Óscar López


Since this is almost certainly a homework-type question, hints only.

How do you add 7 and 2? While most people just come up with 9, is there a more basic way?

How about you increment the first number and decrement the second number until one of them reaches zero?

Then the other one is the answer. Let's try the sample:

7 2
8 1
9 0 <- bingo

This will work fine for natural numbers though you need to be careful if you ever want to apply it to negatives. You can get into the situation (such as with 10 and -2) where both numbers are moving away from zero. Of course, you could check for that before hand and swap the operations.

So now you know can write + in terms of an increment and decrement instruction. It's not fantastic for recursion but, since your multiply-by-recursive-add already suffers the same problem, it's probably acceptable.

Now you just have to find out how to increment and decrement in LISP without using +. I wonder whether there might be some specific instructions for this :-)

like image 43
paxdiablo Avatar answered Oct 23 '22 01:10

paxdiablo