What are Factors? | Maths Explanation for Java Kids
Factors are the complete set of integers that will divide
a particular number without remainder.
Take 36 as an example, it's complete set are:
Other than prime numbers, every other number has at least
one factor - not considering 1 as factor.
Where there is just one factor, then this factor is the square
root of the number in question;
In this guide, we'll explore the math behind factors-of-numbers and walk through
how to code a Java algorithm for listing factors in a simple and fun way.
Code Logic for Factorising Numbers in Java - Fun Maths Exercise
Actually, we've been doing factors over the last two demonstrations (lessons).
We can implement a Java algorithm for factorising a number by simply checking for our factors using the
square-root of number range.
We'll start from 1 (which is always a factor).
For each found factor, we'll get the corresponding complementary factor
by dividing the number (whose factors we are trying to find),
by the found factor.
This Math activity and Java script help primary school students understand factorization by listing all factors of a number.
Create a new Java class file; File, New File.
Call it ListFactors.
Type out the adjoining Java code for listing factors.
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 Factors
As a fun practice exercise, feel free to try out your own different numbers, and see how the Java code lists the factors of those numbers.
Java Code for List Factors class.
import java.util.ArrayList;
import java.util.List;
import java.util.Comparator;
public class ListFactors {
private final List<Integer> found_factors;
private final int find_my_factors;
private final int sqrt_range; // Use this range for our loop
private Comparator c; // For sorting arraylists. It is left as 'null'.
ListFactors(int candidate) {
found_factors = new ArrayList<>();
find_my_factors = candidate;
sqrt_range = (int) Math.ceil(Math.sqrt(find_my_factors));
}
/**
* Does the main job of finding the requested factors.
* @return - String value
*/
public String doBusiness() {
/* Loop through 1 to 'find_my_factors' and test for divisibility. */
for (int i = 1; i < sqrt_range; i++) {
if ((find_my_factors % i) == 0) {
found_factors.add(i);
/* Get the complementing factor by dividing 'find_my_factor' by variable i. */
found_factors.add(find_my_factors / i);
}
}
// Sort the array in ascending order; Not entirely necessary.
found_factors.sort(c);
return ("The factors of " + find_my_factors + " are: \n" + found_factors);
}
}
Java Code for List 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 Factors.
*/
ListFactors factors = new ListFactors(48);
System.out.println(factors.doBusiness());
}
}