Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- #include <cmath>
- double const E = 2.7182818;
- int Length = 1000 * log10(1000 / E) + 3;
- //////////////////////////////////////////////////////////////////
- void Show(short * array, int length)
- {
- using std::cout;
- for(auto i = length -1; i >= 0; i--)
- {
- cout << int(array[i]);
- if(i % 3 == 0)
- {
- cout << ' ';
- }
- }
- }
- int GetLength(short * array, int length)
- {
- auto lengthToReturn = 0;
- for(auto i = 0; i < length; i++)
- {
- if(array[i] != 0)
- {
- lengthToReturn = i+1;
- }
- }
- return lengthToReturn;
- }
- void InitializeArrayWithZeros(short * array, int length)
- {
- for (auto i = 0; i < length; i++)
- {
- array[i] = 0;
- }
- }
- int Multiplaying(short * array, int length, int multiplier)
- {
- auto aditionalIndex = 0;
- short *result = new short[Length];
- InitializeArrayWithZeros(result, Length);
- //mno�enie pisemne
- while(multiplier > 0)
- {
- short digitToMultiply = multiplier % 10;
- auto toSumWithNextDigit = 0;
- auto i = aditionalIndex;
- //mnozenie calej pierwszej z kazda kolejna cyfra drugiej liczby
- for(auto j = 0; j < length; i++, j++)
- {
- auto numberToAdd = digitToMultiply * array[j];
- result[i] += numberToAdd % 10 + toSumWithNextDigit; //dodajemy jednosci mnozenia + cyfre z poprzedniego dodawania
- toSumWithNextDigit = numberToAdd / 10 + result[i] / 10; // zostawiamy tylko dziesiatki do nastepnego dodawania
- result[i] %= 10; // zostawiamy tylko jednosci
- }
- //Po skonczeniu petli dodajemy na sam przod to co jeszcze zostalo
- result[i] += toSumWithNextDigit;
- result[i + 1] = result[i] / 10;
- result[i] %= 10;
- multiplier /= 10;
- aditionalIndex++;
- }
- auto resultLength = GetLength(result, Length);
- memcpy(array, result, sizeof(short) * resultLength);
- return resultLength;
- }
- int Factorial(short * array, int length, int n)
- {
- for(int i = 1; i <= n; i++)
- {
- length = Multiplaying(array, length, i);
- }
- return length;
- }
- int main()
- {
- using namespace std;
- int n;
- cin >> n;
- auto start = clock();
- short *array = new short[Length];
- InitializeArrayWithZeros(array, Length);
- auto length = 1;
- array[0] = 1;
- length = Factorial(array, length, n);
- cout << clock() - start << endl << endl; //czas liczenia przed wyswietleniem
- Show(array, length);
- cout << endl << endl << clock() - start; //czas liczenia po wyswietleniu
- char m;
- cin >> m;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement