Last active
August 29, 2015 13:58
-
-
Save NobbZ/10323707 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.FileReader; | |
import java.io.IOException; | |
import java.io.BufferedReader; | |
import java.util.concurrent.Callable; | |
/** | |
* Solves Problem 8 | |
* | |
* Find the greatest product of five consecutive digits in the 1000-digit number. | |
* | |
* 7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450 | |
* | |
* @author NobbZ | |
*/ | |
public class Problem8 implements Callable<Integer> { | |
/** | |
* Starte den {@see Timer} und zeige anschliessend Ergebnis und Zeit benötigte Zeit. | |
*/ | |
public static void main (String [] args) throws IOException, Exception { | |
Timer timer = Timer.time(new Problem8()); | |
System.out.println ("\nDie Loesung ist " + timer.getResult() + "."); | |
System.out.println ("\nDas Ergebnis hat " + timer.getRunTime() + " ms gebraucht"); | |
} | |
/** | |
* Die eigentliche Berechnung | |
*/ | |
public Integer call() throws Exception { | |
FileReader inputReader = new FileReader("Problem8.txt"); | |
BufferedReader inputTxt = new BufferedReader(inputReader); | |
String numbers = inputTxt.readLine(); | |
Integer result = 0; | |
for (Integer counter = 0; counter <= 994; counter++) { | |
Integer temp = 1; | |
for (Integer tempCounter = 0; tempCounter <= 4; tempCounter++) | |
temp *= ((int)numbers.charAt (tempCounter + counter)) - 48; | |
if (temp > result) | |
result = temp; | |
} | |
return result; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.concurrent.Callable; | |
/** | |
* Misst die Zeit zwischen zwei Punkten im Programmablauf. | |
* | |
* @author NobbZ | |
*/ | |
public class Timer { | |
/** | |
* Wann wurde der Timer gestartet? | |
*/ | |
private long startTime; | |
/** | |
* Läuft gerade ein Timer? | |
*/ | |
private boolean isRunning; | |
/** | |
* Speicher das Ergebnis einer Berechnung deren Dauer gestoppt wurde. | |
*/ | |
private Integer result; | |
/** | |
* Die zuletzt auf diesem Timer gestoppte Laufzeit. | |
*/ | |
private double runTime; | |
/** | |
* Vordefiniert ein paar wichtige Werte. | |
*/ | |
public Timer() { | |
startTime = 0; | |
isRunning = false; | |
} | |
/** | |
* Ist in der Lage die Laufzeit eines `{@code Callable<Integer>}` zu bestimmen. | |
* | |
* Die Zeit die die Funktion benötigte erfährt man mit `{@code Timer.getRunTime()}` | |
* und das Ergebnis mit `{@code Timer.getResult()}`. | |
* | |
* @param func Das Objekt mit der zu stoppenden Funktion. | |
* @return Ein `{@code Timer}`-Objekt aus dem die entsprechenden Werte ausgelesen werden können. | |
* @throws Exception Falls in `{@code func}` eine Exception geworfen wird, wird diese durchgereicht. | |
*/ | |
public static Timer time (Callable<Integer> func) throws Exception { | |
Timer timer = new Timer(); | |
timer.start(); | |
timer.result = func.call(); | |
timer.stop(); | |
return timer; | |
} | |
/** | |
* Startet eine manuelle Messung | |
* | |
* @throws Exception Falls bereits eine Messung auf diesem Objekt läuft. | |
*/ | |
public void start () throws Exception { | |
if (isRunning) { | |
throw new Exception("Do not start a running stopwatch!"); | |
} else { | |
isRunning = true; | |
startTime = System.nanoTime(); | |
} | |
} | |
/** | |
* Stopt eine manuelle Messung | |
* | |
* @return Die gemessene Zeit in Millisekunden. | |
*/ | |
public double stop () { | |
runTime = ((System.nanoTime() - startTime) / 1000000.0); | |
isRunning = false; | |
return runTime; | |
} | |
/** | |
* Das Ergebnis des gemessenen Funktionsaufrufes. | |
* | |
* @return Der Rückgabewert der aufgerufenen Funktion bei einer | |
* automatischen Messung. Bei einer manuellen Messung ist der Inhalt | |
* undefiniert. | |
*/ | |
public int getResult() { | |
return this.result; | |
} | |
/** | |
* Die gemessene Zeit | |
* | |
* @return Die zuletzt von diesem Objekt gemessene Zeit in Millisekunden. | |
* Funktioniert sowohl für manuelle wie auch für automatische Messungen. | |
*/ | |
public double getRunTime() { | |
return runTime; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment