Skip to content

Instantly share code, notes, and snippets.

@lnrsoft
Created October 10, 2019 11:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lnrsoft/4046a0965b2136c84b468a9423aad1ec to your computer and use it in GitHub Desktop.
Save lnrsoft/4046a0965b2136c84b468a9423aad1ec to your computer and use it in GitHub Desktop.
Factorial digital solution [Written by Roland Ihasz , 10th October 2019]
// Title: Factorial digital solution
// 10th Oct 2019
// Written by Roland Ihasz
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
typedef std::vector<unsigned int> Digits;
Digits factorial(unsigned int maxFactor)
{
Digits result = { 1 };
result.reserve(2568);
for(unsigned int factor = 2; factor <= maxFactor; factor++) {
unsigned int carry = 0;
for(auto &digit : result) {
digit = digit * factor + carry;
if(digit >= 10) {
carry = digit / 10;
digit %= 10;
}
else {
carry = 0;
}
}
while(carry != 0) {
result.push_back(carry % 10);
carry /= 10;
}
}
return result;
}
int main()
{
unsigned int tests;
std::cin >> tests;
while(tests--) {
unsigned int number;
std::cin >> number;
unsigned int sum = 0;
for(auto i : factorial(number)) {
sum += i;
}
std::cout << sum << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment