ProgrammingModerate
A program sums the digits of 10^5 numbers with one loop per number that divides by 10. What is the total cost, about?
- A. About 10^10 divisions, too slow
- B. About 10^5 divisions, which is fast enough
- C. About 10^12 divisions, too slow
- D. About 10^6 divisions, which is fast enough
Correct answer: D. About 10^6 divisions, which is fast enough
Each number has at most 10 digits, so the work per number is about 10 steps, and 10^5 numbers take about 10^6 divisions.
More C Programming questions
- A queue for BFS on a grid of 10^6 cells uses a fixed array of size 10^6 indexed by head and tail. Is this enough?
- To find the maximum over 10^5 queries of ranges in a static array, which approach is fast enough?
- A program multiplies two n x n matrices with n = 500 in int, and the product entries reach about 10^12. What is the error?
- To answer 'how many times does x appear in a sorted array' in O(log n), which pair of searches is correct?
- A bit array for flags of 10^9 items is held as one byte per item. What is the memory use, and is it fast enough in typical limits?
- struct P { char a; int b; char c; }; printf("%zu", sizeof(struct P)); What is printed with 4-byte int and natural alignment?