1
$\begingroup$

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?

  • 1
    try to calculate first ... numbers and search this sequence in the OEIS2017-02-17
  • 0
    Thanks, but unfortunately it is not there2017-02-17
  • 0
    1. what is the the input parameter? 2. what is the return parameter? Can you write it in C++/Java2017-02-17
  • 1
    What is `pin`? What is `accum` initialised to?2017-02-17
  • 0
    Programmers typically call such a control structure a *loop* rather than a cycle. It would help Readers if you were a bit clearer about what you want the *math formula* to represent. For example, your snippet tells us nothing about how `pin` and `number` are initialized/declared.2017-02-17
  • 0
    Sorry my fault I have edited code, pin was a wrong variable @PatrickStevens. Initially **accum** equals to 0. and **number** initially for example can be equal to 5602004. When **number** = 0, we should save last result of variable **accum**2017-02-17

1 Answers 1

1

The formulation is naturally a recurrence relation: $$f(n) = f \left( \left\lfloor \left\lfloor \frac{n}{10} \right\rfloor \big/ 10 \right\rfloor \right) + 3 (n \ \mathrm{mod}\ {10}) + (\left\lfloor \frac{n}{10} \right\rfloor \ \mathrm{mod} \ 10)$$

This, itself, is most naturally viewed mod $100$:

$$f(n) = f(\text{$n$ without its two rightmost digits}) + 3 \times \text{rightmost digit} + \text{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.