0

Hi there,

I usually feel really bad posting a whole bunch of code, but might any of you have an idea of why I'm getting an output of 105553 for 55555*66666? I'm supposed to use the A La Russe algorithm for multiplication:
n*m m if n = 1
(n/2)*2m if n is even
(n/2)*2m+m if n is odd

Thanks so much!

public ArrayList<Integer> multiply(ArrayList<Integer> n, ArrayList<Integer> m)
	{
		ArrayList<Integer> temp = new ArrayList(); 
		m = removeZeroes(m);
		n = removeZeroes(n);
		if(m.get(m.size()-1) == 1) //If it's one
		{
			temp = new ArrayList<Integer>(n);
			return temp;
		}
		else if(n.get(n.size()-1) == 1)
		{
			temp = new ArrayList<Integer>(m);
			return temp; 
		}
		else if(n.get(n.size()-1) % 2 == 0) //If it's an even number
		{
			return (multiply((divide(n)), multiplyByInt(m, 2)));
		}
		else //If it's an odd number
		{
			return add(multiply(divide(n), multiplyByInt(m, 2)), m);
		}
	}

Let me know if you need more code -- the divide, add, and multiplyByInt methods seem to be working correctly.

Edited by dark_sider_1: n/a

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by NormR1
0

You don't post the correct answer.
Will the correct answer fit into the type of variable you are using?

Otherwise have you used smaller values, done the math steps manually and compared the results of your program with your manual method to see if your code is doing what you want it to do?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.