Skip to main content
replaced http://codegolf.stackexchange.com/ with https://codegolf.stackexchange.com/
Source Link

Matlab, 76 67 bytes

n=input('');t=num2str(n)-48;if(n<0)t(1)=0;t(2)=-t(2);end n+sum(t+1) 

9 bytes saved thanks to @Luis Mendo@Luis Mendo

Explanation:

n=input(''); -- takes input t=num2str(n)-48; -- makes it a string and then array of digits with "-" becoming -3 (48 is code for 0) if(n<0) t(1)=0; -- set first element (-3) to 0 t(2)=-t(2); -- the second element is the most significant digit, so we have to negate it end n+sum(t+1) -- take sum of n, sum of all digits and length of t (guaranteed by +1 of every element) 

Matlab, 76 67 bytes

n=input('');t=num2str(n)-48;if(n<0)t(1)=0;t(2)=-t(2);end n+sum(t+1) 

9 bytes saved thanks to @Luis Mendo

Explanation:

n=input(''); -- takes input t=num2str(n)-48; -- makes it a string and then array of digits with "-" becoming -3 (48 is code for 0) if(n<0) t(1)=0; -- set first element (-3) to 0 t(2)=-t(2); -- the second element is the most significant digit, so we have to negate it end n+sum(t+1) -- take sum of n, sum of all digits and length of t (guaranteed by +1 of every element) 

Matlab, 76 67 bytes

n=input('');t=num2str(n)-48;if(n<0)t(1)=0;t(2)=-t(2);end n+sum(t+1) 

9 bytes saved thanks to @Luis Mendo

Explanation:

n=input(''); -- takes input t=num2str(n)-48; -- makes it a string and then array of digits with "-" becoming -3 (48 is code for 0) if(n<0) t(1)=0; -- set first element (-3) to 0 t(2)=-t(2); -- the second element is the most significant digit, so we have to negate it end n+sum(t+1) -- take sum of n, sum of all digits and length of t (guaranteed by +1 of every element) 
9 bytes saved thanks to @Luis Mendo
Source Link
pajonk
  • 19.4k
  • 5
  • 29
  • 78

Matlab, 7676 67 bytes

n=input('');t=num2str(n)-48;if(n<0)t(1)=0;t(2)=-t(2);end sum([n numeln+sum(t) t]t+1) 

9 bytes saved thanks to @Luis Mendo

Explanation:

n=input(''); -- takes input t=num2str(n)-48; -- makes it a string and then array of digits with "-" becoming -3 (48 is code for 0) if(n<0) t(1)=0; -- set first element (-3) to 0 t(2)=-t(2); -- the second element is the most significant digit, so we have to negate it end sum([n numeln+sum(tt+1) t])  -- take sum of n, numbersum of all digits +and signlength andof allt  digits (guaranteed by +1 of every element) 

Matlab, 76 bytes

n=input('');t=num2str(n)-48;if(n<0)t(1)=0;t(2)=-t(2);end sum([n numel(t) t]) 

Explanation:

n=input(''); -- takes input t=num2str(n)-48; -- makes it a string and then array of digits with "-" becoming -3 (48 is code for 0) if(n<0) t(1)=0; -- set first element (-3) to 0 t(2)=-t(2); -- the second element is the most significant digit, so we have to negate it end sum([n numel(t) t]) -- take sum of n, number of digits + sign and all digits 

Matlab, 76 67 bytes

n=input('');t=num2str(n)-48;if(n<0)t(1)=0;t(2)=-t(2);end n+sum(t+1) 

9 bytes saved thanks to @Luis Mendo

Explanation:

n=input(''); -- takes input t=num2str(n)-48; -- makes it a string and then array of digits with "-" becoming -3 (48 is code for 0) if(n<0) t(1)=0; -- set first element (-3) to 0 t(2)=-t(2); -- the second element is the most significant digit, so we have to negate it end n+sum(t+1)   -- take sum of n, sum of all digits and length of t   (guaranteed by +1 of every element) 
Source Link
pajonk
  • 19.4k
  • 5
  • 29
  • 78

Matlab, 76 bytes

n=input('');t=num2str(n)-48;if(n<0)t(1)=0;t(2)=-t(2);end sum([n numel(t) t]) 

Explanation:

n=input(''); -- takes input t=num2str(n)-48; -- makes it a string and then array of digits with "-" becoming -3 (48 is code for 0) if(n<0) t(1)=0; -- set first element (-3) to 0 t(2)=-t(2); -- the second element is the most significant digit, so we have to negate it end sum([n numel(t) t]) -- take sum of n, number of digits + sign and all digits