Count sum of all digits
Mathematica 30-(10+50)= -30
Shortened by 4 chars thanks to ybeltukov.
Range@n
returns the numbers from 1 through n
.
Integerdigits@n
breaks up each of those numbers into its digits.
Total[n,2]
sums the digits. The 2 is to allow summing across different levels, i.e. lists of lists.
IntegerDigits@Range@#~Total~2&
Testing
IntegerDigits@Range@#~Total~2&[12]
51
IntegerDigits@Range@#~Total~2 &[1000000]
27000001
Expressions
IntegerDigits@Range@#~Total~2 &[55*96 - 12]
55*96 - 12
81393
5268
IntegerDigits@Range@#~Total~2 &[5268]
81393
IntegerDigits@Range@#~Total~2 &[55*96^2 - 12]
55*96^2 - 12
12396621
506868
IntegerDigits@Range@#~Total~2 &[506868]
12396621
C: 150 138 - (100+50) = -12
a,b,c;main(d){for(scanf("%d ",&a);~scanf("%c%d ",&d,&b);a=d^43?d%5?d%2?a/b:a*b:a-b:a+b);for(;a;)for(b=a--;b;b/=10)c+=b%10;printf("%d",c);}
Very shamefully stealing @Fors answer from here to do the expression evaluation: https://codegolf.stackexchange.com/a/11423/13877
Sample usage:
./a.exe <<< "5 + 7"
51
Note: the expression implementation assumes no operator precedence and consumes values as it receives them; ex, 1+2*3 = 9
rather than the typical 7
.
sed, 411 283 - 25 = 258
I can't be bothered to golf it more right now. :-) Not recommended for use with even remotely big integers, but technically it could deal with arbitrarily large integers (you'll likely run out of RAM pretty quickly though, since I (more-or-less have to) encode the number in unary).
s/$/x0123456789/
:l
/9$/H
:b
s/(.)(y*x\1)/y\2/
/(.)y*x\1/b b
s/(.)([xy].*)(.)\1/\3\2\3\1/
:c
s/y(.*(.))/\2\1/
/y/b c
/0$/b f
/^0*x.*9$/!b l
x
s/x[^\n]*\n//g
:d
s/(.)(.*x.*(.)\1)/z\3\2/
/[^z0]x/b d
s/0|x.*|\n//g
H;x
s/./0/g
s/$/x9876543210/
x
:e
x
b l
:f
x
s/.//
/./b e
x
s/^0+|x.*//g
Sample use
(Input lines indented for easier reading.)
5
15
12
51
33
183