-5
\$\begingroup\$

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.

\$\endgroup\$
  • 4
    \$\begingroup\$ You shouldn't immediately answer your own question. It is generally frowned upon. \$\endgroup\$ – fəˈnɛtɪk Apr 28 '18 at 14:19
  • 1
    \$\begingroup\$ related \$\endgroup\$ – fəˈnɛtɪk Apr 28 '18 at 14:20
  • 1
    \$\begingroup\$ 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? \$\endgroup\$ – ElPedro Apr 28 '18 at 22:04
  • 1
    \$\begingroup\$ @ElP See codegolf.stackexchange.com/questions/156100/… \$\endgroup\$ – user202729 Apr 29 '18 at 2:04
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Kelly Lowder Apr 29 '18 at 3:11

19 Answers 19

2
\$\begingroup\$

Pari/GP, 21 bytes

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

Try it online!

\$\endgroup\$
2
\$\begingroup\$

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

\$\endgroup\$
  • 1
    \$\begingroup\$ 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! \$\endgroup\$ – Dennis Apr 28 '18 at 14:45
2
\$\begingroup\$

Jelly, 3 bytes

Jelly trivialises this task

*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
\$\endgroup\$
1
\$\begingroup\$

Python 3, 34 bytes

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

APL+WIN, 8 bytes

Prompts for y followed by x:

+/⍎¨⍕⎕*⎕
\$\endgroup\$
1
\$\begingroup\$

Neim, 2 bytes

𝕎𝐬

Power 𝕎; implicitly coerce to list; sum 𝐬

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Funky, 31 bytes

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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

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

Haskell, 29 28 bytes

x%y=sum[read[d]|d<-show$x^y]

Try it online!

-1 byte thanks to user9549915

\$\endgroup\$
1
\$\begingroup\$

Charcoal, 6 bytes

IΣIXNN

Try it online! Link is to verbose version of code. Explanation:

    N   First input as a number
     N  Second input as a number
   X    Power
  I     Cast to string
 Σ      Sum of digits
I       Cast to string
        Implicitly print
\$\endgroup\$
  • \$\begingroup\$ One of the few times such a hacky sum builtin is useful :P \$\endgroup\$ – ASCII-only May 2 '18 at 8:18
1
\$\begingroup\$

Japt, 5 bytes

Saved 1 byte thanks to @Shagy

pV ìx

Try it online!

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Shaggy May 14 '18 at 13:06
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

Ruby, 24 bytes

->x,y{(x**y).digits.sum}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

JavaScript (Node.js), 36 bytes

X=>g=(Y,d=X**Y)=>d&&d%10+g(Y,d/10|0)

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Perl 6, 16 bytes

(* ***).comb.sum

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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`

\$\endgroup\$
  • \$\begingroup\$ 32 bytes \$\endgroup\$ – Shaggy Apr 28 '18 at 17:41
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.