The sum of the first n positive integers can be computed by the formula
sum(1..n) = 1 + 2 + 3 + 4 + · · · + n = n(n + 1)/2
Write a short Python program that computes the sum of the first 100 positive integers and prints it to the screen, using format specifiers when printing instead of converting each item to a string. Use variables to represent the 1, the 100, and the result of the computation. Your program must compute the 5050 value. You cannot just print the result to the screen. You must compute it first from the 100. The goal is for the output to look exactly the same.The output is as shown below:
sum(1..100) = 5050
m = 1
n = 100
r = n*(n+1)//2
print('sum({:d}..{:d}) = {:d}'.format(m, n, r))
Comments
Leave a comment