Skip to content

Commit 7c4278c

Browse files
committed
new file: Math/sum_of_perfect_powers.sf
1 parent aefeb73 commit 7c4278c

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

Math/sum_of_perfect_powers.sf

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/ruby
2+
3+
# Efficient formula for computing the sum of perfect powers <= n.
4+
5+
# Formula:
6+
#
7+
# a(n) = faulhaber(n,1) - Sum_{1..floor(log_2(n))} mu(k) * (faulhaber(floor(n^(1/k)), k) - 1)
8+
#
9+
# where:
10+
# faulhaber(n,k) = Sum_{j=1..n} j^k.
11+
12+
# See also:
13+
# https://oeis.org/A069623
14+
15+
func perfect_power_sum(n) {
16+
n.faulhaber(1) - sum(1..n.ilog(2), {|k|
17+
mu(k) * (n.iroot(k).faulhaber(k) - 1)
18+
})
19+
}
20+
21+
for n in (0..15) {
22+
printf("a(10^%d) = %s\n", n, perfect_power_sum(10**n))
23+
}
24+
25+
__END__
26+
a(10^0) = 1
27+
a(10^1) = 22
28+
a(10^2) = 452
29+
a(10^3) = 13050
30+
a(10^4) = 410552
31+
a(10^5) = 11888199
32+
a(10^6) = 361590619
33+
a(10^7) = 11120063109
34+
a(10^8) = 345454923761
35+
a(10^9) = 10800726331772
36+
a(10^10) = 338846269199225
37+
a(10^11) = 10659098451968490
38+
a(10^12) = 335867724220740686
39+
a(10^13) = 10595345580446344714
40+
a(10^14) = 334502268562161605300
41+
a(10^15) = 10566065095217905939231

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,7 @@ A nice collection of day-to-day Sidef scripts.
667667
* [Sum of nth power digits](./Math/sum_of_nth_power_digits.sf)
668668
* [Sum of number of divisors of gcd x y](./Math/sum_of_number_of_divisors_of_gcd_x_y.sf)
669669
* [Sum of number of unitary divisors](./Math/sum_of_number_of_unitary_divisors.sf)
670+
* [Sum of perfect powers](./Math/sum_of_perfect_powers.sf)
670671
* [Sum of polygonal numbers function recursive](./Math/sum_of_polygonal_numbers_function_recursive.sf)
671672
* [Sum of prime-power exponents of factorial](./Math/sum_of_prime-power_exponents_of_factorial.sf)
672673
* [Sum of prime-power exponents of product of binomials](./Math/sum_of_prime-power_exponents_of_product_of_binomials.sf)

0 commit comments

Comments
 (0)