简体   繁体   中英

Using nested arrays to show and count number of prime factors or other operations

I have an algorithm which counts prime factors of a given range of numbers. For example between 10 and 20.

Result is: number 10: factors are: 2, 5. number 11: factors are: 11. number 12: factors are: 2, 6. etc. ... .

But I would like to check which numbers have only 2 or 3 or 4 or more prime factors.

Those numbers I store in an array. And my idea is to assign those individual prime factors to a given number. So I should have an arraylist probably, which is changing dynamically (as name as size also).

For example:

ArrayOfNumbers[0] = 10;
ArrayOfPrimeFactorsOf 10 Number[0] = 2; ArrayOfPrimeFactorsOf 10 Number[1] = 5;

ArrayOfNumbers[1] = 11; ArrayOfPrimeFactorsOf 11 Number[0] = 11;

And I could compare which number has for example 2 or 3 prime factors => by getting size of array of prime factors (ArrayOfPrimeFactorsOf..Number) which belongs to a number of array (ArrayOfNumbers) and display this number and its prime factors also.

My question is: How could I make this as nested array (ArrayOfPrimeFactorsOf..Number) and assign this to ArrayOfNumbers[..]?

Maybe do you have any other idea how to resolve this challenge?

Below is a program:

 public static void countPrimeFactors() {

    int min = 10; // min number in array
    int max = 20; // max number in array

    Integer[] arrayOfNumbers = new Integer[100000];

    for (int a = 0; a < arrayOfNumbers.length; a++) {

        for (int k = min; k <= max; k++) {

            arrayOfNumbers[a] = k;
            System.out.print(arrayOfNumbers[a] + ": ");

            if (arrayOfNumbers[a] % 2 == 0) {

                System.out.print(2 + " ");
                arrayOfNumbers[a] /= 2;

            }
        // n must be odd at this point. So we can
        // skip one element (i = i +2)
            // i is a current factor
            // array[a] is a current number

        for (int i = 3; i <= Math.sqrt(arrayOfNumbers[a]); i += 2) {
            // While i divides n, print i and divide n
            if (arrayOfNumbers[a] % i == 0) {

                System.out.print(i + " ");
                arrayOfNumbers[a] /= i;
            }

        }

        // This condition is to handle the case when
        // n is a prime number greater than 2
        if (arrayOfNumbers[a] > 2)

            System.out.print(arrayOfNumbers[a] + " , ");

        }  
        break;
    }

    System.out.print("Length of array[a]: " + arrayOfNumbers.length);
}

I think that using maps would be best solution for this, TreeMap especially, key should be number for which you are finding factors, and value should be some other collection containing all factros. Example:

    Map<Integer, Set<Integer>> numbers = new TreeMap<>();
            int min = 10;
    int max = 20;
    Map<Integer, Set<Integer>> numbers = new TreeMap<>();
    for (int i = min; i <= max; i++) {
        Set<Integer> factors = new TreeSet<>();
        for (int j = 2; j <= i/2; j++) {
            if (i % j == 0) {
                factors.add(j);
            }
        }
        //factors.add(i);
        numbers.put(i, factors);

    }
    System.out.println(numbers.get(11).size());
    System.out.println(numbers.values());

This is general idea, you could easily get then number of factors for each number by numbers.get(someNumber).size();

If you uncomment factors.add(i); then number for which we are finding factors would be counted as well, if you need it that way.

Iterating

  // Iterator mapIt = numbers.keySet().iterator(); - you can only iterate thru map by using iterator

    for(int i = min; i <= max; i++){
        Iterator it = numbers.get(i).iterator();
        System.out.print("Iterating for number " + i+": ");
        while(it.hasNext()){
            System.out.print(it.next()+" ");
        }
        System.out.println();
    }

This should give you general idea, if you don't like using iterators then use ArrayList instead of Set, like Map<Integer, List<Integer>>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM