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);
}- A. 50
- B. 98
- C. 100
- 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
- What is printed? c #include <stdio.h> #include <string.h> int main(void) { char s[] = "stressed"; char a = s, b = s + 7; while (a < b) {…
- What is printed? c #include <stdio.h> unsigned parse(const char s) { unsigned n = 0; while (s) n = n 10 + (unsigned)(s++ - '0'); return n…
- What is printed? c #include <stdio.h> int main(void) { long long x = (1LL << 40) + 5; printf("%d", (int)(x & 0xFFFFFFFFLL)); }
- What is printed? Assume int is 4 bytes. c #include <stdio.h> int main(void) { printf("%zu", sizeof('a')); }
- What is printed? c #include <stdio.h> int main(void) { printf("%d", ~5 & 0xF); }
- What is printed? c #include <stdio.h> int main(void) { unsigned char c = 254; int n = 0; while (++c) n++; printf("%d", n); }