Hello everyone, thought I'd join up as this looks like a good, popular java programming community and a place to learn new things.
I am currently doing a summer research project on the implementation and verification of long integer arithmetic (some may know it as arbitrary-precision arithmetic or bignum). There will be four programs altogether: Addition, subtraction, naive multiplication and karatsuba multiplication.
I have completed the long integer addition program using what our tutor has recommended (convert user input values into an integer array, then perform addition on a units, tens, hundreds, etc basis taking into account carried numbers; like you would do addition in school.)
The result I believe is pretty decent, the program I've made can perform addition of two 50,000 digit numbers and print the solution to a text file in about 3-4 seconds.
My reason for posting it here is so that you can all take a look at the code, review it and make suggestions on improvements and how to increase efficiency (if there is a need to do so).
Obviously if you believe there is a way I can improve the code, I would like to hear merely a hint or a suggestion of a topic/class/method to research as opposed to flat-out typing said improved code.
I look forward to hearing your comments! The code is below:
/************************************************** * LongIntAdd.java - Program which takes in two * long integers and performs an add operation on * them by converting to arrays and operating on a * digit-by-digit basis, taking into account any * carried digits. * * Execution: java LongIntAdd * Input: [Value 1] [Value 2] * Runtime: Two 50000-digit numbers take roughly * 3.5 seconds to add together. *************************************************/ // Package import declarations. import java.util.Scanner; import java.io.*; import static java.lang.Math.*; public class LongIntAdd { public static void main(String args[]) { try { // Scanner class takes user input [Value 1] [Value 2] and stores // as v1 and v2. Scanner in = new Scanner(System.in); String v1 = in.next(); String v2 = in.next(); // Initialize and declare variables (c is the carried number). int vs1 = v1.length(); int vs2 = v2.length(); int vMax = max(vs1,vs2); int vMin = min(vs1,vs2); int[] vArray1 = new int[vMax]; int[] vArray2 = new int[vMax]; int[] sArray = new int[vMax+1]; int c = 0; // Create int arrays from the two strings, working from right to // left. Both arrays are the same size, so if one string is smaller // than the other, the remaining spaces to the left are null values // in the array (0). for(int i = 1; i <= vs1; i++){ vArray1[vMax-i] = Integer.parseInt(v1.substring(vs1-i,vs1-i+1)); } for(int i = 1; i <= vs2; i++){ vArray2[vMax-i] = Integer.parseInt(v2.substring(vs2-i,vs2-i+1)); } // Create solution int array by adding together the digits for the // two value arrays from right to left, taking into account the carried // value. If there is a carry, c is assigned 1 and the array index has // 10 taken away to ensure it only has units and not the tens which is // carried. Otherwise, the carry is assigned 0. for(int i = vMax; i >= 1; i--){ sArray[i] = vArray1[i-1] + vArray2[i-1] + c; if (sArray[i] > 9) { c=1; sArray[i]=sArray[i]-10; } else { c=0; } } // The first index in the solution array is simply the carry value, whether // it be 0 or 1. sArray[0] = c; // Converts the solution int array back into a string and writes it to a text // file. Checks to see whether the first digit is 0 (in other words, there // was no carry from the final addition). If so, it is ignored; else, it is // kept. BufferedWriter out = new BufferedWriter(new FileWriter("LongIntAdditionSolution.txt")); int nullCheck = 0; if (sArray[0] == 0) { nullCheck = 1; } for (int i = nullCheck; i < sArray.length; i++) { out.write(Integer.toString(sArray[i])); } out.close(); } catch (IOException e) { System.err.println("There has been an error!"); e.printStackTrace(); } } }
Oh and I'm all for good programming practice, so if there's anything here that may be bad practice, please tell me and suggest improvements for them!