# Alternested numbers

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  2    3     4     5       6       7        8          9          10      ...


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

• You may choose either 0 or 1-indexing for N.

• This is , shortest code in bytes wins.

# 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
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.

• Kinda related but not really Oct 21 '17 at 17:47
• The sum of all the differences between consecutive digits is just the difference between the last and the first. Oct 23 '17 at 16:01

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


# JavaScript (ES6), 545351 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));
}

• Zero-indexed: k=>-(n=1,g=s=>s[x=k*-~k/2]-s[x+k]-n||g(s+n++))""-n
– tsh
Oct 23 '17 at 5:46

# 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.

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. # 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. # Mathematica, 71 bytes Tr@Differences[TakeList[Join@@IntegerDigits[Range[#^2]],Range@#][[#]]]&  Try it online! ## JavaScript (ES6), 6057 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> # 05AB1E, 8 bytes ∞LJā£è¥O  0-indexed. Try it online! # Python 2, 87 bytes n=input() a=map(int,''.join(map(str,range(1,n*n))))[n*~-n/2:][:n]or print a[-1]-a  Try it online! # 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! # 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 }  # PHP, 163 147 bytes $v=$argv;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

• Welcome to the site! You may want to look through these tips for golfing in PHP Oct 26 '17 at 8:00

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

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


Try it online!

# Pyth, 29 27 bytes

Saved 2 bytes thanks to @Mr.Xcoder.

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


Try it online!

• 27 bytes pretty sure it can be golfed further... Oct 22 '17 at 16:00

# 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.

• +²HRDFṫÐ€³ḶḣÐ€RS€‘¤ṪðḣIS original (successful but long) attempt Dec 1 '17 at 18:31