java

Find Prime Factors()

Parameters: int n

Input integer for which we need to find the prime factors

Returns: List<Integer> which contains the prime factors

This Java function calculates and returns the prime factors of a given number. It iterates over potential factors and checks if each one is a prime and a factor of the input number.

Variables
Loops
Conditionals
Functions
Medium dificulty

Writing a Java Function to Find Prime Factors

Hello there! This blog post is intended for programmers interested in crafting a particular Java function. We'll carefully walk you through how to code a function called 'find-prime-factors'. This function uncovers the prime factors of a given number. Stay tuned, as next up are the steps you'll need to write this function. Let's get started!

Step 1: Understanding the Problem

Before we begin writing the code, we need to understand what prime numbers are and how prime factorization works. A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. The process of prime factorization involves breaking down a composite number into smaller non-trivial divisors, which when multiplied, gives us the original number.

To find the prime factors of a given number, we start by dividing the number with the smallest prime number, i.e., 2. If the number is completely divisible, we divide it by 2 again and continue this process until the number is not divisible by 2 any more, then we move to the next prime number. We keep doing this until the number becomes 1.

Here is the initial setup of our function:

public ArrayList<Integer> findPrimeFactors(int number) {
  ArrayList<Integer> primeFactors = new ArrayList<>();
  // we will add the prime factorization logic here
  return primeFactors;
}

Step 2: Handling Prime Factor 2

As mentioned earlier, we start by dividing the number with the smallest prime number 2. We keep dividing the number by 2 until it's not divisible any more. Our function is now updated as follows:

public ArrayList<Integer> findPrimeFactors(int number) {
  ArrayList<Integer> primeFactors = new ArrayList<>();
  while (number % 2 == 0) {
    primeFactors.add(2);
    number /= 2;
  }
  return primeFactors;
}

Step 3: Handling Larger Prime Factors

After handling the prime factor 2, we handle other prime factors. Starting from 3, we keep dividing the number by the given factor until it's not divisible any more. We increment the factor by 2 after each successful division to check for the next possible prime factor. The function is updated accordingly:

public ArrayList<Integer> findPrimeFactors(int number) {
  ArrayList<Integer> primeFactors = new ArrayList<>();
  while (number % 2 == 0) {
    primeFactors.add(2);
    number /= 2;
  }

  for (int i = 3; i*i <= number; i += 2) {
    while (number % i == 0) {
      primeFactors.add(i);
      number /= i;
    }
  }
  return primeFactors;
}

Step 4: Handling the Remaining Number

There might be a case where the remaining number is a prime number greater than 2. If so, the remaining number will be the prime factor of the original number. We update our function to add the remaining number to our list of prime factors:

public ArrayList<Integer> findPrimeFactors(int number) {
  ArrayList<Integer> primeFactors = new ArrayList<>();
  while (number % 2 == 0) {
    primeFactors.add(2);
    number /= 2;
  }

  for (int i = 3; i*i <= number; i += 2) {
    while (number % i == 0) {
      primeFactors.add(i);
      number /= i;
    }
  }

  if (number > 2) {
    primeFactors.add(number);
  }

  return primeFactors;
}

Conclusion

The function findPrimeFactors accurately computes the prime factors of any given number. It does this by continuously dividing the given number by the smallest unused prime number until the number is reduced to 1. The remaining number is added to the final list of prime factors if it's greater than 2. This function works for all natural numbers greater than 1.

Learn function in:

Prime Factorization

Prime factorization is determining the prime numbers that multiply together to give the original number

Learn more

Mathematical principle

The prime factorization of a number is a representation of the number as a product of prime numbers. For example, the prime factorization of `18` is `2 * 3 * 3`. The function applies this principle by testing potential factors starting from `2` and checking if each factor divides the input number completely and is a prime number.

Learn more