Question: Python 3: Write a function n Sum which takes one input, n , and finds the sum: n + nn + nnn + nnnn +

Python 3: Write a function nSum which takes one input, n, and finds the sum:

n + nn + nnn + nnnn + ... up to n digits of n.

For example:

>>> print (nSum( 1 ))

1

Which is 1 = 1.

>>> print (nSum( 2 ))

24

Which is 2 + 22 = 24.

>>> print (nSum( 3 ))

369

Which is 3 + 33 + 333 = 369.

>>> print (nSum( 4 ))

4936

Which is 4 + 44 + 444 + 4444 = 4936

>>> print (nSum( 5 ))

61725

Which is 5 + 55 + 555 + 5555 + 55555 = 61725.

Hint: think about each element of the sum (3, 33, 333, for example) represented as a sum of powers of 10:

333 = 3 * 10^0 + 3 * 10^1 + 3 * 10^2 = 3 + 30 + 300

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!