porogramming c

profilejeff2000
largefactuisngvector.docx

// LargefactUisngVector.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include <iostream>

using namespace std;

#include <vector>

void print(const vector<size_t>&);

void compute(vector<size_t> &, size_t);

int main()

{

size_t ndig, n;

cout << "# of digits:"; cin >> ndig;

vector<size_t> v(ndig);

v.at(ndig - 1) = 1; // v[ndig - 1] = 1;

print(v);

cout << "input n:"; cin >> n;

compute(v, n);

print(v);

return 0;

}

void print(const vector<size_t>& v) {

for (size_t i = 0; i < v.size(); i++)

cout << v.at(i) << " ";

cout << endl;

}

void compute(vector<size_t> & v, size_t n) {

for (size_t i = 2; i <= n; i++) { // 2 to n facotrial

for (size_t k = v.size(), sum, c = 0; k > 0; k--) {

sum = i*v.at(k-1) + c;

v.at(k-1) = sum % 10;

c = sum / 10;

}

}

}