简体   繁体   中英

Factors In Java

I am trying to compute a list of prime factors of the ​factorial of n, with the prime factors sorted in increasing order and with each in this list exactly as many times as it would appear in the prime factorization of the factorial.

I have a program that computes a linkedlist of prime numbers up to a specified number, but I'm not sure how to implement that while appending the prime factors of the integer that is currently being multiplied into the factorial:

Lets say you have computed all the prime numbers from [2..N] and lets call this set P , then for each p in P you can get each number n from [2..N] and check how many times p divides n and put it on the linked list. If you think a little bit more about what MT756 said on the comment above you can make the algorithm i said a little bit faster. I didn't put the java code to make this task a little bit fun for you. : )

public static List<Integer> getFactorialPrimeFactors(int n)
{
    List <Integer> primes = primeNum(n);

    ArrayList <Integer> primeDivisors = new ArrayList<>();
    for(int i: primes)
    {
        int count = 0;
        for(int num = i; num <= n; num *= i)
        {
            count += n/num;
        }

        while(count > 0)
        {
            primeDivisors.add(i);
            count--;
        }
    }

    return primeDivisors;
}

Explanation - https://math.stackexchange.com/a/642220

You should also have a method that computes the prime factors of a number and returns it as a list. You pass in the list of primes you got from primeNum, which returns the list of primes up to a number.

public static List<Integer> primeFactors(int number, List<Integer> primes) {
    List<Integer> ans = new ArrayList<>();
    // test condition includes number >= primes.get(i)
    // so the loop exits when the current prime is greater than the number
    for(int i = 0; i < primes.size() && number >= primes.get(i); i++){
        while(number % primes.get(i) == 0){
            ans.add(primes.get(i));
            number = number / primes.get(i);
        }
    }
    return ans;
}

Then in your main method you can write a forloop that iterate through all number from 1 up to n and call this primeFactors method every loop. You iterate thruogh the result you get from calling this method and add those prime numbers to a list. Last you can sort the list if you want the numbers to be in order.

List<Integer> primes = primeNum(n);
for(int i = 1; i <= 10; i ++){
    List<Integer> temp = primeFactors(i,primes);
    for(int j = 0; j < temp.size(); j++){
        list.add(temp.get(j));
    }
}

This is yet another way. It includes several checks to ensure unnecessary testing so as to exit the loops as soon as possible. It works the same way if one wants to find the number of 0's at the end of N factorial. To do that, just add up the quotients of dividing N by successive powers of 5 (as 5 is the larger factor of 10).

This works by adding up the quotients of the N by continuously dividing by each prime.

    public static List<Integer> getNFactorialFactors(int n, List<Integer> primes) {
        List<Integer> factors = new ArrayList<>();
        int nn = n;
        for (int p : primes) {
            while (nn > 1) {
                for (int i = nn / p; i > 0; i--) {
                    factors.add(p);
                }
                nn /= p;
            }
            nn = n;
        }

        return factors;
    }

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