I want to implement a program in C++ with which I can see if a number $n$ has a prime factorization of only consecutive primes.
For example $30=2\cdot 3 \cdot 5$ is such a number, while $21=3 \cdot 7$ isn't because $5$ is missing. Repeated prime factors are allowed so $60=2^2 \cdot 3 \cdot 5$ would have the desired property. Before getting my hands on the keyboard I was figuring out how a possible algorithm might look.
Here is what i have so far:
(1) Find the number of prime factors of $n$, call this number $a_1$
(2) Find the maximum of all prime factors of $n$, call it $M$
(3) Find the minimum of all prime factors of $n$, call it $m$
(4) Find the number of primes $< m$ call it $a_2$
(5) Find the number of primes $\leq M$ call it $a_3$
(6) If $a_1=a_3-a_2$ then $n$ has the property that we are looking for.
I have really limited knowledge in programming so the above algorithm would be one (maybe the only) way I am able to implement a program based on my knowledge.
my question : Can I achieve my goal by implementing the above algorithm or is there a serious mistake I overlooked? Thanks in advance !!
Since you stated that $n$ is fairly small, it would be efficient to precompute an array, $\mathtt{p[i]}$, of the primes, in order, less than or equal to the largest value you ever expect the input to be.* Then, starting at the smallest prime, $\mathtt{p[0]}$, see if $\mathtt{p[i]}$ divides $n$. If it doesn't divide $n$, increment $i$ and continue. If it does divide $n$, check whether it divides $\mathtt{n/p[i]}$. If it does, quit, since you don't want repeated factors of the same prime. Otherwise check whether $\mathtt{p[i+1]}$ divides $n$. If so, keep going and if not, again quit. Rinse and repeat.
*Edit. As Greebo pointed out, your list of primes need only go up to the largest prime $\le\sqrt{n}$. For inputs less than a million, you'll only need 168 primes.