← Back to challenges

Sum up Sequence of Numbers

PythonHardalgorithmsloopsnumbers

Instructions

Write a function that computes the sum of k*sign(sin(k*k)) with begin_k <= k <= end_k. Python does not have a sign function but it's easy to implement the required logic: 1 if x > 0 else -1, ( sine of natural_num equal to zero only for n=0). Sum of consecutive numbers can be computed by the formula (n1 + n2) * (n2 – n1 + 1) / 2 when all numbers have the same sign.

The main point of this challenge is that it's hard to predict the sign of sin(k*k) for k. The sum of consecutive numbers between n1 and n2 with the specified sign can be positive or negative, large or small, as illustrated here:

Alternative Text

Examples

sum_up(1, 101) ➞ 65

sum_up(200, 911) ➞ -456

Notes

  • The function must be efficient because the intervals are large (be inventive).
  • For comparison, my solution takes 7 seconds to compute all tests, which is still under the server limit of 12 seconds.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.