Question

1. Write a Python function that implements the following algorithm that computes the sum of the...

1. Write a Python function that implements the following algorithm that computes the sum of the first n non-negative powers of 2: 20 + 21 + ... + 2n-1, where n ≥ 0:

1. Input n (assume n ≥ 0)

2. Set sum to 1

3. Set value to 2

4. Do the following n-1 times:

   a. Add value to sum

   b. Multiply value by 2

5. Output sum

2.Use turtle to draw two flags of two countries whose names start with the same letters as your first names (A AND H). Use for loop whenever possible

0 0
Add a comment Improve this question Transcribed image text
Answer #1

In case of any query, do comment.

Note: As per the HomeworkLib policy only first question can be answered in case of multiple questions. So providing answer to first question.

Please also see the screen shot of the code for indentation.

Code:

#function to calculate sum of powers

def sumOfPowers(n):

    #set sumPowers to 1

    sumPowers = 1

    #set value to 2

    value = 2

    #iterate from 0 to n-1

    for i in range(n-1):

        #add value to sumPowers

        sumPowers += value

        #multiply the value by 2

        value = value * 2

   

    #output sumPowers

    print("sumOfPowers({}) = {}".format(n, sumPowers))

#main driver function

sumOfPowers(4)

sumOfPowers(6)

sumOfPowers(8)

=======screen shot of the code for indentation and output=====

   

Add a comment
Know the answer?
Add Answer to:
1. Write a Python function that implements the following algorithm that computes the sum of the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT