# Compute the Digit Difference Sum of a Number

Consider taking some non-negative integer such as 8675309 and computing the absolute values of the differences between all the pairs of neighboring digits.

## MATLAB (141)(137)

EDIT: 4 bytes less, thanks to @Andras

function[s j]=n(T,b,c),if(T/b>9),u=fix(T/10);[x e]=n(T,b*10,0);y=n(u,b,0);[w z]=n(u,b,c);s=abs(x-y);j=s+e+10*c*z;else,s=mod(T,10);j=s;end
• This doest beat @LuisMendo 's answer but atleast I could reduce execution time, which by, I would have just tried to diversify ways of tackling this problem.
• I could reduce it more but as I go for less time, i waste more bytes, so here is the principle:

The program is summing up digits of same row before inlined digits , it does mean it used integer division "n/10" log_10(n) times only, complexity is O(N).

If n= a b c d

a          b           c           d
|a-b|       |b-c|       |c-d|
||a-b|-|b-c|| ||b-c|-|c-d||
....

My program calculates:

a+|a-b| + | |a-b|-|b-c| |  +  |  | |a-b|-|b-c| | - | |b-c|-|c-d| |  |
+10*(
b+|b-c| + | |b-c|-|c-d| |
+10*(
c+|c-d|
+10*(
d
)
)
)

## Usage:

[a b]=n(13652,1,1)

a =

1

b =

16098
• You can spare 4 bytes by omitting the optional ,end of the function declaration. – Andras Deak Sep 24 '15 at 21:50
• Please consider revising the grammar of your post. I can't quite understand what you have said. – rayryeng Sep 24 '15 at 21:52

# C 162 bytes

golfed:

main(int argc,char **argv){char *c=argv[1];int u=atoi(c),d;do{while(c[1]!=0){*c=abs(*c-*(c+1))+48;c++;}*c=0;c=argv[1];d=atoi(c);u+=d;}while(d>9);printf("%d",u);}

ungolfed:

main(int argc, char **argv)
{
char *c=argv[1];
int u=atoi(c),d;

do
{
while(c[1]!=0)
{
*c=abs(*c-*(c+1))+48;
c++;
}

*c=0;
c=argv[1];
d=atoi(c);
u+=d;
}
while(d>9);

printf("%d\n",u);
}

# R, 134 Bytes

### Code

f=function(x){z=x;while(z>9){n=seq(nchar(z));z=abs(diff(strtoi(substring(z,n,n))));z=sum(z*10**(rev(seq(length(z)))-1));x=x+z};cat(k)}

Test it online.

### Ungolfed

f=function(x){
z=x;
while(z>9){
n=seq(nchar(z));
z=abs(diff(strtoi(substring(z,n,n))));
z=sum(z*10**(rev(seq(length(z)))-1));
x=x+z
};
cat(x)
}

Here is the plot of the difference of the "Digit Difference Sum of a Number" series from f(1) to f(1m). Just because I love to diff.

### Plot code

s <- seq(1,100000)
serie <- sapply(s,f)
plot(diff(ts(serie)),xlab="",ylab="")

# Prolog, 143 bytes

Code:

q(X,N):-X<9,N=0;A is abs(X mod 10-X//10 mod 10),Y is X//10,q(Y,M),N is A+M*10.
r(X,N):-X<9,N=X;q(X,Y),r(Y,M),N is X+M.
p(X):-r(X,N),write(N).

Explained:

q(X,N):-X<9,N=0;                                                         % If only one digit, the difference is 0
A is abs(X mod 10-X//10 mod 10),Y is X//10,q(Y,M),N is A+M*10.   % Else, the difference is the difference between the last 2 digits + the recursive difference of the number without the last digit
r(X,N):-X<9,N=X;                                                         % If we only have 1 digit the final answer is that digit
q(X,Y),r(Y,M),N is X+M.                                          % Else, the final answer is the current number + the recursive difference of that number
p(X):-r(X,N),write(N).

q does the calculations that convert a number into it's Digit Difference.
r recursively calls q and sums up the results to find the Digit Difference Sum.
p is the entry point. Takes a number, calls r and prints the answer.

Example:

>p(8675309).
8898683

Try it online here.

# PHP - 198 bytes

<?$x=$t=$_GET['V'];function z($x){global$t;for($i=0;$i<strlen($x)-1;$i++){$z=str_split($x);$r.=str_replace('-','',$z[$i]-$z[$i+1]);}$r=ltrim($r,'0');$t+=$r;return strlen($r)>1?z($r):0;}z($x);echo$t;

Ungolfed

<?
$x=$t=$_GET['V']; // Gets the value from input function z($x){
global$t; for($i=0;$i<strlen($x)-1;$i++){$z=str_split($x); //Turns the string into an array$r.=str_replace('-','',$z[$i]-$z[$i+1]); // Sums the two values and removes the minus signal
}
$r=ltrim($r,'0'); // Remove trailing zeroes
$t+=$r; // Adds to global var
return strlen($r)>1?z($r):0; // Checks the size of the string. If >1, calls the function again
}

z($x); echo$t;

{[+] $_,{+.comb.rotor(2=>-1)».map((*-*).abs).join}…0} # 56 bytes usage: my &code = {...} # insert code from above (180..190).map: &code; # (259 258 259 260 261 262 263 264 265 266 280) say code 8675309; # 8898683 # Jelly, 6 bytes ạƝḌ$ƬS

Try it online!

## How it works

ạƝḌ$ƬS - Main link. Takes an integer n on the left$Ƭ  - Repeat the following until a repeated value, collected intermediate results:
Ɲ     -   Convert an integer to digits, and on overlapping pairs:
ạ      -     Take the absolute difference
Ḍ    -   Convert from digits to an integer
S - Take the sum of these values

ΣU¡ȯdẊ≠d

Try it online!

Èìä'a}hN

Try it