Another method to multiply two numbers without usi

2020-05-24 21:12发布

I had an interesting interview yesterday where the interviewer asked me a classic question: How can we multiply two numbers in Java without using the * operator. Honestly, I don't know if it's the stress that comes with interviews, but I wasn't able to come up with any solution.

After the interview, I went home and breezed through SO for answers. So far, here are the ones I have found:

First Method: Using a For loop

// Using For loop
public static int multiplierLoop(int a, int b) {
    int resultat = 0;
    for (int i = 0; i < a; i++) {
        resultat += b;
    }

    return resultat;
}

Second Method: Using Recursion

// using Recursion
public static int multiplier(int a, int b) {

    if ((a == 0) || (b == 0))
        return 0;
    else
        return (a + multiplier(a, b - 1));

}

Third Method: Using Log10

**// Using Math.Log10
public static double multiplierLog(int a, int b) {
    return Math.pow(10, (Math.log10(a) + Math.log10(b)));
}**

So now I have two questions for you:

  1. Is there still another method I'm missing?
  2. Does the fact that I wasn't able to come up with the answer proves that my logical reasoning isn't strong enough to come up with solutions and that I'm not "cut out" to be a programmer? Cause let's be honest, the question didn't seem that difficult and I'm pretty sure most programmers would easily and quickly find an answer.

标签: java
9条回答
够拽才男人
2楼-- · 2020-05-24 21:35

One solution is to use bit wise operations. That's a bit similar to an answer presented before, but eliminating division also. We can have something like this. I'll use C, because I don't know Java that well.

uint16_t multiply( uint16_t a, uint16_t b ) {
  uint16_t i = 0;
  uint16_t result = 0;

  for (i = 0; i < 16; i++) {
    if ( a & (1<<i) ) {
      result += b << i;
    }
  }
  return result;
}
查看更多
叼着烟拽天下
3楼-- · 2020-05-24 21:38

If you don't have integer values, you can take advantage of other mathematical properties to get the product of 2 numbers. Someone has already mentioned log10, so here's a bit more obscure one:

public double multiply(double x, double y) {
    Vector3d vx = new Vector3d(x, 0, 0);
    Vector3d vy = new Vector3d(0, y, 0);
    Vector3d result = new Vector3d().cross(vx, vy);
    return result.length();
}
查看更多
小情绪 Triste *
4楼-- · 2020-05-24 21:43

The questions interviewers ask reflect their values. Many programmers prize their own puzzle-solving skills and mathematical acumen, and they think those skills make the best programmers.

They are wrong. The best programmers work on the most important thing rather than the most interesting bit; make simple, boring technical choices; write clearly; think about users; and steer away from stupid detours. I wish I had these skills and tendencies!

If you can do several of those things and also crank out working code, many programming teams need you. You might be a superstar.


But what should you do in an interview when you're stumped?

Ask clarifying questions. ("What kind of numbers?" "What kind of programming language is this that doesn't have multiplication?" And without being rude: "Why am I doing this?") If, as you suspect, the question is just a dumb puzzle with no bearing on reality, these questions will not produce useful answers. But common sense and a desire to get at "the problem behind the problem" are important engineering virtues.

The best you can do in a bad interview is demonstrate your strengths. Recognizing them is up to your interviewer; if they don't, that's their loss. Don't be discouraged. There are other companies.

查看更多
登录 后发表回答