Finding Prime Factors in Java
Finding prime factors is all about selecting those factors of
a number that are prime.
The prime factors of 36 as an example, are:
Finding prime factors is all about selecting those factors of
a number that are prime.
The prime factors of 36 as an example, are:
We'll go about the code in a simple way:
Starting with 2, find the first factor of the number of interest
- this factor will definitely be a prime.
Store this factor away.
Divide number in question by found factor.
Using result from Step 2 as the new number in question (number whose prime factors we are trying to find), repeat Step 1.
Continue recursively until we arrive at 1.
We'll use the square-root of number range.
Create a new class file; File, New File.
Call it PrimeFactors.
Type out the adjoining Java code for listing prime factors.
Note: You can comment out the Java code for the main class from the previous lesson if you have been following.