0

I would like to archive number factorisation in java but am getting errors when try to run the program.I am also getting unrealiable results but am convinced this should work.

Below is my code

  import java.math.*;

public class FactorizerBig{
    private BigDecimal input;
    FactorizerBig(BigDecimal x){
    input = x;
    }
    public void processBig(){//main algorithm
    String s = "";
    MathContext mc = new MathContext(2); // 2 precision
    BigDecimal idx = new BigDecimal("2");
    BigDecimal z = new BigDecimal("0");
    while((idx.compareTo(input)) == -1 || (idx.compareTo(input)) == 0 ){
        int comp =  (input.remainder(idx,mc)).compareTo(z);
        if(comp != 0){
        idx = idx.add(idx);
        }else{ 
        s = s.concat(" * "+idx);
        input = input.divide(idx);
        idx = new BigDecimal("2");
        }

    }

    System.out.println(s.substring(2));
    }
}

And here is what I am getting as output

javac FactorizerBig.java Test.java && java Test
12
 2 * 2
23
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -2
    at java.lang.String.substring(String.java:1875)
    at FactorizerBig.processBig(FactorizerBig.java:25)
    at Test.main(Test.java:10)
Emma
  • 323
  • 3
  • 16
  • 2
    You get errors and bad outputs, but you chose not to share either of these with us while asking this question? – Kon Sep 16 '14 at 21:50
  • Could you please give a more specific explanation of what this program is supposed to do? Probably include some examples of what input you're giving it, what you think it should output and why, and what it's actually outputting. – ajb Sep 16 '14 at 21:51
  • I have to enter a bigdecimal which should then be factorised and the program should ask me to enter input again until i enter wrong form of input,for exampe if i enter 12345 it should print 3 * 5 * 823 – Emma Sep 16 '14 at 21:55
  • How will this program ever find factors other than 2?, and what would you expect it to print out if it completely skipped over the while loop? (which, it _does_ when you give it 23 as input.) – Solomon Slow Sep 16 '14 at 22:00
  • Have you written and tested your algorithm with simple _int_s before writing it with `BigDecimal`? It would make your mistakes easier to find. – Volune Sep 16 '14 at 22:07
  • I did with long and it is working well,I am not so well versed with the Bigdecimal use yet,maybe you may also like to see my implentation for that,here:`public class FactorizerLong{ private long input; FactorizerLong(long x){ input = x; } public void process(){//main algorithm int i = 2; String s = ""; while(i <= Math.abs(input)){ if(input % i != 0){ i++; }else{ s = s.concat(" * "+i); input = input / i; i = 2; } } if(input < 0){ System.out.print("-"); } System.out.println(s.substring(2)); } }` – Emma Sep 16 '14 at 22:15
  • What is `idx = idx.add(idx);` supposed to do? It actually computes `idx + idx`, i.e. `2 * idx`, which is probably not what you want. – ajb Sep 16 '14 at 22:29
  • @ajb you're right i want it to be idx = idx + 1 – Emma Sep 16 '14 at 22:44

1 Answers1

1

You converted i++ into idx = idx.add(idx) which means i = i + i.

Use idx = idx.add(BigDecimal.ONE)

By the way, you can also replace i <= input by (idx.compareTo(input)) < 1

import java.math.*;

public class FactorizerBig {
    private BigDecimal input;

    FactorizerBig(BigDecimal x) {
        input = x;
    }

    public void processBig() {//main algorithm
        String s = "";
        MathContext mc = new MathContext(2); // 2 precision
        BigDecimal idx = new BigDecimal("2");
        BigDecimal z = new BigDecimal("0");
        while ((idx.compareTo(input)) < 1) {
            int comp = (input.remainder(idx, mc)).compareTo(z);
            if (comp != 0) {
                idx = idx.add(BigDecimal.ONE);
            } else {
                s = s.concat(" * " + idx);
                input = input.divide(idx);
                idx = new BigDecimal("2");
            }

        }

        System.out.println(s.substring(2));
    }
}
Volune
  • 4,324
  • 22
  • 23
  • while this answer is very useful,I would like to add that the `MathContext mc = new MathContext(2);` should have a precision value larger than 2 to accept larger input values(for my case),I will accept it as it is though – Emma Sep 16 '14 at 23:11