18
\$\begingroup\$

Challenge:

Given an index integer n, either output the n'th item in this sequence, or output the sequence up to and including index n:

25,25,7,28,29,20,21,22,23,14,35,26,7,28,29,20,16,29,12,15,28,21,14,17,30,13,16,29,12,15,28,21,10,6,12,18,15,11,7,13,19,17,13,9,15,21,18,14,10,16,22,19,15,11,17,23,20,16,12,18,24,21,17,13,19,25,23,19,15,21,27,24,20,16,22,28,25,21,17,23,29,16,13,9,15,21,18,14,10,16,22,20,16,12,18,24,21,17,13,19

How does this sequence work?

NOTE: In this explanation, index n is 1-indexed.
Put the numbers 1 through x on two lines of length n*6 - 1, where x depends on the current iteration and the length of the numbers used, and then sum the digits of the n'th/right-most Olympic Rings of those two lines.

The first number in the sequence is calculated as follows:

The length of the lines are 5 (because 1*6 - 1 = 5):
12345
67891(0)

Then leave the digits in an Olympic Rings pattern:
1 3 5
 7 9

And sum them:
1+3+5+7+9 = 25

So n=1 results in 25.

The second number in the sequence is calculated as follows:

The length of the lines are 11 (because 2*6 - 1 = 11):
12345678910
11121314151(6)

Then leave the digits in the second/right-most Olympic Rings pattern:
      7 9 0 
       4 5

And sum them:
7+9+0+4+5 = 25

So n=2 results in 25.

The third number in the sequence is calculated as follows:

The length of the lines are 17 (because 3*6 - 1 = 17):
12345678910111213
14151617181920212(2)

Then leave the digits in the third/right-most Olympic Rings pattern:
            1 2 3
             0 1

And sum them:
1+2+3+0+1 = 7

So n=3 results in 7.

etc.

Challenge rules:

  • When you output the n'th item in the sequence, you are allowed to take the input as 0-indexed instead of 1-indexed, but keep in mind that the calculations of n*6 - 1 will then become (n+1)*6 - 1 or (n+1)*5 + n.
  • Single numbers of more than one digit can be split up at the end of the first line when we've reached the length n*5 + n-1, so it is possible that a number with 2 or more digits is partially the trailing part of line 1, and partially the leading part of line 2.

General rules:

  • This is , so shortest answer in bytes wins.
    Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
  • Standard rules apply for your answer, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
  • Default Loopholes are forbidden.
  • If possible, please add a link with a test for your code.
  • Also, please add an explanation if necessary.

Test cases:

Here is a paste-bin of the test cases 1-1,000, so feel free to choose any of them.

Some additional higher test cases:

1010:   24
1011:   24
2500:   19
5000:   23
7500:   8
10000:  8
100000: 25
\$\endgroup\$
4
  • \$\begingroup\$ 1*5 + 1-1 = 5? Shouldn't it be 1*5 + 5 - 1 = 9? \$\endgroup\$
    – Maya
    Commented Jan 22, 2018 at 14:04
  • \$\begingroup\$ @NieDzejkob No, n=1, so n*5 + n-1 becomes 1*5 + 1-1, which in turn is 5 - 0 = 5. \$\endgroup\$ Commented Jan 22, 2018 at 14:10
  • \$\begingroup\$ isn't n * 5 + n - 1 equal to n * 6 - 1? \$\endgroup\$
    – Brian H.
    Commented Jan 22, 2018 at 15:11
  • \$\begingroup\$ @BrianH. You're right, it indeed is. Noticed that as well after seeing Martin's Retina answer. I've edited the challenge description to use this shorter formula. \$\endgroup\$ Commented Jan 22, 2018 at 15:16

12 Answers 12

5
\$\begingroup\$

Retina, 70 68 62 bytes

.+
10**
.
$.>`
~(`.+
6*$+*
)`.(.+)
L`.{$.1}
%,-6`.

,2,9`.
*
_

Try it online!

Explanation

Let's call the input n, and we'll use 3 as an example.

.+
10**

The 10** is short for 10*$&*_ which replaces the input with a string of 10n underscores.

.
$.>`

Now we replace each underscore with the length of the string up to and including that underscore. So this just results in the number from 1 to 10n all concatenated together (10n is always enough to fill up two lines of the required length).

~(`.+
6*$+*

Eval! This and the next stage will generate the source code of another program, which is then run against that string of concatenated integers.

To generate that program, this stage first replaces the integers with a string of 6n underscores ($+ refers to the program's original input).

)`.(.+)
L`.{$.1}

Then replace those underscores with L`.{…}, where is 6n-1 (the length of the lines we're looking at). So we've generated a regex, whose quantifier depends on the original input.

When this program gets eval'ed it matches chunks of length 6n-1, of which there will be at least two. For our example input 3, we end up with:

12345678910111213
14151617181920212
22324252627282930

Now we just need to extract the relevant digits.

%,-6`.

First, on each line (%) we remove all but the last five digits (,-6). That gives us

11213
20212
82930

Finally:

,2,9`.
*

We expand every other digit (2) in the first ten (9, this is 0-based) in unary. Those happen to be those in the Olympic Rings positions.

_

And we count the number of resulting underscores, to sum them and convert the result to decimal.

\$\endgroup\$
3
  • \$\begingroup\$ 59 bytes A bit messy though \$\endgroup\$
    – H.PWiz
    Commented Jan 22, 2018 at 19:12
  • \$\begingroup\$ @H.PWiz Doesn't seem to work for input 1. \$\endgroup\$ Commented Jan 22, 2018 at 20:35
  • \$\begingroup\$ Oh yeah, that will be the -7 \$\endgroup\$
    – H.PWiz
    Commented Jan 22, 2018 at 20:57
4
\$\begingroup\$

Husk, 16 bytes

ΣĊ2ṁ↑_5↑2CṁdN←*6

Try it online!

-3 bytes thanks to H.PWiz.

(Rushed) explanation:

ΣĊ2ṁ↑_5↑2CṁdN←*6⁰
Σ                 Sum
 Ċ2                Drop every second element
   ṁ↑_5             Map "take last 5 elements", then concatenate
       ↑2            Take first 2 elements
         C            Cut x into sublists of length y
          ṁdN          [1,2,3,4,5,6,7,8,9,1,0,1,1,1,2,1,3,...] (x)
             ←         Decrement (y)
              *6        Multiply with 6
                ⁰        First argument
\$\endgroup\$
4
  • \$\begingroup\$ @KevinCruijssen Weird, because I was just going to add one. However I don't recommend accepting an answer anyway. ;-) \$\endgroup\$ Commented Feb 5, 2018 at 18:36
  • \$\begingroup\$ @KevinCruijssen I've added a rushed-up explanation, although it's not very good atm... \$\endgroup\$ Commented Feb 5, 2018 at 18:44
  • \$\begingroup\$ Good enough for me, accepted. And personally I prefer to accept challenges, even though it indeed isn't really necessary. If someone happens to post a shorter answer than yours I will of course change it again. \$\endgroup\$ Commented Feb 6, 2018 at 8:40
  • \$\begingroup\$ @KevinCruijssen The main objective against that isn't what happens after a shorter answer is posted, but that somebody may be discouraged to post another answer at all. Anyway, your call. \$\endgroup\$ Commented Feb 6, 2018 at 12:54
3
\$\begingroup\$

Python 2, 94 90 bytes

n=input()*6
s=''.join(map(str,range(n*2)))
print sum(map(int,s[n-5:n:2]+s[n*2-5:n*2-1:2]))

Try it online!

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

Japt, 33 32 30 29 28 27 bytes

Oh, this is not pretty!

Outputs the nth term, 1-indexed.

*6É
*2 õ ¬òU mt5n)¬¬ë2 ¯5 x

Try it


Explanation

                         :Implicit input of integer U           :e.g., 3
*6É    
*6                       :Input times 6                         :18
  É                      :Subtract 1                            :17
   \n                    :Assign the above to variable U

*2 õ ¬òU mt5n)¬¬ë2 ¯5 x
*2 õ                     :[1,U*2]                               :[1,2,3,...,33,34]
     ¬                   :Join to a string                      :"123...3334"
      òU                 :Partitions of length U                :["123...13","1415...212","22324...30","31323334"]
         m               :Map
          t5n)           :  Get last 5 characters               :["11213","20212","82930","23334"]
              ¬          :Join to a string                      :"11213202128293023334"
               ¬         :Split to an array                     :["1","1","2","1","3","2","0","2","1","2","8","2","9","3","0"],["2","3","3","3","4"]]
                ë2       :Get every second element              :["1","2","3","0","1","8","9","0","3","3"]
                   ¯5    :Get first 5 elements                  :["1","2","3","0","1"]
                      x  :Reduce by addition                    :7
                         :Implicit output of result
\$\endgroup\$
2
\$\begingroup\$

Python 2, 97 bytes

n=input()*6;k=1;s=''
exec's+=`k`;k+=1;'*n*2
print sum(int(s[p])for p in(n-2,n-4,n-6,n*2-4,n*2-6))

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Python 3, 129 123 bytes

p=lambda r,x='',i=1:sum(map(int,str(x[6*r-2]+x[6*r-4]+x[6*r-6]+x[12*r-4]+x[12*r-6])))if len(x)>12*r-2else p(r,x+str(i),i+1)

Try It Online

This is pretty much messed up though, but works.

\$\endgroup\$
2
\$\begingroup\$

05AB1E, 22 21 20 bytes

6*<xLJsô2£íε5£}SāÉÏO

Try it online!

Explanation

6*<                    # push input*6-1
   xL                  # leave it on the stack while pushing [1 ... 12*input-2]
     J                 # join the numbers to a single string
      sô               # split the string into pieces of size input*6-1
        2£             # take the first 2 such pieces
          í            # reverse each string
           ε5£}        # take the first 5 chars of each
               S       # split to a single list of digits
                ā      # push range [1 ... len(list)]
                 ÉÏ    # keep only the numbers in the list of digits which are odd in this
                   O   # sum

Alternative 21 byte approach

6*<©·LJƵYS24S®-ì®-(èO
\$\endgroup\$
2
  • \$\begingroup\$ @KevinCruijssen: Sure. I was intending to try and golf it a bit more before adding the explanation, but I haven't really had time so here it is :) \$\endgroup\$
    – Emigna
    Commented Jan 23, 2018 at 8:22
  • \$\begingroup\$ Thanks! And I know most people prefer to golf it first as much as possible before adding an explanation, but since there hadn't been an update to your answer in 15+ hours I figured I'd just ask for an explanation. :) Nice answer, btw! \$\endgroup\$ Commented Jan 23, 2018 at 8:30
2
\$\begingroup\$

Jelly, 19 bytes

×6’µḤD€Ẏsḣ2ṫ€-4Ẏm2S

Try it online!

×6’µḤD€Ẏsḣ2ṫ€-4Ẏm2S Arguments: n (1-indexed)
×6                  Multiply by 6
  ’                 Decrement
   µ                Call that value N and start a new chain with argument N
    Ḥ               Double
      €             Create an inclusive range from 1 to 2N and call this link on it
     D               Get the decimal digits of each integer in the range
       Ẏ            Concatenate the lists of digits
        s           Split into length-N chunks
         ḣ2         Get the first two elements
            €-4     Map this link over the length-2 list with right argument -4
           ṫ         Get elements from this index onwards (supports negative indices too)
               Ẏ    Concatenate the two length-5 lists into one length-10 list
                m2  Take every second element starting from the first
                  S Sum
\$\endgroup\$
2
  • \$\begingroup\$ Would you mind adding an explanation? \$\endgroup\$ Commented Jan 23, 2018 at 7:56
  • \$\begingroup\$ @KevinCruijssen sure, but I usually do it on request, otherwise I focus on answering other challenges, doing other stuff or sleeping :P \$\endgroup\$ Commented Jan 23, 2018 at 12:21
1
\$\begingroup\$

J, 61, 58 57 bytes

g=.3 :'+/".,_2(2{.{.)\,_5{."1(2{.(-.6*y)]\;":&.>1+i.9*y)'

Try it online!

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

Ruby, 65 63 56 bytes

->n{[2,4,6,4-n*=6,6-n].sum{|a|([*1..n*2]*'')[n-a].to_i}}

Try it online!

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

Clean, 138 101 bytes

import StdEnv
$n=sum(tl[toInt([c-'0'\\i<-[1..],c<-:toString i]!!(6*i+j))\\i<-[2*n-1,n-1],j<-[4,0,2]])

Try it online!

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

Java 8, 138 111 109 bytes

n->{String s="";int r=0,i=1;for(n=n*6-1;i<3*n;s+=i++);for(i=5;i>0;r+=s.charAt(n+n*(i%2^1)-i--)-48);return r;}

I'll of course have to answer my own challenge. :)
I lost my initial code that I've used to create the test results in the challenge description, so I've just started over.

Explanation:

Try it online.

n->{                               // Method with integer as both parameter and return-type
  String s="";                     //  Temp String
  int r=0,                         //  Result-sum, starting at 0
      i=1;                         //  Index integer, starting at 1
  for(n=n*6-1;                     //  Replace the input with `n*6-1`
      i<3*n;                       //  Loop from 1 up to 3*n (exclusive)
      s+=i++);                     //   And append the temp-String with `i`
  for(i=5;i>0;                     //  Loop from 5 down to 0 (exclusive)
    r+=                            //   Add to the result-sum:
       s.charAt(               )-48);
                                   //    The character at index X, converted to a number,
                n+n*(i%2^1)-i--    //    with X being `n-i` (i=odd) or `n+n-i` (i=even)
  return r;}                       //  Return the result-sum
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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