Skip to content

Instantly share code, notes, and snippets.

@codebucketdev
Created September 25, 2014 19:45
Show Gist options
  • Save codebucketdev/6e9eb7827189adb488e0 to your computer and use it in GitHub Desktop.
Save codebucketdev/6e9eb7827189adb488e0 to your computer and use it in GitHub Desktop.
With this class you can calculate the square roots of a number/value :)
package de.codebucket.math;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicBoolean;
public class Shifting
{
private double value;
public Shifting(double value)
{
this.value = value;
}
public double getValue()
{
return value;
}
private static final Random random = new Random(System.currentTimeMillis());
public List<Result> calculate()
{
double x = (random.nextInt(12) + 1), y = (this.value / x), square = (x * y);
List<Result> results = new ArrayList<Shifting.Result>();
Result lastResult = new Result(x, y);
results.add(lastResult);
while(!lastResult.isSimilar())
{
double newY = ((lastResult.getX() + lastResult.getY()) / 2);
double newX = (double) (square / newY);
if(lastResult.getX() == newX && lastResult.getY() == newY)
{
lastResult = new Result(0, 0);
break;
}
Result result = new Result(newX, newY);
lastResult = result;
results.add(result);
System.gc();
}
return results;
}
protected static class Result
{
double x, y;
public Result(double x, double y)
{
this.x = x;
this.y = y;
}
public double getX()
{
return x;
}
public double getY()
{
return y;
}
public boolean isSimilar()
{
return (x == y);
}
@Override
public String toString()
{
return "Result(x: " + x + ", y: " + y + ")";
}
}
private static int exit = -1;
public static void main(String[] args)
{
System.out.println("Das Heronverfahren, nachprogrammiert in Java.");
System.out.println("Version 1.1, kompiliert am 25.09.2014 um 19:23.");
System.out.println("(C) Copyright Codebucket 2014\n");
Runtime.getRuntime().addShutdownHook(new Thread()
{
public void run()
{
if(exit == -1)
{
System.err.print("\n\nDer Berechnungsprozess wurde vom Benutzer abgebrochen.");
return;
}
}
});
String square = (args.length == 1 ? args[0] : null);
if(args.length == 0)
{
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Geben Sie eine Zahl ein: ");
try
{
square = console.readLine();
}
catch (IOException e) {}
}
final AtomicValue value = new AtomicValue(0);
try
{
value.set(Double.parseDouble(square));
}
catch(NumberFormatException ex)
{
System.err.println("'" + square + "' ist keine Zahl, kein Ergebnis!");
Shifting.exit = 1;
return;
}
System.out.print("Berechne Naeherungswert aus Wurzel " + value + "..");
final AtomicBoolean ready = new AtomicBoolean(false);
new Thread(new Runnable()
{
@Override
public void run()
{
while(!ready.get())
{
System.out.print(".");
try
{
Thread.sleep(1000);
}
catch (InterruptedException e) {}
}
}
}).start();
final Thread math = new Thread(new Runnable()
{
final long start = System.currentTimeMillis();
@Override
public void run()
{
Shifting shift = new Shifting(value.get());
List<Result> results = shift.calculate();
ready.set(true);
System.out.println("\n\n<" + seperator() + ">");
for(int i = 0; i < results.size(); i++)
{
Result result = results.get(i);
System.out.println("Ergebnis #" + i);
System.out.println("Zahl X: " + result.getX());
System.out.println("Zahl Y: " + result.getY());
System.out.println("\n<" + seperator() + ">");
}
Result end = results.get(results.size() - 1);
System.out.println("\nNaeherungswert: " + end.getY() + ", Ergebnis: " + Math.sqrt(value.get()));
System.out.println("Berechnet in " + (System.currentTimeMillis() - start) + "ms");
Shifting.exit = 0;
}
}, "math");
math.start();
return;
}
private static class AtomicValue
{
private double value;
public AtomicValue(double value)
{
this.value = value;
}
public double get()
{
return value;
}
public void set(double value)
{
this.value = value;
}
@Override
public String toString()
{
return String.valueOf(value);
}
}
private static String seperator()
{
StringBuilder builder = new StringBuilder(28);
for(int i = 0; i < 28; i++)
{
builder.append("=");
}
return builder.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment