wu :: forums (http://www.ocf.berkeley.edu/~wwu/cgi-bin/yabb/YaBB.cgi)
riddles >> cs >> sum of lcm's
(Message started by: dormant on Jan 29th, 2010, 1:24am)

Title: sum of lcm's
Post by dormant on Jan 29th, 2010, 1:24am
I have to find:
summation (lcm(i,n)) for i=1 to n,n<=10^6

I was thinking of  

summation i/gcd(i,n)

But i can't any efficient way to find this.

need help. :)

P.S->I have wrote this question in pure maths section also.
Apology, :)

Title: Re: sum of lcm's
Post by towr on Jan 29th, 2010, 2:13am

on 01/29/10 at 01:24:31, dormant wrote:
P.S->I have wrote this question in pure maths section also.
Apology, :)
I've removed the thread in the maths section.



Quote:
I was thinking of  

summation i/gcd(i,n)
You would want summation n*i/gcd(i,n)
After all gcd(i,n)*lcm(i,n)=i*n

You can use the Euclidean algorithm (http://en.wikipedia.org/wiki/Euclidean_algorithm) to calculate the gcd fairly easily.

I'll think about it some more whether there is a better way.

[e]See also A051193 (http://www.research.att.com/~njas/sequences/A051193)[/e]

Title: Re: sum of lcm's
Post by towr on Jan 29th, 2010, 5:01am
If you can find a prime decomposition of n, you can use the fact that

sum_of_lcm(n) = n * (a(n)+1)/2
where we have that a(k*l) = a(k)*a(l), and for primes p: a(p) = p*(p-1)+1

And since n <= 106, it's not too difficult to find the prime factorization (just try all numbers under 1000, or do it even a bit smarter).

Title: Re: sum of lcm's
Post by dormant on Jan 29th, 2010, 9:13am

Quote:
sum_of_lcm(n) = n * (a(n)+1)/2
where we have that a(k*l) = a(k)*a(l), and for primes p: a(p) = p*(p-1)+1  


how we arrive at this formula?

Any special name for this a(k) function?

Title: Re: sum of lcm's
Post by dormant on Jan 29th, 2010, 9:42am
Towr,
Thanx for the link to Encyclopedia,
I never thought of searching it there.

But,
I think
a(n) is sum {d*phi(d)} for d|n.
So ,taking only prime factorization can cause trouble.

Title: Re: sum of lcm's
Post by towr on Jan 29th, 2010, 3:16pm
On the page describing the sequence a(n) = sum{d|n} {d*phi(d)}, A057660 (http://www.research.att.com/~njas/sequences/A057660), there is some further useful information. Such as the a(k*l) = a(k)*a(l) I mentioned. But as it turns out that's not entirely correct, it only hold if gcd(k,l)=1. Which means we can use the prime factorization, but we need to be careful if a prime factor occurs more than once; namely, that page also says a(pe) = (p2e+1+1)/(p+1)

So, we can use the following algorithm:

javascript
Code:
<script>

prime = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]

function a(n)
{
res=1;
for(i=0; i < prime.length && 1 < n && prime[i] < n; i++)
{
 x = prime[i];
 while(n % prime[i]==0)
 {
  x *= prime[i]*prime[i];
  n /= prime[i];
 }
 res *= (x+1)/(prime[i]+1);
}
if(n > 1)
 res*=(n*(n-1)+1);

return res;
}

function sum_of_lcm(n)
{
 return n*(a(n)+1)/2;
}

for(n=1; n <= 20; n++)
document.write(n + ": " + sum_of_lcm(n) + "<br>");
document.close();

</script>

Title: Re: sum of lcm's
Post by holtz on May 26th, 2012, 12:44am
Hello,

I want to find

lcm(m,n)+lcm(m+1,n)+...+lcm(n,n)

where m<=n.

How can it be done? Can it be done similarly to the method described above?

Thanks

Title: Re: sum of lcm's
Post by towr on May 26th, 2012, 1:22am
Yes, just take the sum from 1-n and subtract the sum of 1-m-1, this leaves the sum from m-n

Title: Re: sum of lcm's
Post by holtz on May 26th, 2012, 2:34am
I think it wouldn't work, because:

sum of 1..m-1 is
   (m-1)*(a(m-1)+1)/2 =  lcm(1,m-1)+lcm(2,m-1)+...+lcm(m-1,m-1)

but what we need to find is
   lcm(1, n)+lcm(2,n)+...+lcm(m-1,n)

in order to subtract from sum 1..n and get the result.


So, how can we calculate  lcm(1, n)+lcm(2,n)+...+lcm(m-1,n), that is the question.


Title: Re: sum of lcm's
Post by towr on May 26th, 2012, 4:05am
Ah, you're right.. There might still be some way to adapt the algorithm, but I'll have to think on it a bit more..

Title: Re: sum of lcm's
Post by akasina9 on Nov 16th, 2012, 12:12am
For the sum lcm(m,n)+lcm(m+1,n)+...+lcm(n,n), you can proceed in the same way as calculating the sum lcm(1,n)+lcm(2,n)+...+lcm(n,n), but hold the value of the sum when you hit i=m in a temporary location and subtract it from the final achieved sum.   :)

Title: Re: sum of lcm's
Post by towr on Nov 16th, 2012, 8:37am
That's not terribly helpful unless you can calculate both in a fast way.

Title: Re: sum of lcm's
Post by akasina9 on Nov 17th, 2012, 12:50am

on 11/16/12 at 08:37:23, towr wrote:
That's not terribly helpful unless you can calculate both in a fast way.


We need to compute just one value, namely lcm(1,n)+lcm(2,n)+...+lcm(n,n). We just store the intermediate value. So what are the 2 sums you are referring to?
Did I miss something?

Title: Re: sum of lcm's
Post by towr on Nov 17th, 2012, 2:54am
Well, just that in that case it's faster to just skip the first m terms. There's no reason to compute them, just to subtract them again later.
Unless there's a shortcut to compute the sums.

Title: Re: sum of lcm's
Post by birbal on Dec 13th, 2012, 10:15am

on 01/29/10 at 05:01:40, towr wrote:
If you can find a prime decomposition of n, you can use the fact that

sum_of_lcm(n) = n * (a(n)+1)/2
where we have that a(k*l) = a(k)*a(l), and for primes p: a(p) = p*(p-1)+1

And since n <= 106, it's not too difficult to find the prime factorization (just try all numbers under 1000, or do it even a bit smarter).

Does sum_of_lcm(n) means summition lcm(i,n) ?
How is a(p) defined for non-primes ?

Title: Re: sum of lcm's
Post by towr on Dec 13th, 2012, 11:23am

on 12/13/12 at 10:15:40, birbal wrote:
Does sum_of_lcm(n) means summition lcm(i,n) ?
sum_of_lcm(n) means whatever was asked in the opening post that resembles it most closely  :P


Quote:
How is a(p) defined for non-primes ?
Exactly as it says in the quoted text: a(k*l) = a(k)*a(l)
So just prime-decompose n. (But bear in mind reply #5)



Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board