forked from phishman3579/java-algorithms-implementation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Integers.java
215 lines (199 loc) · 7.39 KB
/
Integers.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package com.jwetherell.algorithms.numbers;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
public class Integers {
private static final BigDecimal ZERO = new BigDecimal(0);
private static final BigDecimal TWO = new BigDecimal(2);
public static final String toBinaryUsingDivideAndModulus(int numberToConvert) {
int integer = numberToConvert;
if (integer<0) throw new IllegalArgumentException("Method argument cannot be negative. number="+integer);
StringBuilder builder = new StringBuilder();
int temp = 0;
while (integer > 0) {
temp = integer;
integer = temp / 2;
builder.append(temp % 2);
}
return builder.reverse().toString();
}
public static final String toBinaryUsingShiftsAndModulus(int numberToConvert) {
int integer = numberToConvert;
if (integer<0) throw new IllegalArgumentException("Method argument cannot be negative. number="+integer);
StringBuilder builder = new StringBuilder();
int temp = 0;
while (integer > 0) {
temp = integer;
integer = (temp >> 1);
builder.append(temp % 2);
}
return builder.reverse().toString();
}
public static final String toBinaryUsingBigDecimal(int numberToConvert) {
int integer = numberToConvert;
if (integer<0) throw new IllegalArgumentException("Method argument cannot be negative. number="+integer);
StringBuilder builder = new StringBuilder();
BigDecimal number = new BigDecimal(integer);
BigDecimal[] decimals = null;
while (number.compareTo(ZERO) > 0) {
decimals = number.divideAndRemainder(TWO);
number = decimals[0];
builder.append(decimals[1]);
}
return builder.reverse().toString();
}
public static final String toBinaryUsingDivideAndDouble(int numberToConvert) {
int integer = numberToConvert;
if (integer<0) throw new IllegalArgumentException("Method argument cannot be negative. number="+integer);
StringBuilder builder = new StringBuilder();
double temp = 0d;
while (integer > 0) {
temp = integer/2d;
integer = (int) temp;
builder.append((temp > integer) ? 1 : 0);
}
return builder.reverse().toString();
}
public static final boolean powerOfTwoUsingLoop(int numberToCheck) {
int number = numberToCheck;
if (number == 0)
return false;
while (number % 2 == 0) {
number /= 2;
}
if (number > 1)
return false;
return true;
}
public static final boolean powerOfTwoUsingRecursion(int numberToCheck) {
int number = numberToCheck;
if (number == 1)
return true;
if (number == 0 || number % 2 != 0)
return false;
return powerOfTwoUsingRecursion(number / 2);
}
public static final boolean powerOfTwoUsingLog(int numberToCheck) {
int number = numberToCheck;
double doubleLog = Math.log10(number) / Math.log10(2);
int intLog = (int) doubleLog;
if (Double.compare(doubleLog, intLog) == 0)
return true;
return false;
}
public static final boolean powerOfTwoUsingBits(int numberToCheck) {
int number = numberToCheck;
if (number != 0 && ((number & (number - 1)) == 0))
return true;
return false;
}
// Integer to English
private static final Map<Integer,String> singleDigits = new HashMap<Integer,String>();
static {
singleDigits.put(0,"zero");
singleDigits.put(1,"one");
singleDigits.put(2,"two");
singleDigits.put(3,"three");
singleDigits.put(4,"four");
singleDigits.put(5,"five");
singleDigits.put(6,"six");
singleDigits.put(7,"seven");
singleDigits.put(8,"eight");
singleDigits.put(9,"nine");
singleDigits.put(10,"ten");
singleDigits.put(11,"eleven");
singleDigits.put(12,"twelve");
singleDigits.put(13,"thirteen");
singleDigits.put(14,"fourteen");
singleDigits.put(15,"fifteen");
singleDigits.put(16,"sixteen");
singleDigits.put(17,"seventeen");
singleDigits.put(18,"eighteen");
singleDigits.put(19,"nineteen");
}
private static final Map<Integer,String> multiDigits = new HashMap<Integer,String>();
static {
multiDigits.put(10,"ten");
multiDigits.put(20,"twenty");
multiDigits.put(30,"thirty");
multiDigits.put(40,"forty");
multiDigits.put(50,"fifty");
multiDigits.put(60,"sixty");
multiDigits.put(70,"seventy");
multiDigits.put(80,"eighty");
multiDigits.put(90,"ninety");
}
private static final int BILLION = 1000000000;
private static final int MILLION = 1000000;
private static final int THOUSAND = 1000;
private static final int HUNDRED = 100;
private static final int TEN = 10;
private static final String handleUnderOneThousand(int number) {
StringBuilder builder = new StringBuilder();
int x = number;
int m = x / HUNDRED;
int r = x % HUNDRED;
if (m > 0) {
builder.append(singleDigits.get(m)).append("-hundred");
x = x % HUNDRED;
}
if (r > 0) {
if (m > 0) builder.append(" ");
if (x <= 19) {
builder.append(singleDigits.get(x));
} else {
m = x / TEN;
r = x % TEN;
if (r == 0) {
builder.append(multiDigits.get(x));
} else {
x = x - r;
builder.append(multiDigits.get(x)).append("-");
builder.append(singleDigits.get(r));
}
}
}
return builder.toString();
}
public static final String toEnglish(int number) {
int x = number;
if (x>Integer.MAX_VALUE || x<=Integer.MIN_VALUE) throw new IllegalArgumentException("Number has to be <= Integer.MAX_VALUE and > Integer.MIN_VALUE. number="+x);
StringBuilder builder = new StringBuilder();
if (x==0) {
//Zero is a special case
builder.append(singleDigits.get(x));
return builder.toString();
}
boolean billion = false;
boolean million = false;
boolean thousand = false;
if (x<0) {
builder.append("negative ");
// Make the number positive
x = x * -1;
}
int m = x / BILLION;
if (m > 0) {
billion = true;
builder.append(handleUnderOneThousand(m)).append("-billion");
x = x % BILLION;
}
m = x / MILLION;
if (m > 0) {
if (billion) builder.append(" ");
million = true;
builder.append(handleUnderOneThousand(m)).append("-million");
x = x % MILLION;
}
m = x / THOUSAND;
if (m > 0) {
if (billion || million) builder.append(" ");
thousand = true;
builder.append(handleUnderOneThousand(m)).append("-thousand");
x = x % THOUSAND;
}
if (billion || million || thousand && x!=0) builder.append(" ");
builder.append(handleUnderOneThousand(x));
return builder.toString();
}
}