|
| 1 | +package hw04; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.FileNotFoundException; |
| 5 | +import java.io.FileReader; |
| 6 | +import java.io.IOException; |
| 7 | +import java.math.BigInteger; |
| 8 | +import java.util.StringTokenizer; |
| 9 | + |
| 10 | +public class Karatsuba { |
| 11 | + |
| 12 | + private int[] arr; |
| 13 | + |
| 14 | + public Karatsuba(String filePath){ |
| 15 | + String data = this.readFile(filePath); |
| 16 | + StringTokenizer str = new StringTokenizer(data, " |\n"); |
| 17 | + arr = new int[str.countTokens()]; |
| 18 | + for(int i = 0; str.hasMoreTokens(); i++){ |
| 19 | + arr[i] = Integer.parseInt(str.nextToken()); |
| 20 | + } |
| 21 | + |
| 22 | + } |
| 23 | + |
| 24 | + private int[] getArr(){ |
| 25 | + return this.arr; |
| 26 | + } |
| 27 | + |
| 28 | + public BigInteger doKaratsuba(){ |
| 29 | + return karatsuba(getArr()[0], getArr()[1]); |
| 30 | + } |
| 31 | + |
| 32 | +// private int karatsuba(int a, int b){ |
| 33 | +// int aCount = calculateIntNumber(a); |
| 34 | +// int bCount = calculateIntNumber(b); |
| 35 | +// int maxValueCount = max(aCount, bCount); |
| 36 | +// if(maxValueCount == 1){ |
| 37 | +// return a * b; |
| 38 | +// } |
| 39 | +// int exponent = maxValueCount / 2; |
| 40 | +// int divisor = getDivisor(exponent); |
| 41 | +// int x1 = a / divisor; |
| 42 | +// int x2 = a - (x1 * divisor); |
| 43 | +// int y1 = b / divisor; |
| 44 | +// int y2 = b - (y1 * divisor); |
| 45 | +// int z0 = karatsuba(x2, y2); |
| 46 | +// int z2 = karatsuba(x1, y1); |
| 47 | +// int z1 = karatsuba((x2 + x1), (y2 + y1)) - z2 - z0; |
| 48 | +// |
| 49 | +// return (z2 * divisor * divisor) + z1 * divisor + z0; |
| 50 | +// } |
| 51 | + |
| 52 | + private BigInteger karatsuba(int a, int b){ |
| 53 | + int aCount = calculateIntNumber(a); |
| 54 | + int bCount = calculateIntNumber(b); |
| 55 | + int maxValueCount = max(aCount, bCount); |
| 56 | + if(maxValueCount == 1){ |
| 57 | + return BigInteger.valueOf(a * b); |
| 58 | + } |
| 59 | + int exponent = maxValueCount / 2; |
| 60 | + int divisor = getDivisor(exponent); |
| 61 | + int x1 = a / divisor; |
| 62 | + int x2 = a - (x1 * divisor); |
| 63 | + int y1 = b / divisor; |
| 64 | + int y2 = b - (y1 * divisor); |
| 65 | + BigInteger z0 = karatsuba(x2, y2); |
| 66 | + BigInteger z2 = karatsuba(x1, y1); |
| 67 | + BigInteger z1 = karatsuba((x2 + x1), (y2 + y1)).subtract(z2).subtract(z0); |
| 68 | + |
| 69 | + BigInteger bigDivisor = BigInteger.valueOf(divisor); |
| 70 | + return z2.multiply(bigDivisor.pow(2)).add(z1.multiply(bigDivisor)).add(z0); |
| 71 | + } |
| 72 | + |
| 73 | + private int getDivisor(int exponent){ |
| 74 | + int number = 1; |
| 75 | + while(exponent > 0){ |
| 76 | + number *= 10; |
| 77 | + exponent--; |
| 78 | + } |
| 79 | + return number; |
| 80 | + } |
| 81 | + |
| 82 | + private int max(int a, int b){ |
| 83 | + return (a >= b) ? a : b; |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + public int calculateIntNumber(int number){ |
| 88 | + int count = 0; |
| 89 | + while(number > 0){ |
| 90 | + number /= 10; |
| 91 | + count++; |
| 92 | + } |
| 93 | + return count; |
| 94 | + } |
| 95 | + |
| 96 | + private String readFile(String filePath) { |
| 97 | + BufferedReader br = null; |
| 98 | + StringTokenizer str = null; |
| 99 | + StringBuilder sb = null; |
| 100 | + String line; |
| 101 | + |
| 102 | + try { |
| 103 | + br = new BufferedReader(new FileReader(filePath)); |
| 104 | + sb = new StringBuilder(); |
| 105 | + while ((line = br.readLine()) != null) { |
| 106 | + sb.append(line+"\n"); |
| 107 | + } |
| 108 | + } catch (FileNotFoundException e) { |
| 109 | + e.printStackTrace(); |
| 110 | + } catch (IOException e) { |
| 111 | + e.printStackTrace(); |
| 112 | + } finally { |
| 113 | + if (br != null) { |
| 114 | + try { |
| 115 | + br.close(); |
| 116 | + } catch (IOException e) { |
| 117 | + e.printStackTrace(); |
| 118 | + } |
| 119 | + } |
| 120 | + return sb.toString(); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments