What are Prime Factors? | Maths Explanation for Java Kids
Finding prime factors is all about selecting those factors of
a number that are prime.
The prime factors of 36 as an example, are:
In this guide, we'll thoroughly explain prime factorisation and show
how to code a Java algorithm to find prime-factors in a detailed and interactive manner.
This Math exercise and Java algorithm will help young students
understand prime factorization by listing all prime factors of a number.
Step-by-step Guide to Prime Factorisation of Numbers in Java
We'll go about the Java algorithm to find prime factors in a simple way:
Step 1:
Starting with 2, find the first factor of the number of interest
- this factor will definitely be a prime.
Store this factor away.
Step 2:
Divide number in question by found factor.
Step 3:
Using result from Step 2 as the new number in question (number whose prime factors we are trying to find), repeat Step 1.
Step 4:
Continue recursively until we arrive at 1.
Step 5:
We'll use the square-root of number range.
Create a new Java class file; File, New File.
Call it PrimeFactors.
Type out the adjoining Java code for listing prime factors.
Now Java functions come in handy, don't they?
Note: You can comment out the Java code for the main class from the previous lesson if you have been following.
So! Java Fun Practice Exercise - List Prime Factors
As a fun practice exercise, feel free to try out your own different numbers, and see how the Java code lists the prime factors of those numbers.
Java Code for Prime Factors class.
import java.util.ArrayList;
import java.util.List;
public class PrimeFactors {
private final List<Integer> found_prime_factors;
private final int find_my_factors;
private String render_factors;
private int i;
public PrimeFactors(int val) {
found_prime_factors = new ArrayList<>();
find_my_factors = val;
render_factors = "The prime factors of " + val + " are: \n";
i = 2;
}
/**
* Our function checks 'find_my_factors';
* If it finds a factor, it records this factor,
* then divides 'find_my_factors' by the found factor
* and makes this the new 'find_my_factors'.
* It continues recursively until all factors are found.
* @param - integer.
*/
private int onlyPrimeFactors(int in_question) {
int temp_limit;
temp_limit = (int) Math.ceil(Math.sqrt(in_question));
while (i <= temp_limit) {
if (i != 1 && (in_question % i) == 0) { // avoid an infinite loop with the i != 1 check.
found_prime_factors.add(i);
return onlyPrimeFactors(in_question / i);
}
i++;
}
found_prime_factors.add(in_question);
return 0;
}
/**
* Renders the prime prime factors to screen.
* @return - String value
*/
public String getPrimeFactors() {
onlyPrimeFactors(find_my_factors);
//iterate through and retrieve members
found_prime_factors.stream().forEach((factor) -> {
render_factors += factor + " X ";
});
render_factors = render_factors.substring(0, render_factors.length()-3);
return render_factors;
}
}
Java Code for Prime Factors - Main Class.
public class Arithmetic {
public static void main(String[] args) {
System.out.println("Welcome to our demonstration sequels");
System.out.println("Hope you enjoy (and follow) the lessons.");
System.out.println("");
/*
* List Prime Factors.
*/
PrimeFactors prime_factors = new PrimeFactors(48);
System.out.println(prime_factors.getPrimeFactors());
}
}