-
Notifications
You must be signed in to change notification settings - Fork 0
/
JMaths.java
298 lines (257 loc) · 10 KB
/
JMaths.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
* Copyright (C) 2016 Pi Developers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author Pi-Devs
*/
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.util.Scanner;
public class JMaths {
private static final String ALGO = "AES";
private static final byte[] keyValue =
new byte[] { 'T', 'h', 'e', 'B', 'e', 's', 't',
'S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' };
private static ScannerHelper sScannerHelper = new ScannerHelper(" : ");
static int factorial(int n){
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
public static void main(String[] args) {
String JMATHS = " ____ ___ __ __ \n / / |/ /___ _/ /_/ /_ _____\n __ / / /|_/ / __ `/ __/ __ \\\\/ ___/\n/ /_/ / / / / /_/ / /_/ / / (__ ) \n\\\\____/_/ /_/\\\\__,_/\\\\__/_/ /_/____/ ";
System.out.println(JMATHS);
System.out.println(" By Pi-Dev");
System.out.println("=======================================");
System.out.println("Would you like to do ?");
int cursor = sScannerHelper.askAsInt(" 1) Generate Fibonacci Series :\n" +
" 2) Check Even or odd : \n" +
" 3) Check prime no : \n" +
" 4) Encrypt text : \n" +
" 5) Decrypt text : \n" +
" 6) Check Divisibility : \n" +
" 7) Compute Pascal Triangle : \n" +
" 8) Logarithms Calculator :\n"+
" 9) Compute Factorial : \n" +
" 10) Solve a quadratic equation : \n"+
" 11) Check Golden Ration Ration : \n"+
" 99) For Exit ");
if (cursor == 1) {
fibonacci();
}else if (cursor == 2) {
checkEvenOdd();
}else if (cursor == 3) {
checkPrimeNo();
}else if (cursor == 4) {
encrypt();
}else if (cursor == 5) {
decrypt();
}else if (cursor == 99) {
System.exit(0);
}else if (cursor == 7) {
int row = sScannerHelper.askAsInt(" How many rows of pascal's triangle to generate ? ");
genPasCal(row);
}else if (cursor == 6) {
int first_no = sScannerHelper.askAsInt(" Enter Desired Number Here");
int second_no = sScannerHelper.askAsInt(" Enter number to be divided with");
if (first_no %second_no != 0)
System.out.println(("Your input is not divisible by " + second_no));
else
System.out.println("Your input is divisible by " + second_no);
}else if (cursor == 8) {
int type = sScannerHelper.askAsInt("Choose an option:\\n\\t1-Base n\\n\\t2-base 10\\n\\t3-natural logarithm");
if (type == 1) {
int nu = sScannerHelper.askAsInt(" Enter a number");
int base = sScannerHelper.askAsInt(" Enter base");
System.out.println(logOfBase(base,nu));
}else if (type == 3) {
long nu = sScannerHelper.askAsLong(" Enter a number");
System.out.println(Math.log(nu));
}else if (type == 2){
long nu = sScannerHelper.askAsLong(" Enter a number");
System.out.println(Math.log10(nu));
}
}else if (cursor == 9) {
int nu = sScannerHelper.askAsInt(" Enter a number");
System.out.println(factorial(nu));
}else if (cursor==10) {
System.out.println("Enter a,b,c co-efficient");
int a = sScannerHelper.askAsInt(">");
int b = sScannerHelper.askAsInt(">");
int c = sScannerHelper.askAsInt(">");
double temp1 = Math.sqrt(b * b - 4 * a * c);
double root1 = (-b + temp1) / (2*a) ;
double root2 = (-b - temp1) / (2*a) ;
System.out.println("The roots of the Quadratic Equation are "+root1+" and "+root2);
}else if (cursor== 11) {
int n = sScannerHelper.askAsInt(" Enter first number");
int m = sScannerHelper.askAsInt(" Enter second number");
if (m > n ) {
System.out.println("n must be greater than m");
}else {
float Q = ((float)n)/m;
if ( Q > 1.68 && Q < 1.69) {
//print "\n Golden Ratio approximately exists between these 2 digits"
System.out.println(" Golden Ratio approximately exists between these 2 digits");
}else {
System.out.println(" Golden Ratio doesn't exist between these 2 digits");
}
}
}else {
System.out.println("Wrong Input");
}
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
System.gc();
}
}));
}
public static double logOfBase(int base, int num) {
return Math.log(num) / Math.log(base);
}
private static void decrypt() {
String text = sScannerHelper.askAsString("Enter your text to decrypt ");
try {
System.out.println(decrypt(text));
} catch (Exception e) {
e.printStackTrace();
}
}
private static void genPasCal(int rows) {
for(int i =0;i<rows;i++) {
int number = 1;
System.out.format("%" + (rows - i) * 2 + "s", "");
for (int j = 0; j <= i; j++) {
System.out.format("%4d", number);
number = number * (i - j) / (j + 1);
}
System.out.println();
}
}
private static void encrypt() {
String text = sScannerHelper.askAsString("Enter your text to encrypt ");
try {
System.out.println(encrypt(text));
} catch (Exception e) {
e.printStackTrace();
}
}
public static String encrypt(String Data) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encVal = c.doFinal(Data.getBytes());
return new BASE64Encoder().encode(encVal);
}
public static String decrypt(String encryptedData) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.DECRYPT_MODE, key);
byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
byte[] decValue = c.doFinal(decordedValue);
return new String(decValue);
}
private static Key generateKey() throws Exception {
return new SecretKeySpec(keyValue, ALGO);
}
private static void checkPrimeNo() {
int no = sScannerHelper.askAsInt("Enter your no");
int i, m, flag = 0;
m = no / 2;
for (i = 2; i <= m; i++) {
if (no % i == 0) {
System.out.println("Number is not prime");
flag = 1;
break;
}
}
if (flag == 0)
System.out.println("Number is prime");
}
private static void checkEvenOdd() {
int no = sScannerHelper.askAsInt("Enter your no");
boolean even = no%2==0;
if (even) {
System.out.println(" No. '"+no+"' is even");
}
else {
System.out.println(" No. '"+no+"' is odd");
}
}
private static void fibonacci() {
int no = sScannerHelper.askAsInt(" How many terms to generate");
StringBuilder stringBuilder = new StringBuilder();
String result = "";
for (int i = 0 ; i < no ; i++) {
stringBuilder.append(String.valueOf(fibonacci(i))).append(",");
}
if (stringBuilder.toString().endsWith(",")) {
result = stringBuilder.toString().substring(0,stringBuilder.length()-1);
}
System.out.println("The no.s are " +result);
}
private static int fibonacci(int no) {
if (no == 0 || no == 1) {
return no;
}else {
return fibonacci(no - 1) + fibonacci(no - 2);
}
}
private static class ScannerHelper {
private Scanner mScanner;
private String mSeparator;
public ScannerHelper(String Separator) {
this.mSeparator = Separator;
}
private void rebuildScanner() {
mScanner = new Scanner(System.in);
}
public long askAsLong(String questions) {
long result = 0;
try {
String q = askAsString(questions);
result = Integer.parseInt(q);
}catch (Exception e) {
System.out.println("Your input is not Long . ");
System.exit(0);
askAsInt(questions);
}
return result;
}
public int askAsInt(String questions) {
int result = 0;
try {
String q = askAsString(questions);
result = Integer.parseInt(q);
}catch (Exception e) {
System.out.println("Your input is not Integer.");
System.exit(0);
askAsInt(questions);
}
return result;
}
public String askAsString(String questions) {
System.out.println(questions + mSeparator + " ");
rebuildScanner();
String object = mScanner.next();
mScanner = null;
return object;
}
}
}