Skip to content

Commit

Permalink
Fixed #13, added tests for multiply using FFT and using loops with st…
Browse files Browse the repository at this point in the history
…ring input, does appear to have exposed a bug
  • Loading branch information
mnordste committed Feb 23, 2022
1 parent cb84fe5 commit f8f6e97
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
44 changes: 42 additions & 2 deletions src/com/jwetherell/algorithms/mathematics/Multiplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@

public class Multiplication {

static boolean[] reachedBranch = new boolean[200];
private static void BRANCH(int index) {
if (!reachedBranch[index]) {
reachedBranch[index] = true;
System.out.println("--------------");
for (int i = 0; i < reachedBranch.length; i++) {
if (reachedBranch[i]) {
System.out.println("REACHED BRANCH #" + i);
}
}
}
}

public static final long multiplication(int a, int b) {
long result = ((long) a) * ((long) b);
return result;
Expand Down Expand Up @@ -131,19 +144,27 @@ public static String multiplyUsingLoopWithStringInput(String a, String b) {
boolean aIsNegative = false;
ArrayList<Integer> first = new ArrayList<Integer>();
for (char n : a.toCharArray()){
BRANCH(1+100);
if (n=='-') {
BRANCH(2+100);
aIsNegative = true;
continue;
}else{
BRANCH(3+100);
}
first.add(n-'0');
}

boolean bIsNegative = false;
ArrayList<Integer> second = new ArrayList<Integer>();
for (char n : b.toCharArray()){
BRANCH(4+100);
if (n=='-') {
BRANCH(5+100);
bIsNegative = true;
continue;
}else{
BRANCH(6+100);
}
second.add(n-'0');
}
Expand All @@ -153,8 +174,10 @@ public static String multiplyUsingLoopWithStringInput(String a, String b) {

ArrayList<Integer> res = new ArrayList<Integer>(Collections.nCopies(first.size()+second.size(), 0));
for (i=0;i<=lim1;i++) {
BRANCH(7+100);
k=i;
for (j=0;j<=lim2;j++) {
BRANCH(8+100);
int f = first.get(i);
int s = second.get(j);
mul=f*s;
Expand All @@ -165,26 +188,43 @@ public static String multiplyUsingLoopWithStringInput(String a, String b) {
}

for (i=(lim1+lim2)+1;i>=0;i--) {
BRANCH(9+100);
if (flag==1){
BRANCH(10+100);
res.set(i,res.get(i)+carry);
flag=0;
}else{
BRANCH(11+100);
}

if (res.get(i)>=10 && i!=0) {
BRANCH(12+100);
rem=res.get(i)%10;
carry=res.get(i)/10;
res.set(i,rem);
flag++;
}else{
BRANCH(13+100);
}
}

StringBuilder sb = new StringBuilder();
if (aIsNegative ^ bIsNegative)
if (aIsNegative ^ bIsNegative){
BRANCH(14+100);
sb.append('-');
}else{
BRANCH(15+100);
}

boolean zeroCheck = true;
for (Integer s : res) {
if (zeroCheck && s.equals(0))
BRANCH(16+100);
if (zeroCheck && s.equals(0)){
BRANCH(17+100);
continue;
}else{
BRANCH(18+100);
}
zeroCheck = false;
sb.append(s);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,21 @@ public void multiplication() {
assertTrue("Multiplication using loop with int input. a=" + a + " b=" + b + " result=" + result + " check=" + check, (result == check));
}
}
@Test
public void testMultiplicationUsingFFTZero(){
long result = Integer.parseInt(Multiplication.multiplyUsingFFT("0", "0"));
long expected = 0;

assertTrue(result == expected);
}

@Test
public void testMultiplyUsingLoopsWithStringInputZero(){
long result = Integer.parseInt(Multiplication.multiplyUsingLoopWithStringInput("0", "0"));
long expected = 0;

assertTrue(result ==expected);
}

@Test
public void division() {
Expand Down Expand Up @@ -309,4 +324,3 @@ public void millerRabin() {
assertFalse("Miller-Rabin test error. " + composite, Primes.millerRabinTest(composite));
}
}

0 comments on commit f8f6e97

Please sign in to comment.