Exponentiation Sequence

13

0

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!

RedClover

Posted 2017-09-12T16:49:13.713

Reputation: 719

1So 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. – cole – 2017-09-12T16:56:01.450

@cole Actually, yes, it's only with 2. Thanks for mentioning! – RedClover – 2017-09-12T16:56:49.313

1Can output be separated by newline? – H.PWiz – 2017-09-12T17:23:51.877

@H.PWiz Actually, why not? Let me just edit the question. – RedClover – 2017-09-12T17:24:42.217

I can't say that I don't keep upvoting every single answer here. The genius of people on ppcg is just amazing. – RedClover – 2017-09-12T17:48:50.750

Does the output order matter? – Shaggy – 2017-09-13T07:41:56.007

@Shaggy No. You can output in any order you want. – RedClover – 2017-09-13T07:46:15.513

Pushing my luck here but would this be an acceptable form of output?

– Shaggy – 2017-09-13T20:14:48.043

@Shaggy No. I don't think I should allow it. I never seen anything output this way. – RedClover – 2017-09-13T20:17:58.150

1

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!)

– Shaggy – 2017-09-13T22:27:18.607

You are missing 163264128256512102420484096 and 641282565121024204840968192 from the sequence generated from 27. – kamoroso94 – 2017-09-14T04:07:16.297

@kamoroso94 No, I'm not. I stated that if you want to support larger sequences than 2048, you can. But it's optional – RedClover – 2017-09-14T04:27:00.290

1

In the intro, you should capitalize Polish. "polish" is a different English word.

– Peter Cordes – 2017-09-17T03:45:28.740

Answers

7

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

Emigna

Posted 2017-09-12T16:49:13.713

Reputation: 50 798

X› can be – Erik the Outgolfer – 2017-09-13T12:31:36.460

@EriktheOutgolfer: Oh yeah. I always forget that one exist now. Thanks :) – Emigna – 2017-09-13T12:41:05.450

Can Y₃Ým be ₃Ýo? – Riley – 2017-09-13T13:35:51.210

@Riley: Yep. I had that before, but for some reason I must have changed it. Thanks :) – Emigna – 2017-09-13T13:46:06.753

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? – RedClover – 2017-11-13T19:33:38.173

@Soaku: Yeah, there have been a lot of changes to the language recently so old programs might not be compatible in all cases. If you want to run this code you can checkout an interpreter from the time of this challenge. You could also add s as the second last character to get this answer working in the new interpreter. – Emigna – 2017-11-13T19:53:33.317

6

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

user48543

Posted 2017-09-12T16:49:13.713

Reputation:

21 bytes: fqQlTmjkdftlT.:m^2d12 – Mr. Xcoder – 2017-09-12T17:22:57.060

120 bytes - fqQlTjLkftlT.:m^2d12 – Mr. Xcoder – 2017-09-12T17:26:17.910

19 bytes - fqQlTjLkftT.:m^2d12

– Jakube – 2017-09-12T19:42:32.497

18 bytes - fqQlTjLkftT.:^L2yT

– Jakube – 2017-09-12T19:45:46.133

1

17 bytes - fqQlTjLkt#.:^L2yT

– Jakube – 2017-09-12T19:49:33.637

4

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]

Jonathan Allan

Posted 2017-09-12T16:49:13.713

Reputation: 67 804

1

Is this too similar to yours? (please be honest :p)

– Erik the Outgolfer – 2017-09-12T18:47:11.730

Funnily enough I was just trying to use V and it'll work for 16 rather than 1000: ⁴Ḷ2*Ẇṫ17VDL$⁼¥Ðf. – Jonathan Allan – 2017-09-12T18:52:08.763

I'm going for the most :p – Erik the Outgolfer – 2017-09-12T18:52:36.840

@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). – Jonathan Allan – 2017-09-12T19:09:37.337

@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! – Jonathan Allan – 2017-09-12T19:13:39.580

I will keep mine. Oh, and your assumption is correct, because I don't think "Erik" is a female name is it? ;p – Erik the Outgolfer – 2017-09-13T10:55:12.240

Not normally, but one can never be 100% sure of these things :) – Jonathan Allan – 2017-09-13T15:12:21.903

4

Perl 6, 62 59 bytes

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

Try it online!

nwellnhof

Posted 2017-09-12T16:49:13.713

Reputation: 10 037

4

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

Shaggy

Posted 2017-09-12T16:49:13.713

Reputation: 24 623

3

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

H.PWiz

Posted 2017-09-12T16:49:13.713

Reputation: 10 962

3

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!

Rod

Posted 2017-09-12T16:49:13.713

Reputation: 17 588

3

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.

jferard

Posted 2017-09-12T16:49:13.713

Reputation: 1 764

It's returning extra cases for an input of 27 though. – Jonathan Allan – 2017-09-12T18:10:47.303

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. – RedClover – 2017-09-12T18:13:20.657

@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. – jferard – 2017-09-12T18:27:58.350

@jferard that was actually edited in after your answer (it actually stated "limited to 2048" before). I can shorten mine in this case too. – Jonathan Allan – 2017-09-12T18:30:38.017

1@JonathanAllan Yes, I actually realised that some of rules in the question are wrong and misleading just after posting this anwer. – RedClover – 2017-09-12T18:41:57.267

@JonathanAllan I wasn't cheating: I simply didn't see this limitation when I posted my answer! As Soaku stated, that does not change anything for my answer : 11 vs 99. – jferard – 2017-09-12T19:07:40.493

@jferard yeah, I didn't think you were cheating :) – Jonathan Allan – 2017-09-12T19:16:21.893

s<-[show.(2^)=<<[i..j]] saves some bytes. – Laikoni – 2017-09-13T14:24:54.397

@Laikoni thanks – jferard – 2017-09-13T18:38:12.683

3

Jelly, 16 bytes

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

Try it online!

Note: very inefficient. Returns a list of numbers.

Erik the Outgolfer

Posted 2017-09-12T16:49:13.713

Reputation: 38 134

Tio doesn't seem to be able to parse this code... It always exceeds 60 seconds... – RedClover – 2017-09-12T18:50:24.853

1

@Soaku Try replacing the ȷs (i.e. 1000) with 20s (lower upper limit).

– Erik the Outgolfer – 2017-09-12T18:51:00.733

1@Soaku it works in theory - it just times out because it is very inefficient. – Jonathan Allan – 2017-09-12T18:53:06.250

@ErikTheOutgolfer Then there is no output – RedClover – 2017-09-12T18:53:34.373

@Soaku Did you follow my link? There is output there. – Erik the Outgolfer – 2017-09-12T18:53:52.977

@EriktheOutgolfer Weird. Not for me. I followed your link, replaced the J-thing with 20. And no output. – RedClover – 2017-09-12T18:54:52.970

1@Soaku I meant on my comment. I have already replaced them there, and there's output [12, 24, 48]. – Erik the Outgolfer – 2017-09-12T18:55:35.103

@EriktheOutgolfer Oh, sorry, did not notice the second ȷ – RedClover – 2017-09-12T18:55:43.183

@EriktheOutgolfer By the way, you can just simply replace those J with different Jelly built-in, since my challenge requires you to support at least the 2048 sequence. – RedClover – 2017-09-12T18:57:18.013

1@Soaku Why not go for the most if you can without extra cost? ;) – Erik the Outgolfer – 2017-09-13T10:53:46.853

3

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);
}

Arnauld

Posted 2017-09-12T16:49:13.713

Reputation: 111 334

2

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

J42161217

Posted 2017-09-12T16:49:13.713

Reputation: 15 931

2

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++);
}

Steadybox

Posted 2017-09-12T16:49:13.713

Reputation: 15 798

1

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!

flodel

Posted 2017-09-12T16:49:13.713

Reputation: 2 345

1

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!

Xcali

Posted 2017-09-12T16:49:13.713

Reputation: 7 671

1

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

RedClover

Posted 2017-09-12T16:49:13.713

Reputation: 719

0

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!

G B

Posted 2017-09-12T16:49:13.713

Reputation: 11 099