# Challenge :

Given two non-negative integers (X and Y > 1) calculate the sum of digits of their powers . i.e : sum of digits of X ^ Y .

# Input :

Two non-negative integers X and Y

# Output :

The sum of digits of X raised to power Y X ^ Y.

# Examples :

5 , 2                    ---> 7
9 , 4                    ---> 18
3 , 17                   ---> 27


Both numbers will be greater than 1

# Restrictions :

This is so shortest code (in bytes) for each language wins.

# Notes :

All input will be valid. i.e : Both inputs will be greater than 1 and will be integers.

• You shouldn't immediately answer your own question. It is generally frowned upon. – fəˈnɛtɪk Apr 28 '18 at 14:19
• related – fəˈnɛtɪk Apr 28 '18 at 14:20
• I'm not really sure how a question that has 14 answers can get 8 downvotes with no negative comments to explain why. Perhaps I don't fully understand the rules. Anyone care to explain? – ElPedro Apr 28 '18 at 22:04
• – user202729 Apr 29 '18 at 2:04
• I'm tempted to answer 1 since in base X>1, Xis 10, so X^Y is 1 followed by Y zeros. that's always the sum of the digits, and the question didn't specify what base must be used. – Kelly Lowder Apr 29 '18 at 3:11

# Pari/GP, 21 bytes

(x,y)->sumdigits(x^y)


Try it online!

# C, 60 56 Bytes

This is my first time golfing, and I think this code can be shortened. Feel free to help and improve this. Edit: Does not work when you give it a and b such that a^b is over 2^31.

4 Bytes saved thanks to Dennis.

i,q;f(a,b){for(i=0,q=pow(a,b);q;q/=10)i+=q%10;return i;}


test it by editing the numbers in the printf

• Welcome to PPCG! If you declare i and q as globals, you can save a few bytes by using a for loop. Try it online! – Dennis Apr 28 '18 at 14:45

# Jelly, 3 bytes

*DS


A dyadic link accepting a positive integer, X, on the left and a positive integer, Y, on the right which returns the resulting positive integer.

Try it online!

### How?

*DS - Link: X, Y
*   - exponentiate X to the Y
D  - to a list of its digits
S - sum


# Python 3, 34 bytes

lambda a,b:sum(map(int,str(a**b)))


Try it online!

# APL+WIN, 8 bytes

Prompts for y followed by x:

+/⍎¨⍕⎕*⎕


# Neim, 2 bytes

𝕎𝐬


Power 𝕎; implicitly coerce to list; sum 𝐬

Try it online!

# Funky, 31 bytes

a=>b=>{n=0fors in''..a^b n-=-s}


Try it online!

# J, 12 bytes 11 bytes

-1 byte thanks to Cows quack

1#.,.&.":@^


Try it online!

^ power

,.&.": convert to list of digits

1#. add up the digits

• Try 1#.,.&.":@^ – Kritixi Lithos Apr 28 '18 at 14:47
• @Cows quack - Thanks, I should have tried it :) – Galen Ivanov Apr 28 '18 at 15:39

x%y=sum[read[d]|d<-show$x^y]  Try it online! -1 byte thanks to user9549915 # Charcoal, 6 bytes ＩΣＩＸＮＮ  Try it online! Link is to verbose version of code. Explanation:  Ｎ First input as a number Ｎ Second input as a number Ｘ Power Ｉ Cast to string Σ Sum of digits Ｉ Cast to string Implicitly print  • One of the few times such a hacky sum builtin is useful :P – ASCII-only May 2 '18 at 8:18 # Japt, 5 bytes Saved 1 byte thanks to @Shagy pV ìx  Try it online! • Welcome to Japt! You can save a byte by removing the space before x; N.ì(f) converts N to an array of digits, passes that array through f and then converts back to an integer. – Shaggy May 14 '18 at 13:06 # Pyth, 6 bytes ssM^F  Try it online! If the list input isn't allowed, you can just replace the F with E and it'll take in two integers (one on each line) but do the operation in reverse order from input order. # Ruby, 24 bytes ->x,y{(x**y).digits.sum}  Try it online! # JavaScript (Node.js), 36 bytes X=>g=(Y,d=X**Y)=>d&&d%10+g(Y,d/10|0)  Try it online! # Perl 6, 16 bytes (* ***).comb.sum  Try it online! # JavaScript (Node.js), 37 32 bytes -5 bytes thanks to @Shaggy. X=>Y=>eval([...''+X**Y].join+)  Try it online! # Explanation : X => // First input X Y => // second input Y eval( // evaluate [...''+ // convert what is next to string and map to array X**Y]. // X raised to power Y join+ // join them into a string with a plus sign between each ) // The whole thing gets evaluated using eval  • 32 bytes – Shaggy Apr 28 '18 at 17:41 # Gol><>, 11 bytes XWaSD}|lR+B  Try it online! A function that accepts X and Y on the stack and leaves the result as the only value on the stack. ### Example full program & How it works 1AGIE;IGN XWaSD}|lR+B 1AG Register row 1 as function G IE; Take input X as int; halt if EOF I Take input Y as int GN Call G with stack [X Y] and print the result as int Repeat indefinitely X n = X**Y W | While top is nonzero... aSD Pop n, push n / 10 and n % 10 } Move the remainder to the bottom Now the stack contains the digits lR+ Sum everything B Return  # Stax, 4 bytes ║▼δ&  Run and debug it Unpacked: |*E|+  Run and debug this one Explanation: |*E|+ Full program, implicit input Stack: 5 2 |* Power Stack: 25 E Decimal digits Stack: [ 2, 5 ] |+ Sum array Stack: 7 Implicit output  ## PHP, 44 Bytes No loop, passing both arguments to the script. Try it Online Code <?=array_sum(str_split($argv[0]**\$argv[1]));

This just takes both arguments calculates X to the power of Y, explodes the string into an array and adds each digit