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

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

• I prefer Matlab... – Brethlosze Dec 3 '17 at 0:57
• @hyprfrcb How would the code look like in Matlab? – Basj Dec 3 '17 at 1:00

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.

• Important to note, I think, is that you can be sure that in-built functions like divisors are highly optimised. – Myridium Dec 3 '17 at 1:22
• Thanks @Sil. I use good old Maple 7, it seems that d in ... is not allowed in add. Which minimum version is needed for this? – Basj Dec 3 '17 at 19:14
• @Basj Hm I am not sure, I am using Maple 2017. But anyway you can get around this by simply indexing the set, something like divs:=divisors(100): add(f(divs[i])*mobius(100/divs[i]), i=1..nops(divs)); – Sil Dec 3 '17 at 19:48

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

• I think there's a misunderstanding: you speak about the sum of all divisors of n, thus the use of sigma. I speak about the sum of f(d), where d goes through the set of all divisors of n (for a function f). – Basj Dec 3 '17 at 19:10
• Oh sorry I misunderstood. In that case you can just take Sil's suggestion, which in older Maple versions could be done with add(f(d)*mobius(100/d), d=divisors(100)) – acer Dec 6 '17 at 18:15

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

• Thanks @hyprfrcb, but I don't want to do the sum of all divisors of n but the sum of a function, when the variable goes through all divisors of n. – Basj Dec 3 '17 at 19:12
• "How to do such a sum over divisors in Maple?". Since this answer is in the language of Matlab, how does this answer the question? – Therkel Dec 5 '17 at 7:44
• @Therkel I asked the OP and he wanted to see the solution under Matlab – Brethlosze Dec 5 '17 at 15:27