1

I have to write a code, that finds prime factorization of given number. The code have to give an 2-dimensional array back, that has the numbers on the first row and the powers on the second. I don't know how to save the results in this array.

public static long[][] primeFactorization(long l) {
    //n = 6600 = 2^3*3^1*5^2*11^1
    // z[0][4] = 2|3|5|11 - coefficient
    // z[1][4] = 3|1|2|1  - power
    int count = 0;
    int i=2;
    long[][] x = new long[2][];
    while(l%2==0) { 
        l=l/2; 
        count++; //power   8 = 2.2.2 => count = 3
     }
     i++;
     //ToDo: save i=2 in array[0][] = {2,...};
     for (i = 3; i <= Math.sqrt(l); i = i+2) { 
     // While i divides l, print i and divide l 
         while (l%i == 0) { 
                int temp = i; //ToDo: save the divider in array[0][]
                count++; //ToDo: save the power in array[1][]
                l = l/i; 
                i = temp;
            } 
        } 
    return x;
}
1
  • "save A in array[X][Y]" simply means array[X][Y] = A; Commented Nov 6, 2018 at 19:24

2 Answers 2

1

The below one is not working as expected but is in the right direction of what you are looking for. You are missing the filling array part which can be done as follows. I know the below code has some glitches which can be fixed by looking into it. But it will definitely give you a direction. If you still face issues please let us know.

    public static long[][] primeFactorization(long l) {
    //n = 6600 = 2^3*3^1*5^2*11^1
    // z[0][4] = 2|3|5|11 - coefficient
    // z[1][4] = 3|1|2|1  - power
    int count = 0;
    int i=2;
    long[][] x = new long[2][10];
    while(l%2==0) {
        l=l/2;
        count++; //power   8 = 2.2.2 => count = 3
    }
    x[0][0]=2;
    x[1][0]=count;
    //ToDo: save i=2 in array[0][] = {2,...};
    int row=0;
    int col=1;
    for (i = 3; i <= l; i = i+2) {
        count=0; //setting count to zero for every divisor
        // While i divides l, print i and divide l
        while (l%i == 0) {
            count++; //ToDo: save the power in array[1][]
            l = l/i;
        }
        x[row][col]=i;
        x[row+1][col]=count;
        col++;
    }
    return x;
}
Sign up to request clarification or add additional context in comments.

Comments

1

If I got you right you receive values inside each iteration of a loop and you want to store these values in a 2d array with 2 rows. So generally you can do something like this:

1) create an integer variable outside the loop (let's say int index = 0;);

2) in each iteration of the loop you can save the results in your 2d array like this: x[0][index] = result1; and x[1][index] = result2;

3)increment the variable index in the end of your loop (index++);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.