Added some algorithms and moved some files and fixed some comments
This commit is contained in:
parent
017a27931a
commit
9297bea93d
25 changed files with 1043 additions and 192 deletions
117
src/zutil/algo/EuclideansAlgo.java
Normal file
117
src/zutil/algo/EuclideansAlgo.java
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
package zutil.algo;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.LinkedList;
|
||||
|
||||
import zutil.MultiPrintStream;
|
||||
|
||||
/**
|
||||
* Euclidean algorithm is an algorithm to determine
|
||||
* the greatest common divisor (GCD)
|
||||
*
|
||||
* @author Ziver
|
||||
*
|
||||
*/
|
||||
public class EuclideansAlgo {
|
||||
|
||||
/**
|
||||
* Simple Test
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args){
|
||||
MultiPrintStream.out.println("*** Correct Answer: ");
|
||||
MultiPrintStream.out.println("java.util.LinkedList{0, 2, 1, 1, 1, 4, 12, 102, 1, 1, 2, 3, 2, 2, 36}");
|
||||
MultiPrintStream.out.println("GCD: 1");
|
||||
|
||||
MultiPrintStream.out.println("*** Integer:");
|
||||
MultiPrintStream.out.dump(calcGenerators(60728973, 160523347));
|
||||
MultiPrintStream.out.println("GCD: "+calc(60728973, 160523347));
|
||||
|
||||
MultiPrintStream.out.println("*** BigInteger: ");
|
||||
MultiPrintStream.out.dump(calcGenerators(new BigInteger("60728973"), new BigInteger("160523347")));
|
||||
MultiPrintStream.out.println("GCD: "+calc(new BigInteger("60728973"), new BigInteger("160523347")));
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the Euclidean algorithm on the two input
|
||||
* values.
|
||||
*
|
||||
* @param a is the first integer
|
||||
* @param b is the second integer
|
||||
* @return a integer containing the GCD of the integers
|
||||
*/
|
||||
public static int calc(int a, int b){
|
||||
int t;
|
||||
while( b != 0 ){
|
||||
t = b;
|
||||
b = a % b;
|
||||
a = t;
|
||||
}
|
||||
|
||||
return a;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the Euclidean algorithm on the two input
|
||||
* values.
|
||||
*
|
||||
* @param a is the first BigInteger
|
||||
* @param b is the second BigInteger
|
||||
* @return a BigInteger containing the GCD of the BigIntegers
|
||||
*/
|
||||
public static BigInteger calc(BigInteger a, BigInteger b){
|
||||
BigInteger t;
|
||||
|
||||
while( !b.equals(BigInteger.ZERO) ){
|
||||
t = b;
|
||||
b = a.mod( b );
|
||||
a = t;
|
||||
}
|
||||
|
||||
return a;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the Euclidean algorithm on the two input
|
||||
* values to find the generators for the values.
|
||||
*
|
||||
* @param a is the first integer
|
||||
* @param b is the second integer
|
||||
* @return a list of integers that is generators for a and b
|
||||
*/
|
||||
public static LinkedList<Integer> calcGenerators(int a, int b){
|
||||
LinkedList<Integer> list = new LinkedList<Integer>();
|
||||
int t;
|
||||
|
||||
while( b != 0 ){
|
||||
list.add( a/b );
|
||||
t = b;
|
||||
b = a % b;
|
||||
a = t;
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the Euclidean algorithm on the two input
|
||||
* values to find the generators for the values.
|
||||
*
|
||||
* @param a is the first BigInteger
|
||||
* @param b is the second BigInteger
|
||||
* @return a list of BigIntegers that is generators of a and b
|
||||
*/
|
||||
public static LinkedList<BigInteger> calcGenerators(BigInteger a, BigInteger b){
|
||||
LinkedList<BigInteger> list = new LinkedList<BigInteger>();
|
||||
BigInteger t;
|
||||
|
||||
while( !b.equals(BigInteger.ZERO) ){
|
||||
list.add( new BigInteger("0").add( a.divide( b ) ) );
|
||||
t = b;
|
||||
b = a.mod( b );
|
||||
a = t;
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
68
src/zutil/algo/WienersAlgo.java
Normal file
68
src/zutil/algo/WienersAlgo.java
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
package zutil.algo;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.LinkedList;
|
||||
|
||||
import zutil.math.ZMath;
|
||||
|
||||
/**
|
||||
* The Wieners algorithm factorizes two big numbers a and b.
|
||||
* It uses the Euclidien algorithm to calculate the generator of the
|
||||
* numbers and then uses them to calculate the factorization.
|
||||
*
|
||||
* @author Ziver
|
||||
*
|
||||
*/
|
||||
public class WienersAlgo {
|
||||
|
||||
/**
|
||||
* Runs the Wieners algorithm for the given values.
|
||||
*
|
||||
* @param n is the first value
|
||||
* @param e is the second value
|
||||
* @return a BigInteger array of length two.
|
||||
* First index is p and second is q.
|
||||
* If no value was found then it returns null.
|
||||
*/
|
||||
public static BigInteger[] calc(BigInteger n, BigInteger e){
|
||||
BigInteger[] ret = null;
|
||||
|
||||
LinkedList<BigInteger> gen = EuclideansAlgo.calcGenerators(e, n);
|
||||
|
||||
BigInteger c0 = BigInteger.ONE;
|
||||
BigInteger c1 = gen.poll();
|
||||
BigInteger d0 = BigInteger.ZERO;
|
||||
BigInteger d1 = BigInteger.ONE;
|
||||
|
||||
BigInteger t, n1, g;
|
||||
while(!gen.isEmpty()){
|
||||
g = gen.poll();
|
||||
|
||||
t = c1;
|
||||
c1 = g.multiply( c1 ).add( c0 );
|
||||
c0 = t;
|
||||
|
||||
t = d1;
|
||||
d1 = g.multiply( d1 ).add( d0 );
|
||||
d0 = t;
|
||||
|
||||
n1 = d1.multiply( e ).subtract( BigInteger.ONE );
|
||||
if( n1.mod( c1 ).equals( BigInteger.ZERO ) ){
|
||||
n1 = n1.divide( c1 );
|
||||
|
||||
// x^2 - ( n - n1 +1 )x + n = 0
|
||||
ret = ZMath.pqFormula(
|
||||
n.subtract( n1 ).add( BigInteger.ONE ).negate(),
|
||||
n);
|
||||
|
||||
if(ret[0].compareTo( BigInteger.ZERO ) >= 0 &&
|
||||
ret[1].compareTo( BigInteger.ZERO ) >= 0 &&
|
||||
ret[0].multiply( ret[1] ).equals( n )){
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,39 +0,0 @@
|
|||
package zutil.algo.path;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class DijkstraPathFinder {
|
||||
|
||||
public static LinkedList<PathNode> find(PathNode start, PathNode stop){
|
||||
// TODO
|
||||
/*
|
||||
1
|
||||
|
||||
5 dist[source] := 0 // Distance from source to source
|
||||
6 Q := copy(Graph) // All nodes in the graph are unoptimized - thus are in Q
|
||||
7 while Q is not empty: // The main loop
|
||||
8 u := extract_min(Q) // Remove and return best vertex from nodes in two given nodes
|
||||
// we would use a path finding algorithm on the new graph, such as depth-first search.
|
||||
9 for each neighbor v of u: // where v has not yet been considered
|
||||
10 alt = dist[u] + length(u, v)
|
||||
11 if alt < dist[v] // Relax (u,v)
|
||||
12 dist[v] := alt
|
||||
13 previous[v] := u
|
||||
14 return previous[]
|
||||
*/
|
||||
|
||||
|
||||
|
||||
LinkedList<PathNode> path = new LinkedList<PathNode>();
|
||||
PathNode current = stop;
|
||||
while(true){
|
||||
path.addFirst(current);
|
||||
current = current.getSourceNeighbor();
|
||||
if(current.equals(start)){
|
||||
path.addFirst(start);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return path;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package zutil.algo;
|
||||
package zutil.algo.search;
|
||||
|
||||
import zutil.algo.sort.sortable.SortableDataList;
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue