Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to find sum of digits of 100!?

Tags:

c++

algorithm

I know there is a way of finding the sum of digits of 100!(or any other big number's factorial) using Python. But I find it really tough when it comes to C++ as the the size of even LONG LONG is not enough.

I just want to know if there is some other way.

I get it that it is not possible as our processor is generally 32 bits. What I am referring is some other kind of tricky technique or algorithm which can accomplish the same using the same resources.

like image 251
Ashish Yadav Avatar asked Oct 01 '10 08:10

Ashish Yadav


People also ask

What is the sum of all digits in 100?

The sum of all natural numbers from 1 to 100 is 5050. The total number of natural numbers in this range is 100. So, by applying this value in the formula: S = n/2[2a + (n − 1) × d], we get S=5050.

What is the sum of digits of 100 factorial?

The program outputs 93326215443944102188325606108575267240944254854960571509166910400407995064242937148632694030450512898042989296944474898258737204311236641477561877016501813248 as a result for 100! and says the summation of its digits is equal to 666.

How do you find the sum of digits?

What is digit sum? We can obtain the sum of digits by adding the digits of a number by ignoring the place values. So, for example, if we have the number 567 , we can calculate the digit sum as 5 + 6 + 7 , which will give us 18 .

What is sum of digits in the number from 1 to 100?

36. The sum of all the digits of the numbers from 1 to 100 is. 5050.


1 Answers

Use a digit array with the standard, on-paper method of multiplication. For example, in C :

#include <stdio.h>

#define DIGIT_COUNT 256

void multiply(int* digits, int factor) {
  int carry = 0;
  for (int i = 0; i < DIGIT_COUNT; i++) {
    int digit = digits[i];
    digit *= factor;
    digit += carry;
    digits[i] = digit % 10;
    carry = digit / 10;
  }
}

int main(int argc, char** argv) {
  int n = 100;

  int digits[DIGIT_COUNT];
  digits[0] = 1;
  for (int i = 1; i < DIGIT_COUNT; i++) { digits[i] = 0; }

  for (int i = 2; i < n; i++) { multiply(digits, i); }

  int digitSum = 0;
  for (int i = 0; i < DIGIT_COUNT; i++) { digitSum += digits[i]; }
  printf("Sum of digits in %d! is %d.\n", n, digitSum);

  return 0;
}
like image 71
Olathe Avatar answered Sep 25 '22 09:09

Olathe