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.

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.