Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

How to efficiently multiply two numbers represented as Strings, in Java?

/*
	This is an implementation that demonstrates
	how to efficiently multiply two int numbers
	that are represented as strings.

	Let M be the number of digits in first
	and N the number of digits in second.

	Time complexity: O(M*N) 
	Space complexity: O(M+N)
*/

public class MultiplyStrings {
	public static void main(String[] args) {
		String num1 = "123", num2 = "456";
		System.out.println(multiply(num1, num2)); // 56088
	}
	private static String multiply(String num1, String num2) {
		// If either is 0, return 0
		if (num1.equals("0") || num2.equals("0")) {
			return "0";
		}
		// Convert the numbers into StringBuilder objects
		StringBuilder firstNum = new StringBuilder(num1);
		StringBuilder secondNum = new StringBuilder(num2);

		// Reverse both numbers before performing the multiplication
		firstNum.reverse();
		secondNum.reverse();

		// Create answer variable as placeholder for
		// multiplication of each digit of firstNum with secondNum
		int N = firstNum.length() + secondNum.length();
		StringBuilder answer = new StringBuilder();
		for (int i = 0; i < N; i++) {
			answer.append("0");
		}
		int digit1, digit2;
		for (int place2 = 0; place2 < secondNum.length(); place2++) {
			digit2 = secondNum.charAt(place2) - '0';
			// Multiply current digit2 by all digits in firstNum
			for (int place1 = 0; place1 < firstNum.length(); place1++) {
				digit1 = firstNum.charAt(place1) - '0';
				// Current position in answer depends on place
				// of digit2 and place of digit1
				int currentPos = place1 + place2;
				// Digit currently at position currentPos is carried over
				// and summed with current result
				int carry = answer.charAt(currentPos) - '0';
				int product = digit1 * digit2 + carry;
				// Set the ones place of product
				answer.setCharAt(currentPos, (char) (product % 10 + '0'));
				// Add the carry to next position
				int value = (answer.charAt(currentPos + 1) - '0') +
						product / 10;
				answer.setCharAt(currentPos + 1, (char) (value + '0'));
			}
		}
		// Remove excess 0 from the rear of the answer
		if (answer.charAt(answer.length() - 1) == '0') {
			answer.deleteCharAt(answer.length() - 1);
		}
		answer.reverse();
		return answer.toString();
	}

}
Comment

PREVIOUS NEXT
Code Example
Java :: how to iterate hashset in java 8 
Java :: How to compare lists of custom classes without defining equals() and hashCode()? 
Java :: java for loop 
Java :: java code to reverse an integer 
Java :: java days between two dates 
Java :: how to sort a list of integers in java 
Java :: send variable intent 
Java :: spring @Bean destroyMethod 
Java :: duplicate local variable in java 
Java :: java get unique elements from array 
Java :: convert optional object to object java 
Java :: find if a number is in a 2d array java 
Java :: how-to-check-internet-connection-in-android 
Java :: change drawable color programmatically android 
Java :: java string array 
Java :: how to create immutable list java 
Java :: multiplication table in java 
Java :: java swing pic 
Java :: java log2 
Java :: shell sort java 
Java :: java generate secure random password 
Java :: try catch java 
Java :: java string remove more than one space 
Java :: open file java 
Java :: collections.reverseorder() in java 
Java :: java foreach 
Java :: how to get width android 
Java :: java find last element in array 
Java :: javafx stackpane set position 
Java :: how to change label text color javafx 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =