forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathToolkit.java
More file actions
313 lines (287 loc) · 10.4 KB
/
MathToolkit.java
File metadata and controls
313 lines (287 loc) · 10.4 KB
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
* MathToolkit.java
* A simple single-file Java project that provides a command-line math toolkit.
* All user-facing text is in English as requested.
*
* Features:
* - Basic arithmetic (addition, subtraction, multiplication, division)
* - GCD and LCM
* - Factorial (uses BigInteger for large results)
* - Fibonacci (n-th, uses BigInteger)
* - Prime check (BigInteger.isProbablePrime)
* - Quadratic equation solver (real and complex roots)
*
* Usage:
* Compile: javac MathToolkit.java
* Run: java MathToolkit
*
* Author: ChatGPT
*/
import java.math.BigInteger;
import java.util.InputMismatchException;
import java.util.Scanner;
public class MathToolkit {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Welcome to MathToolkit — a simple Java math project.");
boolean running = true;
while (running) {
printMenu();
System.out.print("Choose an option (1-9) or 0 to exit: ");
int choice = readInt();
System.out.println();
switch (choice) {
case 0:
running = false;
System.out.println("Goodbye!");
break;
case 1:
basicArithmetic();
break;
case 2:
gcdLcm();
break;
case 3:
factorial();
break;
case 4:
fibonacci();
break;
case 5:
primeCheck();
break;
case 6:
quadraticSolver();
break;
case 7:
integerOperationsDemo();
break;
case 8:
about();
break;
case 9:
help();
break;
default:
System.out.println("Invalid option — please try again.");
}
System.out.println();
}
scanner.close();
}
private static void printMenu() {
System.out.println("=== MathToolkit Menu ===");
System.out.println("1) Basic arithmetic ( +, -, *, / )");
System.out.println("2) GCD and LCM");
System.out.println("3) Factorial (BigInteger)");
System.out.println("4) Fibonacci (n-th term, BigInteger)");
System.out.println("5) Prime check");
System.out.println("6) Quadratic equation solver");
System.out.println("7) Integer operations demo (safe long handling)");
System.out.println("8) About this project");
System.out.println("9) Help (short usage)");
System.out.println("0) Exit");
}
private static void basicArithmetic() {
System.out.println("-- Basic arithmetic --");
System.out.print("Enter first number: ");
double a = readDouble();
System.out.print("Enter second number: ");
double b = readDouble();
System.out.println("Choose operator: + - * /");
System.out.print("Operator: ");
String op = scanner.next();
double result;
switch (op) {
case "+":
result = a + b;
System.out.printf("Result: %.10g\n", result);
break;
case "-":
result = a - b;
System.out.printf("Result: %.10g\n", result);
break;
case "*":
result = a * b;
System.out.printf("Result: %.10g\n", result);
break;
case "/":
if (b == 0) {
System.out.println("Error: Division by zero is not allowed.");
} else {
result = a / b;
System.out.printf("Result: %.10g\n", result);
}
break;
default:
System.out.println("Unknown operator.");
}
}
private static void gcdLcm() {
System.out.println("-- GCD and LCM --");
System.out.print("Enter first integer (long): ");
long a = readLong();
System.out.print("Enter second integer (long): ");
long b = readLong();
long g = gcd(a, b);
long l = lcm(a, b);
System.out.println("GCD(" + a + ", " + b + ") = " + g);
System.out.println("LCM(" + a + ", " + b + ") = " + l);
}
private static void factorial() {
System.out.println("-- Factorial --");
System.out.print("Enter a non-negative integer (n): ");
int n = readNonNegativeInt();
BigInteger fact = factorialBig(n);
System.out.println(n + "! = " + fact.toString());
}
private static void fibonacci() {
System.out.println("-- Fibonacci (n-th) --");
System.out.print("Enter n (0-based index, non-negative): ");
int n = readNonNegativeInt();
BigInteger fib = fibonacciBig(n);
System.out.println("Fibonacci(" + n + ") = " + fib.toString());
}
private static void primeCheck() {
System.out.println("-- Prime check --");
System.out.print("Enter integer to check (positive): ");
long n = readLongPositive();
BigInteger big = BigInteger.valueOf(n);
boolean probablePrime = big.isProbablePrime(12); // 12 -> sufficiently small error probability
if (probablePrime) {
System.out.println(n + " is probably prime.");
} else {
System.out.println(n + " is composite (not prime).");
}
}
private static void quadraticSolver() {
System.out.println("-- Quadratic equation solver: ax^2 + bx + c = 0 --");
System.out.print("Enter a (non-zero): ");
double a = readDoubleNonZero();
System.out.print("Enter b: ");
double b = readDouble();
System.out.print("Enter c: ");
double c = readDouble();
double disc = b * b - 4 * a * c;
if (disc > 0) {
double r1 = (-b + Math.sqrt(disc)) / (2 * a);
double r2 = (-b - Math.sqrt(disc)) / (2 * a);
System.out.printf("Two real roots: x1 = %.10g, x2 = %.10g\n", r1, r2);
} else if (disc == 0) {
double r = -b / (2 * a);
System.out.printf("One real root: x = %.10g\n", r);
} else {
double real = -b / (2 * a);
double imag = Math.sqrt(-disc) / (2 * a);
System.out.printf("Two complex roots: x1 = %.10g + %.10gi, x2 = %.10g - %.10gi\n", real, imag, real, imag);
}
}
private static void integerOperationsDemo() {
System.out.println("-- Integer operations demo --");
System.out.println("This demonstrates safe handling with long and BigInteger when needed.");
System.out.print("Enter a long integer: ");
long a = readLong();
System.out.print("Enter another long integer: ");
long b = readLong();
System.out.println("Sum as long: " + (a + b));
BigInteger bigSum = BigInteger.valueOf(a).add(BigInteger.valueOf(b));
System.out.println("Sum as BigInteger: " + bigSum.toString());
}
private static void about() {
System.out.println("MathToolkit — single-file Java project. All text is English.");
System.out.println("Feel free to request additional features or a multi-file project structure.");
}
private static void help() {
System.out.println("Help: Use options from the menu, input numbers when asked.");
System.out.println("For large factorial or Fibonacci values, the program uses BigInteger.");
}
// --- Utility math functions ---
private static long gcd(long a, long b) {
a = Math.abs(a);
b = Math.abs(b);
if (a == 0) return b;
if (b == 0) return a;
while (b != 0) {
long t = a % b;
a = b;
b = t;
}
return Math.abs(a);
}
private static long lcm(long a, long b) {
a = Math.abs(a);
b = Math.abs(b);
if (a == 0 || b == 0) return 0;
return Math.abs(a / gcd(a, b) * b);
}
private static BigInteger factorialBig(int n) {
BigInteger result = BigInteger.ONE;
for (int i = 2; i <= n; i++) {
result = result.multiply(BigInteger.valueOf(i));
}
return result;
}
private static BigInteger fibonacciBig(int n) {
if (n == 0) return BigInteger.ZERO;
if (n == 1) return BigInteger.ONE;
BigInteger prev = BigInteger.ZERO, curr = BigInteger.ONE;
for (int i = 2; i <= n; i++) {
BigInteger next = prev.add(curr);
prev = curr;
curr = next;
}
return curr;
}
// --- Robust input helpers ---
private static int readInt() {
while (true) {
try {
return scanner.nextInt();
} catch (InputMismatchException ex) {
System.out.print("Invalid integer — please enter again: ");
scanner.next();
}
}
}
private static int readNonNegativeInt() {
while (true) {
int v = readInt();
if (v < 0) System.out.print("Please enter a non-negative integer: ");
else return v;
}
}
private static long readLong() {
while (true) {
try {
return scanner.nextLong();
} catch (InputMismatchException ex) {
System.out.print("Invalid long — please enter again: ");
scanner.next();
}
}
}
private static long readLongPositive() {
while (true) {
long v = readLong();
if (v <= 0) System.out.print("Please enter a positive integer: ");
else return v;
}
}
private static double readDouble() {
while (true) {
try {
return scanner.nextDouble();
} catch (InputMismatchException ex) {
System.out.print("Invalid number — please enter again: ");
scanner.next();
}
}
}
private static double readDoubleNonZero() {
while (true) {
double v = readDouble();
if (v == 0.0) System.out.print("Value cannot be zero — please enter again: ");
else return v;
}
}
}