[Question]
  [
A simple but hopefully not quite trivial challenge:
Write a program or function that adds up the `k`th powers dividing a number `n`. More specifically:
* Input: two positive integers `n` and `k` (or an ordered pair of integers, etc.)
* Output: the sum of all of the positive divisors of `n` that are `k`th powers of integers
For example, 11! = 39916800 has six divisors that are cubes, namely 1, 8, 27, 64, 216, and 1728. Therefore given inputs `39916800` and `3`, the program should return their sum, `2044`.
Other test cases:
```
{40320, 1} -> 159120
{40320, 2} -> 850
{40320, 3} -> 73
{40320, 4} -> 17
{40320, 5} -> 33
{40320, 6} -> 65
{40320, 7} -> 129
{40320, 8} -> 1
{46656, 1} -> 138811
{46656, 2} -> 69700
{46656, 3} -> 55261
{46656, 4} -> 1394
{46656, 5} -> 8052
{46656, 6} -> 47450
{46656, 7} -> 1
{1, [any positive integer]} -> 1
```
This is code golf, so the shorter your code, the better. I welcome golfed code in all kinds of different languages, even if some other language can get away with fewer bytes than yours.
  
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 9 bytes
```
DLImDŠÖÏO
```
[Try it online!](https://tio.run/nexus/05ab1e#@@/i45nrcnTB4WmH@/3//zcxMzM14zIGAA "05AB1E – TIO Nexus")
**Explanation**
Example input `46656, 3`
```
D          # duplicate first input
           # STACK: 46656, 46656
 L         # range [1 ... first input]
           # STACK: 46656, [1 ... 46656]
  Im       # each to the power of second input
           # STACK: 46656, [1, 8, 27 ...]
    D      # duplicate
           # STACK: 46656, [1, 8, 27 ...], [1, 8, 27 ...]
     Š     # move down 2 spots on the stack
           # STACK: [1, 8, 27 ...], 46656, [1, 8, 27 ...]
      Ö    # a mod b == 0
           # STACK: [1, 8, 27 ...], [1,1,1,1,0 ...]
       Ï   # keep only items from first list which are true in second
           # STACK: [1, 8, 27, 64, 216, 729, 1728, 5832, 46656]
        O  # sum
           # OUTPUT: 55261
```
[Answer]
## Mathematica, 28 bytes
```
Tr[Divisors@#⋂Range@#^#2]&
```
Unnamed function taking `n` and `k` as inputs in that order.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~7~~ 6 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-Page)
-1 byte thanks to Dennis (traverse an implicit range)  
A clever efficiency save **also** by Dennis at **0-byte cost**  
(Previously `ÆDf*€S` would filter keep those divisors that are a power of **k** of any natural number up to **n**. But note that **n** can only ever have a divisor of **ik** if it has a divisor of **i** anyway!)
```
ÆDf*¥S
```
**[Try it online!](https://tio.run/nexus/jelly#@3@4zSVN69DS4P///5uYmZma/TcGAA)**
### How?
```
ÆDf*¥S - Main link: n, k
ÆD     - divisors of n  -> divisors = [1, d1, d2, ..., n]
    ¥  - last two links as a dyadic chain
  f    -     filter divisors keeping those that appear in:
   *   -     exponentiate k with base divisors (vectorises)
       - i.e. [v for v in [1, d1, d2, ..., n] if v in [1^k, d1^k, ..., n^k]]
     S - sum
```
[Answer]
# [Haskell](https://www.haskell.org/), ~~37 35~~ 34 bytes
```
n!k=sum[x^k|x<-[1..n],n`mod`x^k<1]
```
[Try it online!](https://tio.run/nexus/haskell#@5@nmG1bXJobXRGXXVNhoxttqKeXF6uTl5Cbn5IAFLMxjP2fm5iZp2CrUFCUmVeioKJgYmBsZKBo9B8A "Haskell – TIO Nexus") Usage:
```
Prelude> 40320 ! 1
159120
```
The code is quite inefficient because it always computes `1^k, 2^k, ..., n^k`.
**Edit:** Saved one byte thanks to Zgarb.
**Explanation:**
```
n!k=             -- given n and k, the function ! returns
 sum[x^k|        -- the sum of the list of all x^k
   x<-[1..n],    -- where x is drawn from the range 1 to n
   n`mod`x^k<1]  -- and n modulus x^k is less than 1, that is x^k divides n
```
[Answer]
# Python 2, ~~54~~ 52 bytes
```
lambda x,n:sum(i**n*(x%i**n<1)for i in range(1,-~x))
```
Thanks @Rod for cutting off 2 bytes.
[Answer]
## Ruby, 45 bytes
```
->n,m{(1..n).reduce{|a,b|n%(c=b**m)<1?a+c:a}}
```
Would be shorter using "sum" in Ruby 2.4. Time to upgrade?
[Answer]
# [MATL](https://github.com/lmendo/MATL), 10 bytes
```
t:i^\~5M*s
```
[Try it online!](https://tio.run/nexus/matl#@19ilRkXU2fqq1X8/7@JmZmpGZcZAA "MATL – TIO Nexus")
### How it works
Example with `46656`, `6`.
```
t      % Implicitly input n. Duplicate
       % STACK: 46656, 46656
:      % Range
       % STACK: 46656, [1 2 ... 46656]
i      % Input k
       % STACK: 46656, [1 2 ... 46656], 6
^      % Power, element-wise
       % STACK: 46656, [1 64 ... 46656^6]
\      % Modulo
       % STACK: [0 0 0 1600 ...]
~      % Logically negate
       % STACK: [true true true false ...]
5M     % Push second input to function \ again
       % STACK: [true true true false ...], [1^6 2^6 ... 46656^6]
*      % Multiply, element-wise
       % STACK: [1 64 729 0 ...]
s      % Sum of array: 47450
       % Implicitly display
```
[Answer]
# [Perl 6](http://perl6.org/), 39 bytes
```
->\n,\k{sum grep n%%*,({++$**k}...*>n)}
```
### How it works
```
->\n,\k{                              }  # A lambda taking two arguments.
                        ++$              # Increment an anonymous counter
                           **k           # and raise it to the power k,
                       {      }...       # generate a list by repeatedly doing that,
                                  *>n    # until we reach a value greater than n.
            grep n%%*,(              )   # Filter factors of n from the list.
        sum                              # Return their sum.
```
[Try it](https://tio.run/nexus/perl6#TZC7boMwGEZ3nuIfSAk3FzDmUhqkbh26NWOkCoGbIIJBIUSNEI/dOTV2Al2wv@PP9jF9R@ESoDxRej7b0u6cKEp9hae8KShsbna6Y9auGrq@hv2JtsBWK8NaD6apGkY1IoSMlOnj7bs5wbFktFvrMCgA9Us3DQDaoIkRQH1lKWxgV5iCW5qE1R3K9iioZqf3VfrT0vxMi6U0JvyjPvOsPqM2K08dqrN2PQCq6BU2KZjokh17CqOO3rPuwN8DUHYwPWg9OVjTnbr1/3AevkTv4@1zC0XDqH3mv6Jk@0QZb4PvYM@xwB3BTsElses5ygN6AkZkIViQEM/Al/vCGRAB8NIIBAjIDEK5xYtnEknCcxCQYJbBUeQuUMoEceg4M5M6hHjB0rsb4difkXSKHOLNSFr5oU@W08KHhssVMOZR5F/W2HmWH@gf "Perl 6 – TIO Nexus")
[Answer]
# [Python 2](https://docs.python.org/2/), 50 bytes
```
f=lambda n,k,i=1:n/i and(n%i**k<1)*i**k+f(n,k,i+1)
```
[Try it online!](https://tio.run/nexus/python2#@59mm5OYm5SSqJCnk62TaWtolaefqZCYl6KRp5qppZVtY6ipBaK10zTACrQNNf8XFGXmlSikaRjrGGpywTlmOkYInrmRpY6x5n8A "Python 2 – TIO Nexus") Large inputs may exceed the recursion depth depending on your system and implementation.
[Answer]
# JavaScript (ES7), ~~56 53~~ 47 bytes
Expects `(n)(k)`.
```
n=>k=>eval("for(i=t=0;i++<n;)t+=n%i**k?0:i**k")
```
[Try it online!](https://tio.run/##ddDRDoIgFIDh@57CubWBzkLgAFbYszjTRjpo5np9WzdiDa7Ozbdzzv5H825e7WSec2HdrVt6vVhdD7ru3s2I0t5NyOhZk7PJ84s94znXdm@ybLiS03ekeGmdfbmxO4zujnrECaMEoxLj5HhMijopoSop2QUVXZWCCGErkSwsuD8lwwJWwSI7xCoEhIX0V2gVJsqTfyAEiJ8oTKkyonwUUUlCwshnAaAismlThlU8bHwbRYCGja/DJYfIR5tAywc "JavaScript (Node.js) – Try It Online")
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 10 bytes
*Saved lots of bytes thanks to @ETHproductions*
```
òpV f!vU x
```
## Explanation
```
òpV f!vU x
ò           // Creates a range from 0 to U
 pV         // Raises each item to the power of V (Second input)
    f       // Selects all items Z where
     !vU    //   U is divisible by Z
            //   (fvU would mean Z is divisible by U; ! swaps the arguments)
         x  // Returns the sum of all remaining items
```
[Test it online!](http://ethproductions.github.io/japt/?v=1.4.4&code=8nBWIGYhdlUgeA==&input=NDAzMjAsMg==)
[Answer]
# Scala 63 bytes
```
(n:Int,k:Int)=>1 to n map{Math.pow(_,k).toInt}filter{n%_==0}sum
```
[Answer]
## JavaScript (ES7), ~~49~~ 46 bytes
```
n=>g=(k,t=i=0,p=++i**k)=>p>n?t:g(k,t+p*!(n%p))
```
[Answer]
# [APL (Dyalog Classic)](https://www.dyalog.com/), 14 bytes
```
+/*⍨∘⍳(⊣×0=|)⊢
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT9dNzkksLs5M/v@ob6pb0KO2CYZGFub/tfW1HvWueNQx41HvZo1HXYsPTzewrdF81LXofxpQyaPevkddzYfWGz9qmwjUFhzkrACkQjw8g/8bKqQpmBgYGxlwGcFZxnCWCZxlCmeZwVnmcJYFnAU2z8zM1AxiHphlDGeZwFmmcJYZnGUOZwEA "APL (Dyalog Classic) – Try It Online")
Takes the power as the left argument and the number as the right. Even at 128-bit precision, the last couple of test cases exceed the max int size.
### Explanation:
```
     ⍳            ⍝ The range 1 to right argument
  *⍨∘             ⍝ Each raised to the power of the left argument
      (     )⊢    ⍝ Apply the train to this and the right argument
       ⊣×         ⍝ Multiply each power
         0=|      ⍝ By if the right argument is divisible by it
+/                ⍝ And sum
```
[Answer]
# [Pip](https://github.com/dloscutoff/pip), 15 bytes
```
$+Ya%_FN(\,a)Eb
```
[Try It Online!](https://dso.surge.sh/#@WyJwaXAiLCIiLCIkK1lhJV9GTihcXCxhKUViIiwiIiwiMzYwIDIiLCItcCJd)
[Answer]
# PHP, 86 bytes
```
$n=$argv[1];$k=$argv[2];for($i=1;$i<=$n**(1/$k);$i++)if($n%$i**$k<1)$s+=$i**$k;echo$s;
```
[Try it here !](https://eval.in/734616)
**Breakdown :**
```
$n=$argv[1];$k=$argv[2];       # Assign variables from input
for($i=1;$i<=$n**(1/$k);$i++)  # While i is between 1 AND kth root of n
    if($n%$i**$k<1)            #     if i^k is a divisor of n
        $s+=$i**$k;            #         then add to s
echo$s;                        # echo s (duh!)
```
[Answer]
# [CJam](https://sourceforge.net/p/cjam), 20 bytes
Probably not optimally golfed, but I don't see any obvious changes to make...
```
ri:N,:)rif#{N\%!},:+
```
[Try it online!](https://tio.run/nexus/cjam#@1@UaeWnY6VZlJmmXO0Xo6pYq2Ol/f@/iZmZqZmCCQA "CJam – TIO Nexus")
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 8 bytes
```
R*³%$ÐḟS
```
[Try it online!](https://tio.run/nexus/jelly#@x@kdWizqsrhCQ93zA/@//@/iZmZqdl/cwA "Jelly – TIO Nexus")
([Credit not mine.](/a/109659/41024))
[Answer]
# [Bash](https://www.gnu.org/software/bash/) + Unix utilities, 44 bytes
```
bc<<<`seq "-fx=%.f^$2;s+=($1%%x==0)*x;" $1`s
```
[Try it online!](https://tio.run/nexus/bash#@5@UbGNjk1CcWqigpJtWYauqlxanYmRdrG2roWKoqlpha2ugqVVhraSgYphQ/P//fxMzM1Oz/2YA "Bash – TIO Nexus")
Test runs:
```
for x in '40320 1' '40320 2' '40320 3' '40320 4' '40320 5' '40320 6' '40320 7' '40320 8' '46656 1' '46656 2' '46656 3' '46656 4' '46656 5' '46656 6' '46656 7' '1 1' '1 2' '1 3' '1 12' ; do echo -n "$x "; ./sumpowerdivisors $x; done
40320 1 159120
40320 2 850
40320 3 73
40320 4 17
40320 5 33
40320 6 65
40320 7 129
40320 8 1
46656 1 138811
46656 2 69700
46656 3 55261
46656 4 1394
46656 5 8052
46656 6 47450
46656 7 1
1 1 1
1 2 1
1 3 1
1 12 1
```
[Answer]
# [Python](https://docs.python.org/), 56 bytes
```
lambda n,k:sum(j*(j**k**-1%1==n%j)for j in range(1,n+1))
```
[Try it online!](https://tio.run/nexus/python3#bctBCsIwFIThvaeYzaN5MYVGbdBCPImbSqy0aaOkuhLPXiOoiAiz@BfzNbDYTX097F2NoHw1XgfRyTTppcw1aWsDddycIjq0AbEOx4PQKsw18zQmnt2odArk7si3IOOy2fPtv9/YcIVzbMNFjCCIVbFcFApeofk0M/@B6x9oTGne8NUJTg8 "Python 3 – TIO Nexus")
Fairly straightforward. The only noteworthy thing is that `j**k**-1%1` always returns a float in **[0,1)** while `n%j` always returns a non-negative integer, so they can only be equal if both are **0**.
[Answer]
## Batch, 138 bytes
```
@set s=n
@for /l %%i in (2,1,%2)do @call set s=%%s%%*n
@set/at=n=0
:l
@set/an+=1,p=%s%,t+=p*!(%1%%p)
@if %p% lss %1 goto l
@echo %t%
```
Since Batch doesn't have a power operator, I'm abusing `set/a` as a form of `eval`. Very slow when `k=1`. 32-bit integer arithmetic limits the supported values of `n` and `k`:
```
           n   k
  (too slow)   1
 <1366041600   2
 <1833767424   3
 <2019963136   4
 <2073071593   5
 <1838265625   6
 <1801088541   7
 <1475789056   8
 <1000000000   9
 <1073741824  10
 <1977326743  11
  <244140625  12
 <1220703125  13
  <268435456  14
 <1073741824  15
   <43046721  16
  <129140163  17
  <387420489  18
 <1162261467  19
    <1048576  20
           ...
 <1073741824  30
```
[Answer]
# [R](https://www.r-project.org/), 35 bytes
```
function(n,k)sum((a=(1:n)^k)*!n%%a)
```
[Try it online!](https://tio.run/##K/pfXJobX5KRGl@QX55aVAxkJpbEJ6Xa/k8rzUsuyczP08jTydYEKtLQSLTVMLTK04zL1tRSzFNVTdT8X5xYUJBTCRS10MFujE6erYmBsZGBJhdcqTkepWZmpmZApVjlNQx1DA0MNP8DAA "R – Try It Online")
3 years later, but 6 bytes shorter than the [previous R answer](https://codegolf.stackexchange.com/questions/109657/sum-the-powers-that-be/109804#109804).
[Answer]
# [Husk](https://github.com/barbuz/Husk), ~~10 9~~ 8 bytes
```
ΣnḊ³m^⁰ḣ
```
[Try it online!](https://tio.run/##ASAA3/9odXNr///Oo27huIrCs21e4oGw4bij////NDAzMjD/Mg "Husk – Try It Online")
-1 byte from Jo King.
-1 more byte from Jo King.
## Explanation
```
ΣnḊ³m^⁰ḣ
       ḣ range 1..n
    m^⁰  each raised to power k
 n       set intersection with
  Ḋ³     divisors of n
Σ        sum the list
```
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal), 6 bytes
```
K:?e↔∑
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyIiLCIiLCJLOj9l4oaU4oiRIiwiIiwiNDY2NTZcbjMiXQ==)
[Answer]
# [Japt](https://github.com/ETHproductions/japt) [`-x`](https://codegolf.meta.stackexchange.com/a/14339/), 9 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
```
ÆvX=pV)*X
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&flags=LXg&code=xnZYPXBWKSpY&input=NDAzMjAKMw)
## 8 bytes
My first pass. Realised as I was about to post it that it's pretty much identical to Oliver's existing solution.
```
opV f@vX
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&flags=LXg&code=b3BWIGZAdlg&input=NDAzMjAKMw)
```
ÆvX=pV)*X     :Implicit input of integers U=n & V=k
Æ             :Map each X in the range [0,U)
 v            :  Is U divisible by
  X=          :    Reassign to X
    pV        :    X raised to the power of V
      )       :  End divisibility check
       *X     :  Multiply by X
              :Implicit output of sum of resulting array
```
[Answer]
# R, 28 bytes direct, 43 bytes for function
if n,k in memory:
```
sum((n%%(1:n)^k==0)*(1:n)^k)
```
for a function: 
```
r=function(n,k)sum((n%%(1:n)^k==0)*(1:n)^k)
```
]