Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int
- factdigsum (unsigned border)
- {
- #define GROUPING 1000000
- unsigned dig [border + 1]; /* digits grouped by log10 (GROUPING) */
- unsigned first = 0, last = 0, carry, n, x, sum = 0;
- dig [0] = 1;
- for (n = 2; n != border; ++n)
- {
- carry = 0;
- /* multiplying */
- for (x = first; x < last + 1; ++x)
- {
- unsigned nxt;
- nxt = dig [x] * n + carry;
- dig [x] = nxt % GROUPING;
- if ((x == first) && ((nxt % GROUPING) == 0))
- {
- first += 1;
- }
- carry = nxt / GROUPING;
- }
- /* opening new digit group */
- if (carry != 0)
- {
- last += 1;
- dig [last] = carry;
- }
- }
- /* now we may calculate sum of digits */
- for (x = first; x < last + 1; ++x)
- {
- unsigned g = GROUPING;
- while (g > 9)
- {
- g = g / 10;
- sum += (dig [x] / g) % 10;
- }
- }
- return sum;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement