fork download
  1. def calculate_sum(n):
  2. # Calculate the sum of numbers from 1 to n
  3. total_sum = (n * (n + 1)) // 2
  4.  
  5. # Subtract all the 2nd values from the sum
  6. if n % 2 == 0:
  7. total_sum -= (n // 2) * (n // 2 + 1)
  8. else:
  9. total_sum -= ((n - 1) // 2) * ((n - 1) // 2 + 1)
  10.  
  11. return total_sum
  12.  
  13. try:
  14. # Read the number of test cases
  15. t = int(input())
  16.  
  17. # Process each test case
  18. for _ in range(t):
  19. n = int(input())
  20. result = calculate_sum(n)
  21. print(result)
  22.  
  23. except ValueError:
  24. print("Invalid input. Please enter valid integers.")
  25. except ZeroDivisionError:
  26. print("Error: Division by zero occurred.")
  27. except Exception as e:
  28. print("An error occurred:", e)
  29.  
Success #stdin #stdout 0.02s 9828KB
stdin
2
4
1000000000
3
1
5
10
1
15
stdout
4
250000000000000000