Advertisement
IvonLiu

Digit Division

Feb 3rd, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.73 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. /**
  4.  * Created by Owner on 2/2/2016.
  5.  */
  6. public class Application {
  7.  
  8.     public static void main(String[] args) {
  9.         int[] a = new int[] {1, 0, 3, 0, 3, 0, 1};
  10.         int[] b = new int[] {1, 0, 1};
  11.         System.out.println(Arrays.toString(divide(a, b)));
  12.     }
  13.  
  14.     /****************************/
  15.     /*** Arithmetic Operators ***/
  16.     /****************************/
  17.  
  18.     /**
  19.      * Divide a by b
  20.      */
  21.     private static int[] divide(int[] a, int[] b) {
  22.         if (isLess(a, b)) {
  23.             return new int[] {0};
  24.         } else if (isEqual(a, b)) {
  25.             return new int[] {1};
  26.         } else {
  27.  
  28.             int qLength = a.length - b.length + 1;
  29.             int p = b.length - 1;   // So that x is the same length as b
  30.             int[] x = subArray(a, 0, p);
  31.             while (isGreater(b, x)) {
  32.                 p++;
  33.                 qLength--;
  34.                 x = subArray(a, 0, p);
  35.             }
  36.             int[] q = new int[qLength];
  37.  
  38.             for (int i=0; i<q.length; i++) {
  39.                 int c = 1;
  40.                 int[] y = multiply(b, c);
  41.                 while (isLess(y, x) || isEqual(y, x)) {
  42.                     c++;
  43.                     y = multiply(b, c);
  44.                 }
  45.                 q[i] = c - 1;
  46.                 x = subtract(x, multiply(b, c-1));
  47.                 if (p+i+1 < a.length) {
  48.                     x = append(x, a[p+i+1]);
  49.                 }
  50.             }
  51.  
  52.             return q;
  53.  
  54.         }
  55.     }
  56.  
  57.     /**
  58.      * Subtract b from a
  59.      */
  60.     private static int[] subtract(int[] a, int[] b) {
  61.         if (isLess(a, b)) {
  62.             return subtract(b, a);
  63.         }
  64.         int[] d = new int[a.length];
  65.         for (int i=0; i<d.length; i++) {
  66.             int ia = a.length - i - 1;
  67.             int ib = b.length - i - 1;
  68.             int id = d.length - i - 1;
  69.             if (ib >= 0) {
  70.                 d[id] = a[ia] - b[ib];
  71.             } else {
  72.                 d[id] = a[ia];
  73.             }
  74.         }
  75.         return beautify(d, 10);
  76.     }
  77.  
  78.     /**
  79.      * Multiply a by k
  80.      */
  81.     private static int[] multiply(int[] a, int k) {
  82.         int[] p = new int[a.length];
  83.         for (int i=0; i<p.length; i++) {
  84.             p[i] = a[i]*k;
  85.         }
  86.         return beautify(p, 10);
  87.     }
  88.  
  89.     /****************************/
  90.     /*** Relational Operators ***/
  91.     /****************************/
  92.  
  93.     /**
  94.      * Tells you if a is equal to b
  95.      */
  96.     private static boolean isEqual(int[] a, int[] b) {
  97.         if (a.length != b.length) {
  98.             return false;
  99.         } else {
  100.             for (int i=0; i<a.length; i++) {
  101.                 if (a[i] != b[i]) {
  102.                     return false;
  103.                 }
  104.             }
  105.             return true;
  106.         }
  107.     }
  108.  
  109.     /**
  110.      * Tells you if a is less than b
  111.      */
  112.     private static boolean isLess(int[] a, int[] b) {
  113.         if (a.length < b.length) {
  114.             return true;
  115.         } else if (a.length > b.length) {
  116.             return false;
  117.         } else {
  118.             for (int i=0; i<a.length; i++) {
  119.                 if (a[i] < b[i]) {
  120.                     return true;
  121.                 } else if (a[i] > b[i]) {
  122.                     return false;
  123.                 }
  124.             }
  125.             return false;
  126.         }
  127.     }
  128.  
  129.     /**
  130.      * Tells you if a is greater than b
  131.      */
  132.     private static boolean isGreater(int[] a, int[] b) {
  133.         if (a.length > b.length) {
  134.             return true;
  135.         } else if (a.length < b.length) {
  136.             return false;
  137.         } else {
  138.             for (int i=0; i<a.length; i++) {
  139.                 if (a[i] > b[i]) {
  140.                     return true;
  141.                 } else if (a[i] < b[i]) {
  142.                     return false;
  143.                 }
  144.             }
  145.             return false;
  146.         }
  147.     }
  148.  
  149.     /******************/
  150.     /*** Formatting ***/
  151.     /******************/
  152.  
  153.     /**
  154.      * Ensure that all digits of a are within
  155.      * the range [0, b). Remove all leading zeros.
  156.      *
  157.      * @param b
  158.      *          The base of the number a
  159.      */
  160.     private static int[] beautify(int[] a, int b) {
  161.         a = propagateDigits(a, b);
  162.         return removeLeadingZeros(a);
  163.     }
  164.  
  165.     /**
  166.      * Ensure that all digits of a
  167.      * are within the range [0, b)
  168.      *
  169.      * @param b
  170.      *          The base of the number a
  171.      */
  172.     private static int[] propagateDigits(int[] a, int b) {
  173.         int[] c = copy(a);
  174.         for (int i=c.length-1; i>=0; i--) {
  175.             if (c[i] >= b) {
  176.                 int overflow = c[i] / b;
  177.                 if (i > 0) {
  178.                     c[i-1] += overflow;
  179.                 } else {
  180.                     c = prepend(c, overflow);
  181.                     i++;
  182.                 }
  183.                 c[i] = c[i] % b;
  184.             } else if (c[i] < 0) {
  185.                 if (i > 0) {
  186.                     int borrow = -((int) Math.floor(((float) c[i]) / b));
  187.                     c[i-1] -= borrow;
  188.                     c[i] += (borrow * b);
  189.                 }
  190.             }
  191.         }
  192.         return c;
  193.     }
  194.  
  195.     /**
  196.      * Remove all leading zeros of a
  197.      */
  198.     private static int[] removeLeadingZeros(int[] a) {
  199.         int actualLength = 0;
  200.         for (int i=0; i<a.length; i++) {
  201.             if (a[i] != 0) {
  202.                 actualLength = a.length - i;
  203.                 break;
  204.             }
  205.         }
  206.         int[] r = new int[actualLength];
  207.         for (int i=0; i<r.length; i++) {
  208.             r[r.length-i-1] = a[a.length-i-1];
  209.         }
  210.         return r;
  211.     }
  212.  
  213.     /***********************/
  214.     /*** Array Utilities ***/
  215.     /***********************/
  216.  
  217.     /**
  218.      * Creates a copy of an array
  219.      */
  220.     private static int[] copy(int[] a) {
  221.         int[] b = new int[a.length];
  222.         for (int i=0; i<a.length; i++) {
  223.             b[i] = a[i];
  224.         }
  225.         return b;
  226.     }
  227.  
  228.     /**
  229.      * Add b to the beginning of a
  230.      */
  231.     private static int[] prepend(int[] a, int b) {
  232.         int[] r = new int[a.length+1];
  233.         r[0] = b;
  234.         for (int i=0; i<a.length; i++) {
  235.             r[i+1] = a[i];
  236.         }
  237.         return r;
  238.     }
  239.  
  240.     /**
  241.      * Add b to the end of a
  242.      */
  243.     private static int[] append(int[] a, int b) {
  244.         int[] r = new int[a.length+1];
  245.         for (int i=0; i<a.length; i++) {
  246.             r[i] = a[i];
  247.         }
  248.         r[r.length-1] = b;
  249.         return r;
  250.     }
  251.  
  252.     /**
  253.      * Return sub-array of a from a[start] to a[end], inclusive
  254.      */
  255.     private static int[] subArray(int[] a, int start, int end) {
  256.         int[] s = new int[end - start + 1];
  257.         for (int i=0; i<s.length; i++) {
  258.             s[i] = a[start+i];
  259.         }
  260.         return s;
  261.     }
  262.  
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement