Can you reach this number by doubling and rearranging?

34

2

Inspired by this question on Math.SE.

Starting with 1 you can repeatedly perform one of the following two operations:

  • Double the number.

    or

  • Rearrange its digits in any way you want, except that there must not be any leading zeroes.

Taking an example from the linked Math.SE post, we can reach 1000 via the following steps:

1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 125, 250, 500, 1000

Which numbers can you reach with this process and what is the shortest solution?

The Challenge

Given a positive integer N, determine the shortest possible sequence of integers to reach N with the above process, if possible. If there are several optimal solutions, output any one of them. If no such sequence exists, you should output an empty list.

The sequence may be in any convenient, unambiguous string or list format.

You may write a program or function, taking input via STDIN (or closest alternative), command-line argument or function argument and outputting the result via STDOUT (or closest alternative), function return value or function (out) parameter.

This is code golf, so the shortest answer (in bytes) wins.

Test Cases

Here is a list of all reachable numbers up to and including 256. The first column is the number (your input), the second column is the optimal number of steps (which you can use to check the validity of your solution) and the third column is one optimal sequence to get there:

1       1       {1}
2       2       {1,2}
4       3       {1,2,4}
8       4       {1,2,4,8}
16      5       {1,2,4,8,16}
23      7       {1,2,4,8,16,32,23}
29      10      {1,2,4,8,16,32,23,46,92,29}
32      6       {1,2,4,8,16,32}
46      8       {1,2,4,8,16,32,23,46}
58      11      {1,2,4,8,16,32,23,46,92,29,58}
61      6       {1,2,4,8,16,61}
64      7       {1,2,4,8,16,32,64}
85      12      {1,2,4,8,16,32,23,46,92,29,58,85}
92      9       {1,2,4,8,16,32,23,46,92}
104     15      {1,2,4,8,16,32,64,128,256,512,125,250,205,410,104}
106     14      {1,2,4,8,16,32,64,128,256,265,530,305,610,106}
107     14      {1,2,4,8,16,32,23,46,92,29,58,85,170,107}
109     18      {1,2,4,8,16,32,23,46,92,184,368,386,772,277,554,455,910,109}
116     12      {1,2,4,8,16,32,23,46,92,29,58,116}
122     7       {1,2,4,8,16,61,122}
124     16      {1,2,4,8,16,32,23,46,92,29,58,85,170,107,214,124}
125     11      {1,2,4,8,16,32,64,128,256,512,125}
128     8       {1,2,4,8,16,32,64,128}
136     18      {1,2,4,8,16,32,23,46,92,184,148,296,592,259,518,158,316,136}
140     15      {1,2,4,8,16,32,64,128,256,512,125,250,205,410,140}
142     16      {1,2,4,8,16,32,23,46,92,29,58,85,170,107,214,142}
145     17      {1,2,4,8,16,32,23,46,92,184,368,736,376,752,257,514,145}
146     18      {1,2,4,8,16,32,64,128,256,512,125,250,205,410,104,208,416,146}
148     11      {1,2,4,8,16,32,23,46,92,184,148}
149     16      {1,2,4,8,16,32,64,128,182,364,728,287,574,457,914,149}
152     11      {1,2,4,8,16,32,64,128,256,512,152}
154     17      {1,2,4,8,16,32,23,46,92,184,368,736,376,752,257,514,154}
158     16      {1,2,4,8,16,32,23,46,92,184,148,296,592,259,518,158}
160     14      {1,2,4,8,16,32,64,128,256,265,530,305,610,160}
161     13      {1,2,4,8,16,32,23,46,92,29,58,116,161}
163     18      {1,2,4,8,16,32,23,46,92,184,148,296,592,259,518,158,316,163}
164     18      {1,2,4,8,16,32,64,128,256,512,125,250,205,410,104,208,416,164}
166     20      {1,2,4,8,16,32,23,46,92,184,368,736,376,752,257,514,154,308,616,166}
167     17      {1,2,4,8,16,32,23,46,92,184,148,296,269,538,358,716,167}
169     23      {1,2,4,8,16,32,64,128,256,512,125,250,205,410,104,208,416,461,922,229,458,916,169}
170     13      {1,2,4,8,16,32,23,46,92,29,58,85,170}
176     17      {1,2,4,8,16,32,23,46,92,184,148,296,269,538,358,716,176}
182     9       {1,2,4,8,16,32,64,128,182}
184     10      {1,2,4,8,16,32,23,46,92,184}
185     16      {1,2,4,8,16,32,23,46,92,184,148,296,592,259,518,185}
188     23      {1,2,4,8,16,32,23,46,92,184,148,296,592,259,518,185,370,740,470,940,409,818,188}
190     18      {1,2,4,8,16,32,23,46,92,184,368,386,772,277,554,455,910,190}
194     16      {1,2,4,8,16,32,64,128,182,364,728,287,574,457,914,194}
196     23      {1,2,4,8,16,32,64,128,256,512,125,250,205,410,104,208,416,461,922,229,458,916,196}
203     16      {1,2,4,8,16,32,64,128,256,265,530,305,610,160,320,203}
205     13      {1,2,4,8,16,32,64,128,256,512,125,250,205}
208     16      {1,2,4,8,16,32,64,128,256,512,125,250,205,410,104,208}
209     19      {1,2,4,8,16,32,23,46,92,184,368,736,376,752,257,514,145,290,209}
212     8       {1,2,4,8,16,61,122,212}
214     15      {1,2,4,8,16,32,23,46,92,29,58,85,170,107,214}
215     11      {1,2,4,8,16,32,64,128,256,512,215}
218     9       {1,2,4,8,16,32,64,128,218}
221     8       {1,2,4,8,16,61,122,221}
223     14      {1,2,4,8,16,32,23,46,92,29,58,116,232,223}
227     20      {1,2,4,8,16,32,23,46,92,184,148,296,592,259,518,158,316,361,722,227}
229     20      {1,2,4,8,16,32,64,128,256,512,125,250,205,410,104,208,416,461,922,229}
230     16      {1,2,4,8,16,32,64,128,256,265,530,305,610,160,320,230}
232     13      {1,2,4,8,16,32,23,46,92,29,58,116,232}
233     22      {1,2,4,8,16,32,23,46,92,184,368,736,376,752,257,514,154,308,616,166,332,233}
235     19      {1,2,4,8,16,32,23,46,92,184,148,296,269,538,358,716,176,352,235}
236     19      {1,2,4,8,16,32,23,46,92,184,148,296,592,259,518,158,316,632,236}
238     19      {1,2,4,8,16,32,64,128,256,512,125,250,205,410,104,208,416,832,238}
239     25      {1,2,4,8,16,32,23,46,92,184,368,736,376,752,257,514,154,308,616,166,332,233,466,932,239}
241     16      {1,2,4,8,16,32,23,46,92,29,58,85,170,107,214,241}
244     8       {1,2,4,8,16,61,122,244}
247     21      {1,2,4,8,16,32,23,46,92,184,148,296,592,259,518,158,316,632,362,724,247}
248     17      {1,2,4,8,16,32,23,46,92,29,58,85,170,107,214,124,248}
250     12      {1,2,4,8,16,32,64,128,256,512,125,250}
251     11      {1,2,4,8,16,32,64,128,256,512,251}
253     19      {1,2,4,8,16,32,23,46,92,184,148,296,269,538,358,716,176,352,253}
256     9       {1,2,4,8,16,32,64,128,256}

If you want even more test data, here is the same table up to and including 1,000.

Any number not appearing on these tables should yield an empty list (provided the number is in the range of the table).

Martin Ender

Posted 2015-08-19T08:38:05.383

Reputation: 184 808

Are there any bounds on the execution time? – Fatalize – 2015-08-19T09:30:08.993

2@Fatalize no, go crazy. – Martin Ender – 2015-08-19T09:30:26.230

I suppose potentially infinite execution time is not acceptable though? It has to theoretically terminate? – Fatalize – 2015-08-19T09:32:57.903

@Fatalize Ah yes, as usual.

– Martin Ender – 2015-08-19T09:33:57.397

What about when there is more than one result: [1, 2, 4, 8, 16, 32, 64, 46, 92, 29] [1, 2, 4, 8, 16, 32, 23, 46, 92, 29] – dbramwell – 2015-08-20T17:03:58.627

Unfortnately i have no access to a pc right now, so I probably won't add any submission before tomorrow. But would it be legit to reverse the process by starting at N, either multiplying by .5 or reordering the digits? I would think this might open some benefits like a natural boundary as, as soon in a path were N equals 3 it's invalid and you can stop. Or am I missing something crucial there? Also - I assume reordering has to happen in base 10, like, can I cycle the bits of a base 2 representation of same N to perform a reordering? - of course without leading zeroes – C5H8NNaO4 – 2015-08-20T17:15:38.640

@user1147618 "If there are several optimal solutions, output any one of them." – Martin Ender – 2015-08-20T17:57:23.333

@C5H8NNaO4 the permutation has to be done in base 10. It doesn't matter if you search forwards or backwards as long as you output the result forwards. – Martin Ender – 2015-08-20T17:58:50.910

Answers

18

Pyth, 43 bytes

?}QKhu?Jf}QTGJsm+Ld+yedsMfnhT\0.p`edGQ]]1KY

Demonstration.

This is starts by generating all possible double or rearrange sequences. However, since I actually wanted to see it finish, I added a short circuit.

It either runs until it finds a solution, or for a number of iterations equal to the input, at which point it gives up and returns [].


This is guaranteed to be enough iterations. First, we know that this many iterations is enough for all n <= 1000, thanks to the example results. For larger numbers, the following argument holds:

First, each step of the process must either maintain or increase the number of digits.

Second, three numbers that are all rearrangements of one another can never all appear in a shortest sequence, because it would have been quicker to just do a single rearrangement from the first to the last.

Third, all multiples of 3 are unreachable, because neither doubling nor rearrangement can produce a multiple of 3 from a non-multiple of 3.

Thus, the longest possible sequence ending at a given number is equal to the sum of twice the number of sets of digits with less than or equal to as many digits as the input, and where the digits do not sum to a multiple of 3.

The number of such digit sets for each number of digits:

4 - 474
5 - 1332
6 - 3330

In addition, we know from the examples that no shortest sequence ending with a 3 digit number is of length greater than 26. Thus, an upper bound of the sequence lengths is:

4: 474 * 2 + 26 = 974
5: 974 * 2 + 1332 = 3638
6: 3330 * 2 + 3638 = 10298

In each case, the upper bound is lower than any number with the number of digits

The number of digit sets cannot grow by more than a factor of 10 when the number of digits is increased by one, because the new numbers can be separated into groups by last digit, each of which cannot have more sets than there were with one fewer digit.

Thus, the upper bound will be lower than any number with that many digits for all possible numbers of digits greater than or equal to 4, which completes the proof that a number of iterations equal to the input is always enough.

isaacg

Posted 2015-08-19T08:38:05.383

Reputation: 39 268

Are you sure a number of iterations equal to the input is enough? In theory wouldn't the upper bound be around the next greater power of 10 (since the sequence can decrease arbitrarily often). – Martin Ender – 2015-08-19T10:26:24.797

@MartinBüttner Good point. I think there should be a proof that the input is always enough, but I'll edit it for now. – isaacg – 2015-08-19T10:27:59.710

@MartinBüttner Proof that iterations equal to input is always enough added. – isaacg – 2015-08-19T10:59:02.217

Ah, very nice. :) (Interestingly, even up to 100,000 you don't need more than 26 steps.) – Martin Ender – 2015-08-19T11:01:21.740

I believe it would be faster to enumerate all steps not longer than the input? – John Dvorak – 2015-08-19T13:04:12.200

I can't get 136 to work online. Well, 3 doesn't work either. – aditsu quit because SE is EVIL – 2015-08-19T22:42:03.510

@aditsu 136 would be over the timeout. 3 should work now - fixed the link. – isaacg – 2015-08-20T05:27:43.680

7

SWI-Prolog, 252 bytes

a(N,Z):-findall(X,b(N,[1],X),R),sort(R,[Z|_]);Z=[].
b(N,[A|T],Z):-n(A,C),n(N,M),length(C,L),length(M,O),L=<O,((A=N,reverse([A|T],Z));(A\=N,(B is A*2;permutation(C,D),\+ nth0(0,D,48),n(B,D),\+member(B,[A|T])),b(N,[B,A|T],Z))).
n(A,B):-number_codes(A,B).

Example: a(92,Z). outputs Z = [1, 2, 4, 8, 16, 32, 64, 46, 92]

I haven't checked yet that this works for N > 99 because of the time it takes, but I see no reason why it wouldn't work.

Fatalize

Posted 2015-08-19T08:38:05.383

Reputation: 32 976

2

Julia, 306 245 218 bytes

Still working on golfing this. Will provide an ungolfed version once I'm done.

s->(M=s=[s];while 1∉s C=0;for i=1:size(s,1) p=2;for j=permutations(dec(s[i])) j[1]>48&&j[end]%2<1&&(l=int(j);l=l÷p;l∉M&&(M=[M,l];S=[l s[i,:]];C==0?C=S:C=[C;S]));p=1end;end;C==0&&return [];s=C;end;sortrows(s)[1,:])

Glen O

Posted 2015-08-19T08:38:05.383

Reputation: 2 548

1

Haskell, 246 bytes

I am not entirely sure if this works, it does iff the sequence that first diverges lower (as to be sorted lower) is always shorter, as for example

[1,2,4,8,16,32,64,128,256,512,125,250,500,1000]

is shorter than

[1,2,4,8,16,32,64,128,256,512,251,502,250,500,1000]

which i tested to be true up to 1000.

import Data.List
h l|mod(e l)2==0=l:h(div(e l)2:l)|0<1=[l]
s l=map((:l).read)(filter((/='0').e)(permutations$show$e l))
e=head
m=map e
n f=m.groupBy(\a b->e a==e b).sort.concatMap f
w l|e(e l)==1=[nub$e l]|m x/=m l=w x|0<1=[[]] where x=n h(n s l)

Leif Willerts

Posted 2015-08-19T08:38:05.383

Reputation: 1 060

1

C# 655 bytes

List<int> C(int i,List<int> x,int a){x.Add(a);if(a==i)return x;List<int> o=null;string s=a.ToString(),m=i.ToString();var res=G(s,s.Length);foreach (var r in res)if (r.First()!='0'){var l=int.Parse(new String(r.ToArray()));if(!x.Contains(l)&&l.ToString().Length<=m.Length){var n=C(i,x.ToList(),l);if(n!=null&&(o==null||o.Count()>n.Count()))o=n;}}if ((a*2).ToString().Length>m.Length)return o;var p = C(i, x.ToList(), a * 2);if (p!=null&&(o==null||o.Count()>p.Count()))o=p;return o;}IEnumerable<IEnumerable<T>> G<T>(IEnumerable<T> l,int i){return i==1?l.Select(t =>new T[]{t}):G(l,i-1).SelectMany(t=>l.Where(e=>!t.Contains(e)),(a,b)=>a.Concat(new T[]{b}));}

Call with (LinqPad):

var i = 64;
C(i,new List<int>(),1).Dump();

Haven't tested numbers above 99. If you have time -> good luck ;-)

edit: ungolfed version:

List<int> C(int i, List<int> x, int toAdd, bool removeLast)
{
    x.Add(toAdd);

    if ( toAdd == i )
    {
        return x;
    }
    else
    {
        List<int> shortest = null;
        if ( toAdd > 9 )
        {
            var res = G(toAdd.ToString(), toAdd.ToString().Length);

            foreach ( var r in res )
            {
                if ( r.First () != '0' )
                {
                    var resi = int.Parse(new String(r.ToArray()));

                    if ( !x.Contains(resi) && resi.ToString().Length <= i.ToString().Length )
                    {
                        var resPerm = C(i, x.ToList(), resi, false);
                        if ( resPerm != null )
                        {
                            if ( shortest == null || shortest.Count() > resPerm.Count() )
                            {
                                shortest = resPerm;
                            }
                        }
                    }
                }
            }
        }
        if ( (toAdd * 2).ToString().Length > i.ToString().Length )
        {
            return shortest;
        }
        var resDouble = C(i, x.ToList(), toAdd * 2, false);
        if ( resDouble != null )
        {
            if ( shortest == null || shortest.Count() > resDouble.Count() )
            {
                shortest = resDouble;
            }
            return shortest;
        }

        return shortest;
    }
}
IEnumerable<IEnumerable<T>> G<T>(IEnumerable<T> l,int i)
{
    return i==1?l.Select(t => new T[]{t}):G(l,i-1).SelectMany(t=>l.Where(e=>!t.Contains(e)),(a,b)=>a.Concat(new T[]{b}));
}

Stephan Schinkel

Posted 2015-08-19T08:38:05.383

Reputation: 596

0

CJam, 83

ri:N_1a:Xa:Y;{X,{XI=__se!{c~},:i^\2*|NA*,&X-_YI=f+Y\+:Y;X\+:X;}fI}*X#_){Y=W%}{;L}?p

Try it online

I've been sitting on this for a long time, it's not very short nor fast, and I'm not sure I have the energy/motivation to improve it, so I'm just posting it.

aditsu quit because SE is EVIL

Posted 2015-08-19T08:38:05.383

Reputation: 22 326