Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

A simple but hopefully not quite trivial challenge:

Write a program or function that adds up the kth 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 kth 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.

share|improve this question
9  
When I first saw your challenge, I had the weird feeling that it was a Metallica song title. – Arnauld 10 hours ago
    
What? There's no Mathematica built-in for this? – boboquack 2 hours ago

19 Answers 19

05AB1E, 9 bytes

DLImDŠÖÏO

Try it online!

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
share|improve this answer

Mathematica, 28 bytes

Tr[Divisors@#⋂Range@#^#2]&

Unnamed function taking n and k as inputs in that order.

share|improve this answer
    
DivisorSum is frustratingly close to being useful here. – ngenisis 5 hours ago

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?

share|improve this answer
4  
Time to upgrade. – TuukkaX 14 hours ago

Haskell, 37 35 34 bytes

n!k=sum[x^k|x<-[1..n],n`mod`x^k<1]

Try it online! 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
share|improve this answer
1  
mod n(x^k) can be n`mod`x^k. – Zgarb 11 hours ago

MATL, 10 bytes

t:i^\~5M*s

Try it online!

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
share|improve this answer

Python 2.X, 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.

share|improve this answer
    
You can replace x%i**n==0 with x%i**n<1, and move to the other side as i**n*(x%i**n<1) – Rod 13 hours ago

JavaScript (ES7), 56 53 bytes

Takes n and k in currying syntax (n)(k).

n=>k=>[...Array(n)].reduce(p=>n%(a=++i**k)?p:p+a,i=0)

Test cases

let f =

n=>k=>[...Array(n)].reduce(p=>n%(a=++i**k)?p:p+a,i=0)

console.log(f(40320)(1)) // -> 159120
console.log(f(40320)(2)) // -> 850
console.log(f(40320)(3)) // -> 73
console.log(f(40320)(4)) // -> 17
console.log(f(40320)(5)) // -> 33
console.log(f(40320)(6)) // -> 65
console.log(f(40320)(7)) // -> 129
console.log(f(40320)(8)) // -> 1
console.log(f(46656)(1)) // -> 138811
console.log(f(46656)(2)) // -> 69700
console.log(f(46656)(3)) // -> 55261
console.log(f(46656)(4)) // -> 1394
console.log(f(46656)(5)) // -> 8052
console.log(f(46656)(6)) // -> 47450
console.log(f(46656)(7)) // -> 1

share|improve this answer

Perl 6, 39 bytes

->\n,\k{sum grep n%%*,({++$**k}...*>n)}

How it works

->\n,\k{                              }  # A lambda taking two arguments.
                        ++$**k           # Increment a counter and raise it to the power k,
                       {      }...       # and 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.
share|improve this answer

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!

share|improve this answer
    
Does vU detect numbers divisible by U, or numbers that divide U? – Greg Martin 6 hours ago
    
@GregMartin fvU filters to items that are divisible by U; f!vU filters to items that U is divisible by. ! swaps the arguments. – obarakon 6 hours ago
    
Cool, so the code looks right, but the explanation might need to be tweaked. – Greg Martin 6 hours ago
    
@GregMartin Should be clearer now. – ETHproductions 6 hours ago

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 !

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!)
share|improve this answer

CJam, 20 bytes

Probably not optimally golfed, but I don't see any obvious changes to make...

ri:N,:)rif#{N\%!},:+

Try it online!

share|improve this answer

Scala 63 bytes

(n:Int,k:Int)=>1 to n map{Math.pow(_,k).toInt}filter{n%_==0}sum
share|improve this answer

Python, 56 bytes

lambda n,k:sum(j*(j**k**-1%1==n%j)for j in range(1,n+1))

Try it online!

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.

share|improve this answer

Python 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! Large inputs may exceed the recursion depth depending on your system and implementation.

share|improve this answer

Jelly, 7 6 bytes

-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!

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
share|improve this answer

Bash + Unix utilities, 44 bytes

bc<<<`seq "-fx=%.f^$2;s+=($1%%x==0)*x;" $1`s

Try it online!

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
share|improve this 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. Won't work near the limits of 32-bit integer arithmetic, and very slow when k=1.

share|improve this answer

JavaScript (ES7), 49 bytes

(n,k)=>(g=(t,p=++i**k)=>p>n?t:g(t+p*!(n%p)))(i=0)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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