Alternested numbers

12

0

Consider the array of positive integers:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, ...

Then, concatenate them:

1234567891011121314151617181920212223242526...

And then split them into chunks of variable length, each length being equal to the Nth positive integer:

[1][23][456][7891][01112][131415][1617181][92021222][324252627][2829303132] ...
---------------------------------------------------------------------------
 1  2    3     4     5       6       7        8          9          10      ...

Task

Given an integer N (positive for 1-indexing or non-negative for 0-indexing), your task is to output the sum of the deltas of the digits in the Nth chunk (the differences between consecutive digits).

Examples & Test cases

1-indexed test cases. If you want 0-indexed ones, just decrement N.

N, Chunk, Deltas, Sum

1  -> 1          -> []                               -> 0
2  -> 23         -> [1]                              -> 1
3  -> 456        -> [1, 1]                           -> 2
4  -> 7891       -> [1, 1, -8]                       -> -6
5  -> 01112      -> [1, 0, 0,1]                      -> 2
6  -> 131415     -> [2, -2, 3, -3, 4]                -> 4
7  -> 1617181    -> [5, -5, 6, -6, 7, -7]            -> 0
8  -> 92021222   -> [-7, -2, 2, -1, 1, 0, 0]         -> -7
9  -> 324252627  -> [-1, 2, -2, 3, -3, 4, -4, 5]     -> 4
10 -> 2829303132 -> [6, -6, 7, -6, -3, 3, -2, 2, -1] -> 0

Puzzle 2 on CodeGolf-Hackathon (I am the original author there too, so I am allowed to repost). Related, Inspiration. Related.

Mr. Xcoder

Posted 2017-10-21T17:16:48.373

Reputation: 39 774

Kinda related but not really – James – 2017-10-21T17:47:17.483

1The sum of all the differences between consecutive digits is just the difference between the last and the first. – KSmarts – 2017-10-23T16:01:03.527

Answers

5

JavaScript (ES6), 54 53 51 50 bytes

Saved 1 byte thanks to @tsh

0-indexed.

k=>-(n=1,g=s=>s[x=k*-~k/2]-s[x+k]-n||g(s+n++))``-n

Test cases

let f =

k=>-(n=1,g=s=>s[x=k*-~k/2]-s[x+k]-n||g(s+n++))``-n

for(i = 0; i < 10; i++) {
  console.log('a(' + i + ') = ' + f(i));
}

Arnauld

Posted 2017-10-21T17:16:48.373

Reputation: 111 334

Zero-indexed: k=>-(n=1,g=s=>s[x=k*-~k/2]-s[x+k]-n||g(s+n++))""-n – tsh – 2017-10-23T05:46:19.317

4

Husk, 9 bytes

ΣẊ-!SCṁdN

Try it online!

My solution to the Hackathon.

Explanation:

ΣẊ-!SCṁdN⁰
    S      (x -> y -> z):f -> (x -> y):g -> x:x :: return f(x, g(x))
     C      f= [num]:x -> [x]:y -> [x] :: cut y in pieces where each piece has its respective length in x
      ṁ     g= (x -> [y]):f -> ([x]:x -> [y]) :: maps f over x then concatenate
       d     f= num:x -> [num] :: return decimal digits of x
        N   x= sequence of natural numbers [1..]
   !     ⁰ [x]:x -> num:y -> x :: get yth (impl. input) element of x (above result)
 Ẋ         (x -> x -> y):f -> [x]:x -> [y] :: map f over overlapping pairs of x (above result)
  -         f= num:x -> num:y -> num :: return y - x
Σ          [num]:x -> num :: return sum of x (above result)

Erik the Outgolfer

Posted 2017-10-21T17:16:48.373

Reputation: 38 134

4

APL (Dyalog), 32 bytes

{+/2-⍨/⍎¨⍵↑(+/⍳⍵-1)↓' '~⍨⍕⍳+/⍳⍵}

Try it online!

How?

+/⍳⍵ - sum of 1 to n

- make range of that

' '~⍨⍕ - into string, without spaces

(+/⍳⍵-1)↓ - drop first (sum of 1 to n-1) chars

⍵↑ - keep the next n chars

⍎¨ - make every char into integer

2-⍨/ - differences list (backward subtraction for every 2 items)

+/ - sum it up.

Uriel

Posted 2017-10-21T17:16:48.373

Reputation: 11 708

4

Haskell, 61 60 bytes

l=fromEnum<$>(show=<<[1..])
f n|t<-sum[2..n]=l!!t-l!!(t-n+1)

Try it online!

Explanation:

The sum of the deltas of a list is the same as the difference between the last and the first element.

The last element (zero-indexed) is t, triangle(n)-1 = sum[2..n]. The first element, then is t-n+1, as the list has n elements.

H.PWiz

Posted 2017-10-21T17:16:48.373

Reputation: 10 962

4

Python 2, 80 bytes

n=input()
s=map(int,''.join(map(str,range(2**n))))
print s[n*-~n/2]-s[~-n*n/2+1]

Try it online!

2**n is way overkill, of course, but it’s a byte shorter than something like n*n+1.

Lynn

Posted 2017-10-21T17:16:48.373

Reputation: 55 648

3

Mathematica, 71 bytes

Tr@Differences[TakeList[Join@@IntegerDigits[Range[#^2]],Range@#][[#]]]&

Try it online!

J42161217

Posted 2017-10-21T17:16:48.373

Reputation: 15 931

3

JavaScript (ES6), 60 57 53 bytes

f=(n,s=i='',m=n*-~n/2)=>s[m]?s[m]-s[m-n+1]:f(n,s+i++)
<input type=number min=1 oninput=o.textContent=f(this.value)><pre id=o>

1-indexed. Previous 60-byte nonrecursive version:

f=
(n,s=[...Array(n*n+1).keys()].join``)=>s[m=n*-~n/2]-s[m-n+1]
<input type=number min=1 oninput=o.textContent=f(this.value)><pre id=o>

Neil

Posted 2017-10-21T17:16:48.373

Reputation: 95 035

2

05AB1E, 8 bytes

∞LJā£è¥O

0-indexed.

Try it online!

Okx

Posted 2017-10-21T17:16:48.373

Reputation: 15 025

2

Python 2, 87 bytes

n=input()
a=map(int,''.join(map(str,range(1,n*n))))[n*~-n/2:][:n]or[0]
print a[-1]-a[0]

Try it online!

ovs

Posted 2017-10-21T17:16:48.373

Reputation: 21 408

1

Python 2, 104 bytes

def f(N):A=map(int,"".join(map(str,range(1,N*N)))[~-N*N/2:][:N]);return sum(a-b for a,b in zip(A[1:],A))

Try it online!

Jonathan Frech

Posted 2017-10-21T17:16:48.373

Reputation: 6 681

1

Perl 6,  58  55 bytes

{[+] ($_=(1..*).map(|*.comb).rotor(1..*)[$^a])[1..*]Z-@$_}

Test it

{[+] ($_=(1..*).map(|*.comb)[^$^a+[+] ^$a])[1..*]Z-@$_}

Test it

Expanded:

{ # bare block lambda with placeholder parameter 「$a」
  [+]  # reduce using &infix:«+» the following


    (
      $_ =                # store into 「$_」 for later use

        ( 1 .. * )        # Range of all positive integers
        .map( | *.comb )\ # split into digits and flatten into single list

        [                 # index into the sequence (1 based)

          ^$^a            # Range up to (and excluding) the input
                          # 「0 ..^ $a」 or 「0 .. $a-1」

          +               # shift it up by
          [+] ^$a         # the sum of the values up to (and excluding) the input

        ]

    )[ 1 .. *]            # skip the first value

    Z-                    # zip using &infix:«-»

    @$_                   # 「$_」 used as a List
}

Brad Gilbert b2gills

Posted 2017-10-21T17:16:48.373

Reputation: 12 713

1

PHP, 163 147 bytes

$v=$argv[1];for($i=1;$i<=$v*$v;$i++){$s.=$i;$j+=$i<$v?$i:0;}$s=array_slice(str_split($s),$j,$v);for($i=0;$i<$v-1;$i++){$k+=$s[$i+1]-$s[$i];}echo$k;

Try it online!

My first attempt at code golfing... have a feeling that this can be shorter

Edit: saved 16 bytes by removing several instantiations

SmartCoder

Posted 2017-10-21T17:16:48.373

Reputation: 131

Welcome to the site! You may want to look through these tips for golfing in PHP

– caird coinheringaahing – 2017-10-26T08:00:37.763

0

Perl 5, 79 + 1 (-p) = 80 bytes

$_=substr join('',1..$_**2),$_*($_-1)/2,$_;1while s/(.)(.)/$r+=$2-$1;$2/e;$_=$r

Try it online!

Xcali

Posted 2017-10-21T17:16:48.373

Reputation: 7 671

0

Pyth, 29 27 bytes

Saved 2 bytes thanks to @Mr.Xcoder.

s.+msdc:sm+hd""U*QQKsUQ+QK1

Try it online!

Ian H.

Posted 2017-10-21T17:16:48.373

Reputation: 2 431

127 bytes pretty sure it can be golfed further... – Mr. Xcoder – 2017-10-22T16:00:45.170

0

Jelly, 14 bytes

²RDFṫ³ḶS‘¤ðḣIS

Try it online!

Explanation

²RDFṫ³ḶS‘¤ðḣIS    Main Link
²                  Square input
 R                 Range: [1,2,3,..,n^2]
  D                Digits: [1,2,...,[1,0],[1,1],...]
   F               Flatten list
     ³ḶS‘¤         n(n-1)/2+1
    ṫ              Remove the first n(n-1)/2+1 elements from the list of digits
          ðḣ       Take the first n digits of the list. ð is needed to prevent I from acting on n.
            I      Increment. Take the diferences
             S     Sum

I originally started by taking the range( n(n+1)/2 ) but since you can have extra digits at the end of the list before slicing it I changed it to range(n^2). You have extra digits after 1-9 anyway.

dylnan

Posted 2017-10-21T17:16:48.373

Reputation: 4 993

+²HRDFṫЀ³ḶḣЀRS€‘¤ṪðḣIS original (successful but long) attempt – dylnan – 2017-12-01T18:31:05.323