You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here’s an optimized version of your program.
**Summary of optimizations:**
- The `for i in range(number * 100): k += i` is replaced by a direct formula for the sum of an arithmetic sequence, which is O(1) time and memory.
- Every instance of `sum(range(number))` is replaced similarly.
- The `" ".join(str(i) for i in range(number))` is optimized by using a list comprehension and `map(str, ...)`, which is much faster for large N than the generator and avoids multiple generator state transitions.
**Optimized code:**
This version eliminates the time-consuming loops and generator, while producing **identical results** for all input values.
0 commit comments