/*******************************************
** This program reads an integer from the
** user
and prints out its factorization
** into
irreducibles (prime numbers).
This program relies
** on a function firstFactor, which already has
** a prototype but
does NOT have a function definition.
**
** YOUR JOB: write
the function definition for firstFactor.
** You should NOT
change anything else in the code (don’t change main).
**
** Details on firstFactor: this function takes an
integer n,
** where
n > 1, and returns the smallest
** factor
of n, i.e. the smallest
** integer
greater than 1 that divides n evenly
**
** For instance, firstFactor(10) should return 2
** (since 2 is the smallest number > 1 that divides 10)
** and firstFactor(27) should return 3
** (since 3 is the smallest number > 1 that divides 27)
** and firstFactor(17) should return 17
** (since 17 is the smallest number > 1 that divides 17)
**
** Note that this should be a relatively short function:
** do NOT write an if
statement testing for 2, testing for 3, testing for 4, etc.
** Instead, your
function should work for any possible number (think about how to do this!)
*******************************************/
#include <iostream>
using namespace std;
/*******************************************
** firstFactor
FUNCTION PROTOTYPE
*******************************************/
int firstFactor(int);
/*******************************************
** MAIN FUNCTION
*******************************************/
int main()
{
//
Get integer n, n > 1, from user
int
n;
cout << "Enter an
integer larger than 1: ";
cin >> n;
// Print out factorization
cout << "The
factorization of " << n << " is ";
while(n > 1)
{
//
get & print next irreducible factor
int
f = firstFactor(n);
cout << '(' << f << ')';
n = n / f;
}
cout << endl;
return 0;
}
/*******************************************
** firstFactor
FUNCTION DEFINITION goes below these lines
*******************************************/
syntax highlighted by Code2HTML, v. 0.9.1