Finding Prime Factors in C++
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 C++ 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;
Call it ListPrimeFactors.
Type out the adjoining C++ code for listing prime factors.
Note: You can comment out the C++ code for the Arithmetic class from the previous lesson if you have been following.