How to do a sum of a function over all divisors of an integer with Maple?

473 Views Asked by At

I'd like to define sumdiv in Maple such that this:

with(numtheory);
f:=x->x^2;
sumdiv(f(d)*mobius(100/d), d=1..100);

would do a sum on all divisors d of $100$.

How to do such a sum over divisors in Maple?

Here's what I've tried:

isdivisible:=(a,b)->if a mod b = 0 then 1 else 0 fi;
sum(f(d)*mobius(100/d)*isdivisible(100,d), d=1..100);

but even if isdivisible(100,d) is $0$ (i.e. $d$ not divisible by $100$), it tries to evaluate mobius(100/d) anyway which is impossible, thus an error.

Error, (in mobius) invalid arguments

3

There are 3 best solutions below

3
On

In Matlab, D is a vector containing all the divisors, for any n:

n=10;
k=1:n;
D=K(rem(n,k)==0);
s=sum(D)

Edit: The sum of a function f over the divisors of n. Note the . operator before the ^ and * operators, for applying them component-wise instead of matrix-wise (default).

n=10;
k=1:n;
D=K(rem(n,k)==0);
f=@(x)(x.^2+2.*x+sin(x)+1);
s=sum(f(D))
3
On

Just use divisors function:

> divisors(100);
{1, 2, 4, 5, 10, 20, 25, 50, 100}

So for example:

> add(f(d)*mobius(100/d), d in divisors(100));
7200

The add allows to iterate over set as oposed to sum which supports only range a..b.

2
On

You can do this by adding up the results from the Maple command numtheory[divisors], or you could go more directly to the Maple command numtheory[sigma].

restart;

`+`(op(numtheory[divisors](100)));

                                 217

numtheory[sigma](100);

                                 217

F1 := n -> `+`(op(numtheory[divisors](n))):
F2 := n -> numtheory[sigma](n):

seq( F1(i), i = 10 .. 20 );

             18, 12, 28, 14, 24, 24, 31, 18, 39, 20, 42

seq( F2(i), i = 10 .. 20 );

             18, 12, 28, 14, 24, 24, 31, 18, 39, 20, 42