13
\$\begingroup\$

The oldest Polish salt mine, located in Bochnia*, was started in year 1248, which we can consider a magical number. We can see that it's equal to 4 digits from the sequence of exponentiations: 2^0, 2^1, 2^2, 2^3.

As the date is actually 4 digits from the sequence, we could make it longer. We could repeat the process until we reach infinity. The sequence would look like this, if we limit it to number 2048

124816326412825651210242048

To make it look a bit better, we can separate the numbers:

1|2|4|8|16|32|64|128|256|512|1024|2048

Let's try a custom, longer sequence than the date. Let's say, we want it to have 5 digits - there are more than one possibility:

  • 24816
  • 81632
  • 64128

Or 3 digit ones:

  • 124
  • 248
  • 816

We could also add the 3 digit numbers to this, but let's say, that a sequence must have at least two numbers.

* There is no information about this on the English Wikipedia. If you enter the Polish version - then there is. If you visit the mine, the workers will also tell you, that it started in 1248.

The challenge

Create a exponentiation sequence like in examples above with 2 as the base.

Given a number from range 2-27, output all possible parts of the sequence (The 2048 one or larger if you want) with amount of digits equal to the input. You cannot cut a number, so output like 481 is invalid, because 16 is cut in half.

Rules:

  • Standard loopholes are forbidden.
  • You can assume the input is a number inside the range.
  • Your program can accept inputs larger than the range (28+), but that won't increase/decrease score.
  • Spaces in output are ignored. You can output like 124 or like 4 8 16.
  • Different possibilities should be separated by any character from the list: ,./| or a line feed.
  • You can output as an array.
  • Every possibility should include at least 2 different numbers.
  • You must output a part of the sequence, you cannot mix numbers that aren't next to each other, like: 14.
  • Hardcoded output isn't allowed, however, you can hardcode a string/number/array containing the full sequence.
  • Input 27 should return the full 2048 sequence.
  • As already mentioned before, do not cut numbers. Ex. 16 must stay 16 - you can't use 481 - you must use 4816.
  • EDIT: I might have said something wrong there; 2048 is the last number which your program should support, you can add support for larger int's.

Test cases

Input: 2

12, 24, 48

Input: 3

124, 248, 816

Input: 4

1248, 4816, 1632, 3264

Input: 5

24816, 81632, 64128

Input: 27

124816326412825651210242048

And later numbers...

If I made a mistake in any of the test cases, tell me or edit the question.


This is , so the shortest code in bytes wins!

\$\endgroup\$
  • 1
    \$\begingroup\$ So this is only with 2 as the base, correct? Could you clarify that in the question? I'm not sure if it's implied by "Sequence of Exponentiations," but even if it is, I'm sure there are people out there like me who don't know that. \$\endgroup\$ – cole Sep 12 '17 at 16:56
  • \$\begingroup\$ @cole Actually, yes, it's only with 2. Thanks for mentioning! \$\endgroup\$ – RedClover Sep 12 '17 at 16:56
  • 1
    \$\begingroup\$ Can output be separated by newline? \$\endgroup\$ – H.PWiz Sep 12 '17 at 17:23
  • 1
    \$\begingroup\$ No worries; like I said, I was pushing it. Some challenge authors can be incredibly flexible on the output format so, for the sake of a byte or 2, it's worth asking ;) (Note: That should not be interpreted as a suggestion!) \$\endgroup\$ – Shaggy Sep 13 '17 at 22:27
  • 1
    \$\begingroup\$ In the intro, you should capitalize Polish. "polish" is a different English word. \$\endgroup\$ – Peter Cordes Sep 17 '17 at 3:45

16 Answers 16

7
\$\begingroup\$

05AB1E, 12 11 10 bytes

Supports the sequence up to 2^95 = 39614081257132168796771975168

₃ÝoŒʒg≠}Jù

Try it online!

Explanation

₃Ý            # push range [0 ... 95]
  o           # raise 2 to the power of each
   Π         # get a list of all sublists
    ʒ         # filter, keep elements that satisfy:
     g        # length
      ≠       # false (not equal to 1)
       }      # end filter
        J     # join each
         ù    # keep numbers of length matching the input

Saved 1 byte thanks to Erik the Outgolfer
Saved 1 byte thanks to Riley

\$\endgroup\$
  • \$\begingroup\$ X› can be \$\endgroup\$ – Erik the Outgolfer Sep 13 '17 at 12:31
  • \$\begingroup\$ @EriktheOutgolfer: Oh yeah. I always forget that one exist now. Thanks :) \$\endgroup\$ – Emigna Sep 13 '17 at 12:41
  • \$\begingroup\$ Can Y₃Ým be ₃Ýo? \$\endgroup\$ – Riley Sep 13 '17 at 13:35
  • \$\begingroup\$ @Riley: Yep. I had that before, but for some reason I must have changed it. Thanks :) \$\endgroup\$ – Emigna Sep 13 '17 at 13:46
  • \$\begingroup\$ Trying the code now (much late after challenge end)... and your solution seems to return tons of empty arrays for me... Am I doing something wrong? \$\endgroup\$ – RedClover Nov 13 '17 at 19:33
6
\$\begingroup\$

Pyth, 22 21 20 17 bytes

fqQlTjLkt#.:^L2yT

Try it Online

Explanation

fqQlTjLkt#.:^L2yT
            ^L2yT  Get the powers of 2 up to 2^20
        t#.:       Get all consecutive sequences of at least 2
     jLk           Concatenate each
fqQlT              Get the ones whose length is the input
\$\endgroup\$
4
\$\begingroup\$

Jelly,  19 18  16 bytes

There may be a shorter solution now that we may use any cut-off (not just 2048), although this change to the specification has allowed a one byte save from this implementation by moving to a cut-off of 32768.
--yep...

-2 bytes thanks to Erik the Outgolfer (use of V to allow implicit right argument of the filter and tightening)
--yes it is very similar to his inefficient one now; go upvote his!

⁴Ḷ2*Ẇṫ17VDL$⁼¥Ðf

A monadic link taking a number and returning a list of numbers.

Try it online!

How?

⁴Ḷ2*Ẇṫ17VDL$⁼¥Ðf - Link: number, n        e.g. 3
⁴                - literal sixteen             16
 Ḷ               - lowered range               [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
  2              - literal two                 2
   *             - exponentiate                [1,2,4,8,16,32,...,32768]
    Ẇ            - all sublists                [[1],[2],...,[1,2],[2,4],...,[1,2,4],...]
      17         - literal seventeen           17
     ṫ           - tail from index             [[1,2],[2,4],...,[1,2,4],...]]
        V        - evaluate as Jelly code      [12,24,...,124,...]
              Ðf - filter keep:
             ¥   -   last two links as a dyad
           $     -     last two links as a monad:
         D       -       decimal list (of entry) (i.e. 816 -> [8,1,6] or 24 -> [2,4])
          L      -       length                  (i.e. 816 -> 3, or 24 -> 2)
            ⁼    -   equals (n)                  (i.e. 816 -> 1, or 24 -> 0)
                 - ...resulting in             [816, 124, 248]
\$\endgroup\$
  • 1
    \$\begingroup\$ Is this too similar to yours? (please be honest :p) \$\endgroup\$ – Erik the Outgolfer Sep 12 '17 at 18:47
  • \$\begingroup\$ Funnily enough I was just trying to use V and it'll work for 16 rather than 1000: ⁴Ḷ2*Ẇṫ17VDL$⁼¥Ðf. \$\endgroup\$ – Jonathan Allan Sep 12 '17 at 18:52
  • \$\begingroup\$ I'm going for the most :p \$\endgroup\$ – Erik the Outgolfer Sep 12 '17 at 18:52
  • \$\begingroup\$ @EriktheOutgolfer they may now be similar but I am of the mind we should both keep them I'm sure you came up with yours independently and I'm sure I'd have found the eval trick too (since I was looking at exactly that, just needed to get the chaining right). \$\endgroup\$ – Jonathan Allan Sep 12 '17 at 19:09
  • \$\begingroup\$ @EriktheOutgolfer I have made the assumption you are male, and prefer to be referred to as such, but actually do not know either as fact; do let me know if you prefer a different pronoun! \$\endgroup\$ – Jonathan Allan Sep 12 '17 at 19:13
4
\$\begingroup\$

Perl 6, 62 59 bytes

{grep *.comb==$_,map {[~] 2 X**[...] $_},combinations 12,2}

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Japt, 22 20 19 16 bytes

Supports input up to 639 but gaps start appearing in the sequence after 234 (See the full list of supported input ranges here). Outputs an array of strings.

IÆIo!²ãX m¬lUäc

Test it

I (64) could be replaced with L (100) but we'd be getting into scientific notation and precision inaccuracies. Filtering those out would, obviously, increase the byte count and only raise the maximum input to 736.

                     :Implicit input of integer U
I                    :64
 Æ                   :Map each X in [0,64)
  Io                 :  Range [0,64)
    !²               :  Raise 2 to the power of each
      ãX             :  Subsections of length X
         m           :  Map
          ¬          :    Join
           lU        :  Filter elements of length U
             Ã       :End map
              ¤      :Slice off the first 2 elements
               c     :Flatten
\$\endgroup\$
3
\$\begingroup\$

Python 2, 105 bytes

lambda l,r=range:[x for x in[''.join(`2**n`for n in r(i,j+2))for i in r(13)for j in r(i,11)]if len(x)==l]

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Husk, 18 17 bytes

Output is separated by newlines

fo=⁰LmṁsftQ↑12¡D1

Try it online!

How?

           ↑12¡D1    The sequence [1,2,4...2048]
              ¡      Repeatedly apply function, collecting results in a list
               D     double
                1    initially applying to 1
           ↑12       Take the first 12 elements
          Q          Get all sublists
        ft           With a length greater than 1
     mṁs             Convert each list into a string, e.g [4,8,16] -> "4816"
fo=⁰L                Keep only those whose length is equal to the input
\$\endgroup\$
3
\$\begingroup\$

Jelly, 16 bytes

ȷḶ2*ẆṫȷḊVDL$⁼¥Ðf

Try it online!

Note: very inefficient. Returns a list of numbers.

\$\endgroup\$
  • \$\begingroup\$ Tio doesn't seem to be able to parse this code... It always exceeds 60 seconds... \$\endgroup\$ – RedClover Sep 12 '17 at 18:50
  • 1
    \$\begingroup\$ @Soaku Try replacing the ȷs (i.e. 1000) with 20s (lower upper limit). \$\endgroup\$ – Erik the Outgolfer Sep 12 '17 at 18:51
  • 1
    \$\begingroup\$ @Soaku it works in theory - it just times out because it is very inefficient. \$\endgroup\$ – Jonathan Allan Sep 12 '17 at 18:53
  • 1
    \$\begingroup\$ @Soaku I meant on my comment. I have already replaced them there, and there's output [12, 24, 48]. \$\endgroup\$ – Erik the Outgolfer Sep 12 '17 at 18:55
  • 1
    \$\begingroup\$ @Soaku Why not go for the most if you can without extra cost? ;) \$\endgroup\$ – Erik the Outgolfer Sep 13 '17 at 10:53
3
\$\begingroup\$

JavaScript (ES7), 102 100 bytes

Prints all matching sub-sequences with alert().

l=>[...1e11+''].map((_,k,a)=>a.map((_,x)=>(s=(g=n=>x<=k|n<k?'':g(n-1)+2**n)(x)).length-l||alert(s)))

Demo

NB: This snippet is buffering the results and printing them to the console for user-friendliness.

let f =

l=>[...1e11+''].map((_,k,a)=>a.map((_,x)=>(s=(g=n=>x<=k|n<k?'':g(n-1)+2**n)(x)).length-l||alert(s)))

alert = s => res += ' ' + s;

for(l = 2; l <= 27; l++) {
  res = '';
  f(l);
  console.log('L = ' + l + ' -->' + res);
}

\$\endgroup\$
3
\$\begingroup\$

Haskell, 72 67 bytes

f n=[s|i<-[0..99],j<-[i+1..99],s<-[show.(2^)=<<[i..j]],length s==n]

Try it online!

Saved 5 bytes thanks to Laikoni

I used a limit of 99 because 2^99 has a length > 27.

\$\endgroup\$
  • \$\begingroup\$ It's returning extra cases for an input of 27 though. \$\endgroup\$ – Jonathan Allan Sep 12 '17 at 18:10
  • \$\begingroup\$ You can replace 99 with 11, so it becomes more valid. Although, I did not state that numbers > 2048 are invalid. I stated only, that 2048 is the minimum range. \$\endgroup\$ – RedClover Sep 12 '17 at 18:13
  • \$\begingroup\$ @JonathanAllan I think it's still correct: "The 2048 one or larger if you want" I took the 633825300114114700748351602688 sequence, because it guarantees that there won't be any other solution (in the range 2-27). Actually, I think a 45 limit would be sufficient, because length$(show$2^44)++(show$2^45)==28. \$\endgroup\$ – jferard Sep 12 '17 at 18:27
  • \$\begingroup\$ @jferard that was actually edited in after your answer (it actually stated "limited to 2048" before). I can shorten mine in this case too. \$\endgroup\$ – Jonathan Allan Sep 12 '17 at 18:30
  • 1
    \$\begingroup\$ @JonathanAllan Yes, I actually realised that some of rules in the question are wrong and misleading just after posting this anwer. \$\endgroup\$ – RedClover Sep 12 '17 at 18:41
2
\$\begingroup\$

Mathematica, 122 bytes

(s=#;FromDigits@F[f/@#]&/@Select[Subsequences[Array[2^#&,99,0]],l@#>1&&(l=Length)@(F=Flatten)[(f=IntegerDigits)/@#]==s&])&  


Input

[27]

Output

{879609302220817592186044416, 134217728268435456536870912, 524288104857620971524194304, 163843276865536131072262144, 204840968192163843276865536, 256512102420484096819216384, 641282565121024204840968192, 163264128256512102420484096, 124816326412825651210242048}

Input [1000]
Output  1441151880758558722882303761517117445764607523034234881152921504606846976230584300921369395246116860184273879049223372036854775808184467440737095516163689348814741910323273786976294838206464147573952589676412928295147905179352825856590295810358705651712118059162071741130342423611832414348226068484722366482869645213696944473296573929042739218889465931478580854784377789318629571617095687555786372591432341913615111572745182864683827230223145490365729367654460446290980731458735308812089258196146291747061762417851639229258349412352483570327845851669882470496714065569170333976494081934281311383406679529881638685626227668133590597632773712524553362671811952641547425049106725343623905283094850098213450687247810566189700196426901374495621121237940039285380274899124224247588007857076054979824844849517601571415210995964968969903520314283042199192993792198070406285660843983859875843961408125713216879677197516879228162514264337593543950336158456325028528675187087900672316912650057057350374175801344
\$\endgroup\$
2
\$\begingroup\$

C, 170 bytes

i,j;f(n){char t[99],s[12][5]={"1"};for(i=j=1;i<12;)sprintf(s+i++,"%d",j*=2);for(i=0;i<12;++i,strlen(t)-n||j>1&&puts(t))for(j=*t=0;strlen(t)<n&&j+i<12;)strcat(t,s+i+j++);}

Try it online!

Unrolled:

i,j;
f(n)
{
    char t[99], s[12][5] = {"1"};
    for (i=j=1; i<12;)
        sprintf(s+i++, "%d", j*=2);
    for (i=0; i<12; ++i, strlen(t)-n || j>1 && puts(t))
        for (j=*t=0; strlen(t)<n && j+i<12;)
            strcat(t, s+i+j++);
}
\$\endgroup\$
1
\$\begingroup\$

R, 99 bytes

function(n)for(i in 1:11)for(j in i:11+1)if(sum(nchar(x<-2^(0:11))[i:j])==n)cat(x[i:j],"\n",sep="")

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Perl 5, 76 bytes

75 bytes of code + 1 for -a

for$i(0..10){$/='',(map$/.=2**$_,$i..$_)&&$F[0]-length$/||say$/for$i+1..11}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Japt, 24 bytes

Don't upvote

Now I realised that it's the same way of doing this as @Shaggy did, just less golfed. (Should I remove the answer?)

After some long time since posting this question, I learned my first golfing language. Because of this, I decided to try my luck here.

2oI,@IÆ2pYÃãX ®q
c f_Ê¥N

Try it online!

The score isn't the best, it's not even good, but it took me a lot of time to do this .-.

I lose a lot of score, because for some reasons, ã can only return x length arrays... It could be even ~10 bytes, if not that.

Explanation:

2oI,@IÆ2pYÃãX ®q # All 2^ combinations:
2oI              # Range 2-64
   ,@            # Map (X as index)
     IÆ          #   Range 0-64, map (Y as index)
       2pY       #   2^Y
          Ã      #   End function (map)
                 #   this = array of powers.
           ãX    #   All combinations with X length
              ®q # Join then (arrays to numbers)

c f_Ê¥N          # Filter length to input:
c                # Flatten
  f_             # Filter
    Ê            #  Length
     ¥           #  ==
      N          #  Parsed input
\$\endgroup\$
0
\$\begingroup\$

Ruby, 94 bytes

->n{a=*b=1;a<<b*=2until b>3**n;(2..n).flat_map{|x|a.each_cons(x).map &:join}.grep /^.{#{n}}$/}

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.