Lets first write a program that calculates the n_th power of 2 starting from I i
ID: 3832222 • Letter: L
Question
Lets first write a program that calculates the n_th power of 2 starting from I in terms of (2, 2^1, 2^2, 2^3, ellipsis 2^n). For the integer numbers from 2 to 2^n where 2^n is less than 10000, please calculate and print out their total (total = 2 + 2^1 + 2^2,+2^3, ellipsis 2^n), count and average. Instead of adding up the numbers, now we want to concatenate these numbers to form a new number. For instance, when n = 3, we have three numbers which are 2, 2^2 = 4, 2^3 = 8. Instead of calculating their total: 14 = 2 + 4 + 8, now we concatenate them together to form a new number: 248. Please write a Python program to concatenate n numbers (2, 2^1, 2^2, 2^3 ellipsis 2^n) to form a new number. Continue this concatenation process only when the concatenated number is less than 200000. Please print out the final value of the concatenated number? We still use the integer numbers from 2 to 2^n where 2^n is less than 10000. However, we add one more restriction this time: The number must be an exponentiation of 2 (2^n) but it can't be the exponentiation of 4 (4^n). For example, we will not add number 16 into the total. Despite that 16 is an exponentiation of 2 (2^4), 16 is also an exponentiation of 4 (4^2). Please write a different Python program to calculate and print out the new total, count and average. Submit all three py files hereExplanation / Answer
Question-1:
Code:
#!/usr/bin/python
# script name: power_1.py
# script to get nth power of 2 and calculate total, count and average
# initializing fixed constants
n = 1
total = avg = 0
# iterate until 2^n < 10000
while (2**n < 10000):
total += 2 ** n
n += 1
# printing total, count and average
avg = total/n
print "total = " + str(total)
print "count = " + str(n)
print "average = " + str(avg)
Execution and output:
186590cb0725:Chegg bonkv$ python power_1.py
total = 16382
count = 14
average = 1170
186590cb0725:Chegg bonkv$
Question-2:
Code:
#!/usr/bin/python
# script name: power_2.py
# script to concatenate n numbers instead of adding 2^n values
# initializing constants
n = 1
concat_value = ""
# iterate until 2^n is less than 20000
while ( 2**n < 20000 ):
# concatenating value thats why converting 2**n to string
concat_value = concat_value + str(2**n)
print concat_value
n += 1
print "Final value is: " + str(concat_value)
Execution and output:
186590cb0725:Chegg bonkv$ python power_2.py
2
24
248
24816
2481632
248163264
248163264128
248163264128256
248163264128256512
2481632641282565121024
24816326412825651210242048
248163264128256512102420484096
2481632641282565121024204840968192
248163264128256512102420484096819216384
Final value is: 248163264128256512102420484096819216384
186590cb0725:Chegg bonkv$
Question-3:
Code:
#!/usr/bin/python
# script name: power_3.py
# script to calculate total, count and average only if exponentiation of 2 not equal to exponentiation of 4
# initializing fixed constants
n = 1
total = avg = 0
count = 0
# iterate until 2^n < 10000
while (2**n < 10000):
if ( 2**n != 4**(n/2) ):
total += 2 ** n
count += 1
n += 1
# printing total, count and average
avg = total/count
print "total = " + str(total)
print "count = " + str(count)
print "average = " + str(avg)
Execution and output:
186590cb0725:Chegg bonkv$ python power_3.py
total = 10922
count = 7
average = 1560
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.