ProgrammingVery hardCompetitive coding (CP)

How many factors of 2 does 100! contain? Compute with this program.

How many factors of 2 does 100! contain? Compute with this program.
#include <stdio.h>
int main(void) {
    int c = 0;
    for (int i = 2; i <= 100; i++)
        for (int x = i; x % 2 == 0; x /= 2)
            c++;
    printf("%d", c);
}
  1. A. 50
  2. B. 98
  3. C. 100
  4. D. 97

Correct answer: D. 97

The loop counts the factors of 2 in each number from 2 to 100 and sums them. The total is 50 + 25 + 12 + 6 + 3 + 1 = 97, which is Legendre's formula for 100!.

More C Programming questions

Exams that ask this topic