1
$\begingroup$

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

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

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.

$\endgroup$
  • 1
    $\begingroup$ Important to note, I think, is that you can be sure that in-built functions like divisors are highly optimised. $\endgroup$ – Myridium Dec 3 '17 at 1:22
  • $\begingroup$ 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? $\endgroup$ – Basj Dec 3 '17 at 19:14
  • $\begingroup$ @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)); $\endgroup$ – Sil Dec 3 '17 at 19:48
2
$\begingroup$

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
$\endgroup$
  • $\begingroup$ 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). $\endgroup$ – Basj Dec 3 '17 at 19:10
  • $\begingroup$ 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)) $\endgroup$ – acer Dec 6 '17 at 18:15
2
$\begingroup$

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))
$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Basj Dec 3 '17 at 19:12
  • $\begingroup$ "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? $\endgroup$ – Therkel Dec 5 '17 at 7:44
  • $\begingroup$ @Therkel I asked the OP and he wanted to see the solution under Matlab $\endgroup$ – Brethlosze Dec 5 '17 at 15:27

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.