initial commit
This commit is contained in:
commit
ded099dbac
Binary file not shown.
|
@ -0,0 +1,144 @@
|
|||
import java.util.Objects;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
byte a = 1, b = 1, n = 10, m = 10;
|
||||
boolean simplify_math = true;
|
||||
int iteration_amount = 1;
|
||||
byte verbosity = 1;
|
||||
int p = 0;
|
||||
boolean run_safe = false;
|
||||
|
||||
while (p < args.length) {
|
||||
try {
|
||||
if (Objects.equals(args[p], "-h")) {
|
||||
System.out.print("Usage: java Main [options]\nOptions:\n -v <byte> Sets verbosity level (0-2, default is 1\n -a, -b, -n, -m <byte> Sets default values for a, b, n and m variables respectively\n -s <bool> Use simplified algorithm to calculate the desired value (true/false, default is true)\n -t <int> Enables benchmarking mode and performs selected calculation <int> times\n");
|
||||
System.exit(0);
|
||||
}
|
||||
else if (Objects.equals(args[p], "-v")) {
|
||||
verbosity = (byte) Integer.parseInt(args[p+1]);
|
||||
p += 2;
|
||||
}
|
||||
else if (Objects.equals(args[p], "-a")) {
|
||||
a = (byte) Integer.parseInt(args[p+1]);
|
||||
p += 2;
|
||||
}
|
||||
else if (Objects.equals(args[p], "-b")) {
|
||||
b = (byte) Integer.parseInt(args[p+1]);
|
||||
p += 2;
|
||||
}
|
||||
else if (Objects.equals(args[p], "-n")) {
|
||||
n = (byte) Integer.parseInt(args[p+1]);
|
||||
p += 2;
|
||||
}
|
||||
else if (Objects.equals(args[p], "-m")) {
|
||||
m = (byte) Integer.parseInt(args[p+1]);
|
||||
p += 2;
|
||||
}
|
||||
else if (Objects.equals(args[p], "-t")) {
|
||||
iteration_amount = Integer.parseInt(args[p+1]);
|
||||
p += 2;
|
||||
}
|
||||
else if (Objects.equals(args[p], "-s")) {
|
||||
simplify_math = Boolean.parseBoolean(args[p+1]);
|
||||
p += 2;
|
||||
}
|
||||
else if (Objects.equals(args[p], "--safe")) {
|
||||
run_safe = true;
|
||||
p++;
|
||||
}
|
||||
else {
|
||||
if (verbosity >= 1) {
|
||||
System.err.println("Unknown argument found: " + args[p] + "\nPlease, use 'java Main -h' for help");
|
||||
}
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
System.err.println("Exception while parsing CLI arguments: " + e);
|
||||
System.err.println("Aborting execution.");
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
if (verbosity >= 2) {
|
||||
System.out.println(String.format("Using arguments: a=%d, b=%d, n=%d, m=%d", a, b, n, m));
|
||||
}
|
||||
|
||||
if (simplify_math == true) {
|
||||
if (b <= 0 && m >= 0) {
|
||||
System.err.println("Preventing division by zero!\nAborting calculations.");
|
||||
System.exit(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (a <= 0 && n >= 0) {
|
||||
System.err.println("Bypassing division by zero with fast math; expect wrong calculations");
|
||||
if (run_safe) {
|
||||
System.err.println("--safe flag is set, preventing further execution");
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
for (int g = 0; g < iteration_amount; g++) {
|
||||
double result = calc_quick(verbosity, a, n, b, m);
|
||||
if (iteration_amount == 1) {
|
||||
if (verbosity >= 1) {
|
||||
System.out.println(String.format("Result is %.3f", result));
|
||||
}
|
||||
else
|
||||
{
|
||||
System.out.print(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if ((a <= 0 && n >= 0) || (b <= 0 && m >= 0)) {
|
||||
System.err.println("Preventing division by zero!\nAborting calculations.");
|
||||
System.exit(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int g = 0; g < iteration_amount; g++) {
|
||||
double result = calc_generic(verbosity, a, n, b, m);
|
||||
if (iteration_amount == 1) {
|
||||
if (verbosity >= 1) {
|
||||
System.out.println(String.format("Result is %.3f", result));
|
||||
}
|
||||
else
|
||||
{
|
||||
System.out.print(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static double calc_generic(byte verbosity, byte a, byte n, byte b, byte m) {
|
||||
double result = 0;
|
||||
for (byte i = a; i <= n; i++) {
|
||||
for (byte j = b; j <= m; j++) {
|
||||
if (verbosity >= 3) {
|
||||
System.out.print(result);
|
||||
}
|
||||
|
||||
result += ((double) i / (double) j) / ((double) i - (double) 0);
|
||||
|
||||
if (verbosity >= 3) {
|
||||
System.out.println(" + " + (((double)i / (double)j) / ((double)i-(double)0)) + " = " + result);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static double calc_quick(byte verbosity, byte a, byte n, byte b, byte m) {
|
||||
double t = 0;
|
||||
for (byte j = b; j <= m; j++) {if (verbosity >= 3) {System.out.print(t);}; t += (double)1/(double)j; if (verbosity >= 3) {System.out.println(" + " + (double)1/(double)j + " = " + t);} }
|
||||
return (double) (t * ((double)n-(double)a + (double) 1));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
#!/bin/sh
|
||||
|
||||
time -f 'Non-optimal: %U %S' java Main -v 1 -t 10000000 -s false
|
||||
time -f ' Optimal: %U %S' java Main -v 1 -t 10000000 -s true
|
|
@ -0,0 +1,9 @@
|
|||
#!/bin/python3
|
||||
|
||||
import os
|
||||
|
||||
for i in range(-5, 11):
|
||||
print(f"Current test: {i}")
|
||||
os.system(f"java Main -s false -a {i}")
|
||||
os.system(f"java Main -s true -a {i}")
|
||||
print()
|
Loading…
Reference in New Issue