Skip to content

Commit 6ab365e

Browse files
committed
new file: Math/partial_sums_of_gcd-sum_function.sf
1 parent 6228f86 commit 6ab365e

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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 mertens_lookup = [0,1]
44+
var euler_sum_lookup = [0,1]
45+
46+
var lookup_size = n.iroot(3)**2 # O(n^(2/3))
47+
48+
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)
51+
}
52+
53+
func moebius_partial_sum(n) {
54+
55+
if (n <= lookup_size) {
56+
return mertens_lookup[n]
57+
}
58+
59+
n.mertens
60+
}
61+
62+
func euler_phi_partial_sum(n) {
63+
64+
if (n <= lookup_size) {
65+
return euler_sum_lookup[n]
66+
}
67+
68+
var s = n.isqrt
69+
70+
var A = sum(1..s, {|a|
71+
(a * moebius_partial_sum(floor(n/a))) + (moebius(a) * faulhaber(floor(n/a), 1))
72+
})
73+
74+
var C = (moebius_partial_sum(s) * faulhaber(s, 1))
75+
76+
return (A - C)
77+
}
78+
79+
var A = sum(1..s, {|a|
80+
(a * euler_phi_partial_sum(floor(n/a))) + (euler_phi(a) * faulhaber(floor(n/a), 1))
81+
})
82+
83+
var C = (euler_phi_partial_sum(s) * faulhaber(s, 1))
84+
85+
return (A - C)
86+
}
87+
88+
func g(n) { n }
89+
func h(n) { euler_phi(n) }
90+
91+
func test_sum(n, g, h) {
92+
sum(1..n, {|k|
93+
k.divisors.sum {|d|
94+
g(d) * h(k/d)
95+
}
96+
})
97+
}
98+
99+
say 20.of { test_sum(_, g, h) }
100+
say 20.of { partial_sums_of_gcd_sum_function(_) }
101+
102+
__END__
103+
[0, 1, 4, 9, 17, 26, 41, 54, 74, 95, 122, 143, 183, 208, 247, 292, 340, 373, 436, 473]
104+
[0, 1, 4, 9, 17, 26, 41, 54, 74, 95, 122, 143, 183, 208, 247, 292, 340, 373, 436, 473]

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ A simple collection of Sidef scripts.
244244
* [Ore's harmonic numbers](./Math/ore_s_harmonic_numbers.sf)
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)
247+
* [Partial sums of gcd-sum function](./Math/partial_sums_of_gcd-sum_function.sf)
247248
* [Partial sums of jordan totient function](./Math/partial_sums_of_jordan_totient_function.sf)
248249
* [Partial sums of prime bigomega function](./Math/partial_sums_of_prime_bigomega_function.sf)
249250
* [Partial sums of prime omega function](./Math/partial_sums_of_prime_omega_function.sf)

0 commit comments

Comments
 (0)