Categories

See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

Python cycle to math formula

General Tech Bugs & Fixes
Max. 2000 characters
Replies

usr_profile.png
Liza Sain

User

( 7 months ago )

There is some code snippet written on python:

 

number = 5602004

accum = 0

while number:

    accum += (3 * (number % 10))

    number = int(number / 10)

    accum += (number % 10)

    number = int(number / 10)

So, cycle is working while variable number greater then 0. The question is: can this cycle be presented as math formula?

usr_profile.png
Priyanka Chadda

User

( 7 months ago )

The formulation is naturally a recurrence relation:

f(n)=f(⌊⌊n10⌋/10⌋)+3(n mod 10)+(⌊n10⌋ mod 10)

This, itself, is most naturally viewed mod 100:

 

f(n)=f(n without its two rightmost digits)+3×rightmost digit+next-rightmost digit

If n has evenly-many digits, then: take n as a base 10 string. Take the first, third, fifth… digits' sum. Take the second, fourth, sixth… digits' sum, and multiply by 3. Add the two together.

 

If n has odd-many digits, then just stick a 0 on the front and pretend it has evenly-many.

what's your interest


forum_ban8_5d8c5fd7cf6f7.gif