Skip to content

Commit 364b32e

Browse files
committed
new file: Math/partial_sums_of_gcd-sum_function_fast.sf
1 parent 6ab365e commit 364b32e

File tree

3 files changed

+109
-9
lines changed

3 files changed

+109
-9
lines changed

Math/partial_sums_of_gcd-sum_function.sf

+16-9
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,28 @@
4040
func partial_sums_of_gcd_sum_function(n) {
4141
var s = n.isqrt
4242

43+
var lookup_size = (2 * n.iroot(3)**2) # O(n^(2/3))
44+
4345
var mertens_lookup = [0,1]
4446
var euler_sum_lookup = [0,1]
4547

46-
var lookup_size = n.iroot(3)**2 # O(n^(2/3))
48+
var euler_phi_lookup = [0]
49+
var moebius_lookup = ::moebius(0, lookup_size)
4750

4851
for i in (1 .. lookup_size) {
49-
mertens_lookup[i] = (mertens_lookup[i-1] + i.moebius)
50-
euler_sum_lookup[i] = (euler_sum_lookup[i-1] + i.euler_phi)
52+
mertens_lookup[i] = (mertens_lookup[i-1] + moebius_lookup[i])
53+
euler_sum_lookup[i] = (euler_sum_lookup[i-1] + (euler_phi_lookup[i] = i.euler_phi))
5154
}
5255

53-
func moebius_partial_sum(n) {
56+
var mertens_cache = Hash()
57+
58+
func moebius_partial_sum (n) {
5459

5560
if (n <= lookup_size) {
5661
return mertens_lookup[n]
5762
}
5863

59-
n.mertens
64+
mertens_cache{n} := n.mertens
6065
}
6166

6267
func euler_phi_partial_sum(n) {
@@ -67,17 +72,19 @@ func partial_sums_of_gcd_sum_function(n) {
6772

6873
var s = n.isqrt
6974

70-
var A = sum(1..s, {|a|
71-
(a * moebius_partial_sum(floor(n/a))) + (moebius(a) * faulhaber(floor(n/a), 1))
75+
var A = sum(1..s, {|k|
76+
var t = floor(n/k)
77+
(k * moebius_partial_sum(t)) + (moebius_lookup[k] * t.faulhaber(1))
7278
})
7379

7480
var C = (moebius_partial_sum(s) * faulhaber(s, 1))
7581

7682
return (A - C)
7783
}
7884

79-
var A = sum(1..s, {|a|
80-
(a * euler_phi_partial_sum(floor(n/a))) + (euler_phi(a) * faulhaber(floor(n/a), 1))
85+
var A = sum(1..s, {|k|
86+
var t = floor(n/k)
87+
(k * euler_phi_partial_sum(t)) + (euler_phi_lookup[k] * t.faulhaber(1))
8188
})
8289

8390
var C = (euler_phi_partial_sum(s) * faulhaber(s, 1))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/usr/bin/ruby
2+
3+
# Daniel "Trizen" Șuteu
4+
# Date: 04 February 2019
5+
# https://github.com/trizen
6+
7+
# A sublinear algorithm for computing the partial sums of the gcd-sum function, using Dirichlet's hyperbola method.
8+
9+
# The partial sums of the gcd-sum function is defined as:
10+
#
11+
# a(n) = Sum_{k=1..n} Sum_{d|k} d*phi(k/d)
12+
#
13+
# where phi(k) is the Euler totient function.
14+
15+
# Also equivalent with:
16+
# a(n) = Sum_{j=1..n} Sum_{i=1..j} gcd(i, j)
17+
18+
# Based on the formula:
19+
# a(n) = (1/2)*Sum_{k=1..n} phi(k) * floor(n/k) * floor(1+n/k)
20+
21+
# Example:
22+
# a(10^1) = 122
23+
# a(10^2) = 18065
24+
# a(10^3) = 2475190
25+
# a(10^4) = 317257140
26+
# a(10^5) = 38717197452
27+
# a(10^6) = 4571629173912
28+
# a(10^7) = 527148712519016
29+
# a(10^8) = 59713873168012716
30+
# a(10^9) = 6671288261316915052
31+
32+
# OEIS sequences:
33+
# https://oeis.org/A272718 -- Partial sums of gcd-sum sequence A018804.
34+
# https://oeis.org/A018804 -- Pillai's arithmetical function: Sum_{k=1..n} gcd(k, n).
35+
36+
# See also:
37+
# https://en.wikipedia.org/wiki/Dirichlet_hyperbola_method
38+
# https://trizenx.blogspot.com/2018/11/partial-sums-of-arithmetical-functions.html
39+
40+
func partial_sums_of_gcd_sum_function(n) {
41+
var s = n.isqrt
42+
43+
var euler_sum_lookup = [0]
44+
45+
var lookup_size = (2 + 2*n.iroot(3)**2)
46+
var euler_phi_lookup = [0]
47+
48+
for k in (1 .. lookup_size) {
49+
euler_sum_lookup[k] = (euler_sum_lookup[k-1] + (euler_phi_lookup[k] = k.euler_phi))
50+
}
51+
52+
var seen = Hash()
53+
54+
func euler_phi_partial_sum(n) {
55+
56+
if (n <= lookup_size) {
57+
return euler_sum_lookup[n]
58+
}
59+
60+
if (seen.has(n)) {
61+
return seen{n}
62+
}
63+
64+
var s = n.isqrt
65+
var T = n.faulhaber(1)
66+
67+
var A = sum(2..s, {|k|
68+
__FUNC__(floor(n/k))
69+
})
70+
71+
var B = sum(1 .. floor(n/s)-1, {|k|
72+
(floor(n/k) - floor(n/(k+1))) * __FUNC__(k)
73+
})
74+
75+
seen{n} = (T - A - B)
76+
}
77+
78+
var A = sum(1..s, {|k|
79+
var t = floor(n/k)
80+
(k * euler_phi_partial_sum(t)) + (euler_phi_lookup[k] * t.faulhaber(1))
81+
})
82+
83+
var T = s.faulhaber(1)
84+
var C = euler_phi_partial_sum(s)
85+
86+
return (A - T*C)
87+
}
88+
89+
say 20.of { partial_sums_of_gcd_sum_function(_) }
90+
91+
__END__
92+
[0, 1, 4, 9, 17, 26, 41, 54, 74, 95, 122, 143, 183, 208, 247, 292, 340, 373, 436, 473]

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ A simple collection of Sidef scripts.
245245
* [Partial sums of dedekind psi function](./Math/partial_sums_of_dedekind_psi_function.sf)
246246
* [Partial sums of euler totient function](./Math/partial_sums_of_euler_totient_function.sf)
247247
* [Partial sums of gcd-sum function](./Math/partial_sums_of_gcd-sum_function.sf)
248+
* [Partial sums of gcd-sum function fast](./Math/partial_sums_of_gcd-sum_function_fast.sf)
248249
* [Partial sums of jordan totient function](./Math/partial_sums_of_jordan_totient_function.sf)
249250
* [Partial sums of prime bigomega function](./Math/partial_sums_of_prime_bigomega_function.sf)
250251
* [Partial sums of prime omega function](./Math/partial_sums_of_prime_omega_function.sf)

0 commit comments

Comments
 (0)