Working With Number – Infinity Multiplication – Chapter 3B – Java

Thank you all my reader for following up the “Infinity” subject. This chapter is the sub-release chapter of chapter 3 – Optimised The Code for Java in the Infinity subject. This is a very short chapter before I move onto chapter four of the subject. I will start releasing chapter four of the Infinity subject as early as Saturday this week or latest is by Sunday this week. The modification in the code is just to make the code operate a little bit quicker.

There was only one thing I modified in the code to make the code operate a little bit more quicker. I used the Java’s concat() string prototype function for joining the string together instead of the plus sign. I did the same benchmark test for this version of infiX, and this version of infiX took 2.400 seconds to computes the multiplication result 100,000 times for the same two strings of digits, each string contained 40 digits and both strings did not contain the decimal. The earlier production version for normal usage of infiX took 2.865 seconds to compute the result for the same test. With the decimal, this version of infiX took 3.013 seconds while the earlier version of infiX took 3.331 seconds.

What took me so long to release this chapter although it was short? In the last couple days, I have been a bit busy. I just finished launching a brand new web directory for anyone who owns a blog or a website that contain a vast amount of articles can submit their web’s link. If you have a blog, please feel free to submit your website’s link at http://directory.iblog.website. Iblog.website‘s main site was also launched. I also launched a brand web shop to separate the e-commerce operation from the blog. The shop can be found at http://shop.iblog.website.

Previous Chapter: Optimised The CodeNext Chapter: To Be Release

This is the source code for infiX-vr-2.

Advertisement

// "Copyright Notice", please do not remove.
// Written by Kevin Ng
// The full tutorial on this subject can be found @ http://kevinhng86.iblog.website or http://programming.world.edu.
// Release date to http://programming.world.edu will lag one week after release on http://kevinhng86.iblog.website
// This source code file is a part of Kevin Ng's Z library.
// This source code is licenses under CCDL-1.0  A copy of CDDL1.0 can be found at https://opensource.org/licenses/CDDL-1.0
// End "Copyright Notice"
// 
// Notice: Version: InfiX-vr-2 (Infinity Multiplication) | Normal Usage | Bound by some limitation. 
//         The first limitation of this version is how much memory the Java Programming Language allow the string to be store.
//         The second limitation is how much memory Java's environment can assign to the program.
//         If memory allow, for this version a string can only be less than 1,073,741,823 digits
//         There is a tester code ready to go to test against the bigDecimal 
//
//    Example of memory usage calculation:
//         500 megabyte string of digits multiply to another 500 megabyte string of digits.
//         1000 Mb of memory to store the strings outside of the function.
//         1000 Mb of memory for when the strings are passing into the function.
//         1000 Mb for the answer string.
//          100 Mb (this can vary but most likely to be less than this) for script operation.
//         3100 Mb of memory is requires for adding two strings of digits that costed 500MB per string.
public class libZ
{
    public static void main(String[] args)
    {
        String x = "1234567890123456789012345678901234567890";
        String y = "1234567890123456789012354678901234567890";
        String xy = infiX(x,y);                        
        System.out.println(xy);
    }
    
    public static String infiX(String a, String b)
    { 
        boolean isaNeg = a.charAt(0) == '-';
        boolean isbNeg = b.charAt(0) == '-';       
        a = a.replaceAll("^[-+]+", "");
        b = b.replaceAll("^[-+]+", "");    
        a = a.replaceAll("^0+", "");
        b = b.replaceAll("^0+", ""); 
          
        int aDecPos = a.indexOf('.');
        int bDecPos = b.indexOf('.');
        int oDecPos = 0;
        
      
        if ( aDecPos > -1){
            a = a.replaceAll("0+$", "");
            aDecPos = ((a.length() - 1 - aDecPos) > 0)? (a.length() - 1 - aDecPos) : 0;
            a = a.replaceAll("[.]", ""); 
        }
        if ( bDecPos > -1){
            b = b.replaceAll("0+$", "");
            bDecPos = ((b.length() - 1 - bDecPos) > 0)? (b.length() - 1 - bDecPos) : 0;
            b = b.replaceAll("[.]", ""); 
        }      
    
      
        if ( (a.length() < 1 || b.length() < 1 ) ){
            return "0";    
        }
            
        if ( a.equals("1") && aDecPos < 1 ){
            if ( bDecPos > 0 ){
                b = b.substring(0, b.length() - bDecPos).concat(".").concat(b.substring(b.length() - bDecPos)); 
                b = b.charAt(0) == '.'? "0".concat(b): b;
                return ((isaNeg != isbNeg)? "-".concat(b) : b);
            }
            return ((isaNeg != isbNeg)? "-".concat(b) : b);
        }
          
        if ( b.equals("1") && bDecPos < 1 ){
            if ( aDecPos > 0 ){
                a = a.substring(0, a.length() - aDecPos).concat(".").concat(a.substring(a.length() - aDecPos)); 
                a = a.charAt(0) == '.'? "0".concat(a): a;
                return ((isaNeg != isbNeg)? "-".concat(a) : a);

            }
            return ((isaNeg != isbNeg)? "-".concat(a) : a);
        }
           
        if ( aDecPos > -1 || bDecPos > -1  ){
            aDecPos = (aDecPos > -1)? aDecPos : 0;
            bDecPos = (bDecPos > -1)? bDecPos : 0;        
            oDecPos = aDecPos + bDecPos;
        } 
        
        byte digit = 9;
        long temp = 0;
        int alen = a.length();
        int blen = b.length();
        int bi = blen;
        int posr = 0;
        int posw = 0;
        String output = "";
        String pad0 = "";
        String tempstr = "";
        String leftoverstr = "";
      
        while ( pad0.length() < digit - 1 ){
            pad0 = "0".concat(pad0); 
        }
      
        while ( bi > 0 ){
            int outlen = output.length();
            int ai = alen;
            long z = Long.parseLong(b.substring((bi - digit > -1? bi - digit : 0), bi));
            long leftover = 0;
        
            if ( outlen < 1 ){        
                while ( ai > 0){
                    temp = Long.parseLong(a.substring((ai - digit > -1? ai - digit: 0), ai)) * z + leftover;
                    tempstr = String.valueOf(temp);
                    leftover = tempstr.length() > digit? Long.parseLong(tempstr.substring(0,tempstr.length() - digit)) : 0 ;
                    tempstr = pad0.concat(tempstr);
                    output = tempstr.substring(tempstr.length() - digit).concat(output);
                    ai = ai - digit;

                }
                if ( leftover > 0){
                    leftoverstr = pad0.concat(String.valueOf(leftover));
                    output = leftoverstr.substring(leftoverstr.length() - digit).concat(output);
                }
            } else {
                long tempadd = 0;
                long outposval = 0;
                int remainder = 0;
                int loopidx = 0;
                String tempaddstr = "";
                String outposstr = "";
                while ( ai > 0){
                    posw = (posr + loopidx) * digit;
                    temp = Long.parseLong(a.substring((ai - digit > -1? ai - digit: 0), ai)) * z + leftover + remainder;
                    
                    tempstr = String.valueOf(temp);    
                    leftover = tempstr.length() > digit? Long.parseLong(tempstr.substring(0,tempstr.length() - digit)) : 0 ;
                    tempstr = pad0.concat(tempstr);

                    outlen = output.length();
                    outposstr = output.substring((outlen - posw - digit > 0 ? outlen - posw - digit : 0), (outlen - posw > 0 ? outlen - posw : 0));    
                    outposval = outposstr.isEmpty()? 0 : Long.parseLong(outposstr);

                    tempadd = Long.parseLong(tempstr.substring(tempstr.length() - digit )) + outposval;
                    tempaddstr = String.valueOf(tempadd);
                    remainder = tempaddstr.length() > digit? 1 : 0;
                    tempaddstr = pad0.concat(String.valueOf(tempadd));

                    output = output.substring(0, (outlen - posw - digit > 0? outlen - posw - digit : 0)).concat(tempaddstr.substring(tempaddstr.length() - digit)).concat(output.substring((outlen - posw > 0? outlen - posw : 0), outlen));

                    loopidx = loopidx + 1;
                    ai = ai - digit;               
                }
                if ( leftover + remainder > 0){
                    leftoverstr = pad0.concat(String.valueOf(leftover + remainder));
                    output = leftoverstr.substring(leftoverstr.length() - digit).concat(output);
                }
            }
            posr = posr + 1;
            bi = bi - digit;
        }   
          
        if ( oDecPos > 0 ){
            while (output.length() < oDecPos){
                output = "0".concat(output);
            }
            output = (output.length() < oDecPos)? "0".concat(output) : output;
            output = output.substring(0, output.length() - oDecPos).concat(".").concat(output.substring(output.length() - oDecPos)); 
            output = output.replaceAll("^0+", "");
            output = output.replaceAll("0+$", "");
            output = output.replaceAll("[.]$", ""); 
            output = output.charAt(0) == '.' ? "0".concat(output) : output;
            return (isaNeg != isbNeg? "-".concat(output): output);
        }      

        output = output.replaceAll("^0+", "");      
        return (isaNeg != isbNeg? "-".concat(output): output);
    }
}

Digiprove sealCopyright secured by Digiprove © 2017

This post was written by Kevin and was first post @ http://kevinhng86.iblog.website.
Original Post Name: "Working With Number – Infinity Multiplication – Chapter 3B – Java".
Original Post Link: http://kevinhng86.iblog.website/2017/03/02/working-with-number-infinity-multiplication-chapter-3b-java/.

Advertisement


Random Article You May Like

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*