Answer to Question #221364 in Python for Dion Martins

Question #221364

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


1
Expert's answer
2021-07-29T03:13:36-0400
def sum(n):
  return n * (n + 1) / 2

def main():
  n = 100
  print("sum(1..100)=%d" % sum(n))

if __name__ == '__main__':
  main()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS