Q160: Factors and Factorials

//Q160 Factors and Factorials
//Accepted 2008-04-13 21:09
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;
int ispf(int);
int n;
int main(void)
{
int i, j, nt, pn, //total primefacter number
pf[100], //primefacters
pfn[100]; //each primefacters contains
while(1)
{
pn = 0;
cin >> n;
if (n == 0)
break;
nt = n;
for(i = 2; i <= nt; i++)
{
if (ispf(i) == 1)
pf[++pn] = i;
}
for(i = 1; i <= pn; i++)
{
nt = n;
pfn[i] = 0;
while(nt > 0)
{
pfn[i] += (nt / pf[i]);
nt /= pf[i];
}
}
cout << setw(3) << n << "! =";
for(i = 1; i <= pn; i++)
{
if ((i % 15 == 1) && (i != 1))
cout << endl << " ";
cout << setw(3) << pfn[i];
}
cout << endl;

for(i = 1; i <= pn; i++) //clear
{
pf[i] = 0;
pfn[i] = 0;
}
}
}

int ispf(int testn)
{
for(int i = 2; i <= sqrt(testn); i++)
if (testn % i == 0)
return 0;
return 1;
}

留言

這個網誌中的熱門文章

成功安裝Vista & Ubuntu Linux 8.04 LTS 雙系統!!!

駕訓班~XD