# Rotations of a number

If we start with a number like 1234 and produce the following sum 1234 + 123 + 12 + 1 = 1370.

If we are given the 1370 can I retrieve the 1234? A similar question was migrated over to the Math.SE because the OP did not in any way relate it to MMa. The math given over there is in no way too tough for anyone over here but the solution will still require MMA for anything more than a trivial problem. I wish to do it using MMa. This is what I did:

Suppose we have the non trivial problem of reconstructing the original number from 308460277.

Basically, I just used the structure of the FindInstance command as a template and used MMa commands to fill in the various fields.

n = 9; (*digit length of the number*)
m = 308460277;
var = Table[Subscript[a, k], {k, n, 1, -1}];
f = Sum[1/9 (-1 + 10^k) Subscript[a, k], {k, 1, n}] == m;
s = FindInstance[Append[{f, 10 > Subscript[a, 1] > 0},
Table[10 > Subscript[a, k] >= 0, {k, 2, n}]] // Flatten, var,Integers];
var /. s

(*{{2, 7, 7, 6, 1, 4, 2, 5, 3}}*)


So the starting number was 277614253

To check:

277614253 + 27761425 + 2776142 + 277614 + 27761 + 2776 + 277 + 27 + 2 = 308460277

This is pretty fast and can do 98766665555567902460 instantaneously.

What is the right way to do this?

Addenda: My solution gags on m = 137174210013717421001371742085. I was worried about FindInstance being able to do larger ones, that is the reason I posted for a better way.

A ploddingly procedural implementation:

n = 308460277;
FromDigits[Reap[Do[{q, n} = QuotientRemainder[n, (10^k - 1)/9]; Sow[q],
{k, IntegerLength[n], 1, -1}]][[-1, 1]]]
277614253

• george2079 and yours do not gag on m = 137174210013717421001371742085 and mine does +1 Oct 13, 2016 at 22:15

This is a direct implementation of this answer https://math.stackexchange.com/a/1967330/92921

m = 308460277;
Reap[NestWhile[{#[] - #[] Sow@Floor[Divide @@ #]],
Floor[#[]/10]} &, {m,
FromDigits@ConstantArray[1, Ceiling@Log[10, m]]} , #[] >
0 &]][[2, 1]] // FromDigits


277614253

note this gives the 'closest' value for any input m. You need to check that its correct:

 Total@NestWhileList[ Floor[#/10] &, 277614253, # > 0 &]==m


True

I'm not sure of the etiquette re: reposting here vs giving code answers on the math site..

• I was not sure of the correctness either, I apologize to all. Good answer though +1 Oct 13, 2016 at 22:08