Skip to content

Instantly share code, notes, and snippets.

@MaSven
Created January 26, 2017 14:40
Show Gist options
  • Save MaSven/75e69f0657774f773aa1d86080062c74 to your computer and use it in GitHub Desktop.
Save MaSven/75e69f0657774f773aa1d86080062c74 to your computer and use it in GitHub Desktop.
KryptoAufgabe1
package fh.krypto.Aufgabe1;
public class KryptoMathException extends Exception{
public KryptoMathException(String string) {
super(string);
}
}
package fh.krypto.Aufgabe1;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;
import java.util.stream.IntStream;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.MultimapBuilder;
import com.google.common.collect.Multimaps;
/**
*
* @author sven
*
*/
public class KryptoMath
{
private static List<String> linearCombinations = new ArrayList<String>();
private static StringBuilder builder = new StringBuilder();
private static Stack<int[]>combination = new Stack<>();
public static int getLeastCommonFactor(final int a,final int b) throws KryptoMathException {
int rest =0;
int bufferA=a;
int buffer=0;
if(b==0) throw new KryptoMathException("Kann nicht durch 0 teilen");
rest = b;
int[] blah = new int[3];
do {
buffer=rest;
rest=getRest(bufferA, rest);
if(rest==1){
rest=0;
buffer=-1;
}else{
builder.append('\n');
bufferA=buffer;
}
} while (rest!=0);
if(buffer!=-1){
addCombination(bufferA);
}
return buffer;
}
private static int getRest(int start, int divisor){
int[] numbers = new int[4];
int times=0;
times=0;
times = start/divisor;
if((start-(divisor*times)==0))return 0;
numbers[0]=start;
numbers[1]=times;
numbers[2]=divisor;
numbers[3]=(start-(divisor*times));
combination.push(numbers);
builder.append(start)
.append('=')
.append(times)
.append('*')
.append(divisor)
.append('+')
.append((start-(divisor*times)));
return (start-(divisor*times));
}
private static void addCombination(int d){
builder.append(d)
.append('=');
int[] buffer;
int[] buffer2;
while(!combination.isEmpty()&&combination.size()>1){
buffer=combination.pop();
buffer2=combination.peek();
builder.append(buffer[0])
.append('-')
.append(buffer[1])
.append('*').append('(')
.append(buffer2[0]).append('-')
.append(buffer2[1])
.append('*')
.append(buffer[0]).append(')').append('=')
.append('-')
.append(buffer[1]).append('*')
.append(buffer2[0])
.append('+').append('(')
.append('1').append('+')
.append(buffer[1])
.append('*')
.append(buffer2[1]).append(')')
.append('*')
.append(buffer[0]);
if(combination.size()!=1){
builder.append('=');
}
}
}
/**
* @return the linearCombinations
*/
public static String getLinearCombinations() {
return builder.toString();
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>fh.krypto</groupId>
<artifactId>Aufgabe1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Aufgabe1</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>21.0</version>
</dependency>
</dependencies>
</project>
package fh.krypto.Aufgabe1;
import org.junit.Test;
/**
* Unit test for simple App.
*/
public class AppTest
{
@Test
public void testLeastCommonFactorize() {
try {
int a = KryptoMath.getLeastCommonFactor(2406, 654);
System.out.println(a);
System.out.println(KryptoMath.getLinearCombinations());
} catch (KryptoMathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment