Skip to content

Commit 043f248

Browse files
committed
new file: Math/count_of_perfect_powers.pl
new file: Math/sum_of_perfect_powers.pl new file: Math/sum_of_prime_powers.pl
1 parent 6008395 commit 043f248

8 files changed

+213
-3
lines changed
File renamed without changes.

Math/count_of_perfect_powers.pl

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/perl
2+
3+
# Efficient formula for counting the numbers of perfect powers <= n.
4+
5+
# Formula:
6+
# a(n) = n - Sum_{1..floor(log_2(n))} mu(k) * (floor(n^(1/k)) - 1)
7+
# = 1 - Sum_{2..floor(log_2(n))} mu(k) * (floor(n^(1/k)) - 1)
8+
9+
# See also:
10+
# https://oeis.org/A069623
11+
12+
use 5.036;
13+
use ntheory qw(logint rootint moebius vecsum);
14+
15+
sub perfect_power_count ($n) {
16+
1 - vecsum(map { moebius($_) * (rootint($n, $_) - 1) } 2 .. logint($n, 2));
17+
}
18+
19+
foreach my $n (0 .. 15) {
20+
printf("a(10^%d) = %s\n", $n, perfect_power_count(10**$n));
21+
}
22+
23+
__END__
24+
a(10^0) = 1
25+
a(10^1) = 4
26+
a(10^2) = 13
27+
a(10^3) = 41
28+
a(10^4) = 125
29+
a(10^5) = 367
30+
a(10^6) = 1111
31+
a(10^7) = 3395
32+
a(10^8) = 10491
33+
a(10^9) = 32670
34+
a(10^10) = 102231
35+
a(10^11) = 320990
36+
a(10^12) = 1010196
37+
a(10^13) = 3184138
38+
a(10^14) = 10046921
39+
a(10^15) = 31723592
File renamed without changes.
File renamed without changes.

Math/partitions_count.pl

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use strict;
1717
use warnings;
1818

19+
no warnings 'recursion';
20+
1921
use Memoize qw(memoize);
2022
use Math::AnyNum qw(:overload floor ceil);
2123

Math/sum_of_perfect_powers.pl

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/perl
2+
3+
# Efficient formula for computing the sum of perfect powers <= n.
4+
5+
# Formula:
6+
# a(n) = faulhaber(n,1) - Sum_{1..floor(log_2(n))} mu(k) * (faulhaber(floor(n^(1/k)), k) - 1)
7+
# = 1 - Sum_{2..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+
use 5.036;
16+
use ntheory qw(moebius);
17+
use Math::AnyNum qw(faulhaber_sum sum ipow iroot ilog2);
18+
19+
sub perfect_power_sum ($n) {
20+
1 - sum(map { moebius($_) * (faulhaber_sum(iroot($n, $_), $_) - 1) } 2 .. ilog2($n));
21+
}
22+
23+
foreach my $n (0 .. 15) {
24+
printf("a(10^%d) = %s\n", $n, perfect_power_sum(ipow(10, $n)));
25+
}
26+
27+
__END__
28+
a(10^0) = 1
29+
a(10^1) = 22
30+
a(10^2) = 452
31+
a(10^3) = 13050
32+
a(10^4) = 410552
33+
a(10^5) = 11888199
34+
a(10^6) = 361590619
35+
a(10^7) = 11120063109
36+
a(10^8) = 345454923761
37+
a(10^9) = 10800726331772
38+
a(10^10) = 338846269199225
39+
a(10^11) = 10659098451968490
40+
a(10^12) = 335867724220740686
41+
a(10^13) = 10595345580446344714
42+
a(10^14) = 334502268562161605300
43+
a(10^15) = 10566065095217905939231

Math/sum_of_prime_powers.pl

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#!/usr/bin/perl
2+
3+
# Three sublinear algorithms for computing the sum of prime powers <= n,
4+
# based on the sublinear algorithm for computing the sum of primes <= n.
5+
6+
# See also:
7+
# https://oeis.org/A074793
8+
9+
use 5.036;
10+
use Math::GMPz;
11+
use ntheory qw(:all);
12+
use Math::Prime::Util::GMP qw(faulhaber_sum);
13+
14+
sub sum_of_primes ($n, $k = 1) { # Sum_{p prime <= n} p^k
15+
16+
return sum_primes($n) if ($k == 1); # optimization
17+
18+
$n > ~0 and return undef;
19+
$n <= 1 and return 0;
20+
21+
my $r = sqrtint($n);
22+
my @V = map { divint($n, $_) } 1 .. $r;
23+
push @V, CORE::reverse(1 .. $V[-1] - 1);
24+
25+
my $t = Math::GMPz::Rmpz_init_set_ui(0);
26+
my $u = Math::GMPz::Rmpz_init();
27+
28+
my %S;
29+
@S{@V} = map { Math::GMPz::Rmpz_init_set_str(faulhaber_sum($_, $k), 10) } @V;
30+
31+
foreach my $p (2 .. $r) {
32+
if ($S{$p} > $S{$p - 1}) {
33+
my $cp = $S{$p - 1};
34+
my $p2 = $p * $p;
35+
Math::GMPz::Rmpz_ui_pow_ui($t, $p, $k);
36+
foreach my $v (@V) {
37+
last if ($v < $p2);
38+
Math::GMPz::Rmpz_sub($u, $S{divint($v, $p)}, $cp);
39+
Math::GMPz::Rmpz_submul($S{$v}, $u, $t);
40+
}
41+
}
42+
}
43+
44+
$S{$n} - 1;
45+
}
46+
47+
sub sum_of_prime_powers ($n) {
48+
49+
# a(n) = Sum_{p prime <= n} p
50+
# b(n) = Sum_{p prime <= n^(1/2)} p^2
51+
# c(n) = Sum_{p prime <= n^(1/3)} f(p)
52+
53+
# sum_of_prime_powers(n) = a(n) + b(n) + c(n)
54+
55+
my $ps1 = sum_of_primes($n);
56+
my $ps2 = sum_of_primes(sqrtint($n), 2);
57+
58+
# f(p) = (Sum_{k=1..floor(log_p(n))} p^k) - p^2 - p
59+
# = (p^(1+floor(log_p(n))) - 1)/(p-1) - p^2 - p - 1
60+
61+
my $ps3 = 0;
62+
foreach my $p (@{primes(rootint($n, 3))}) {
63+
$ps3 += divint(powint($p, logint($n, $p) + 1) - 1, $p - 1) - $p * $p - $p - 1;
64+
}
65+
66+
return vecsum($ps1, $ps2, $ps3);
67+
}
68+
69+
sub sum_of_prime_powers_2 ($n) {
70+
71+
# a(n) = Sum_{p prime <= n} p
72+
# b(n) = Sum_{p prime <= n^(1/2)} f(p)
73+
74+
# sum_of_prime_powers(n) = a(n) + b(n)
75+
76+
my $ps1 = sum_of_primes($n);
77+
78+
# f(p) = (Sum_{k=1..floor(log_p(n))} p^k) - p
79+
# = (p^(1+floor(log_p(n))) - 1)/(p-1) - p - 1
80+
81+
my $ps2 = 0;
82+
forprimes {
83+
$ps2 += divint(powint($_, logint($n, $_) + 1) - 1, $_ - 1) - $_ - 1;
84+
} sqrtint($n);
85+
86+
return vecsum($ps1, $ps2);
87+
}
88+
89+
sub sum_of_prime_powers_3 ($n) {
90+
91+
# a(n) = Sum_{k=1..floor(log_2(n))} Sum_{p prime <= n^(1/k)} p^k.
92+
vecsum(map { sum_of_primes(rootint($n, $_), $_) } 1 .. logint($n, 2));
93+
}
94+
95+
foreach my $n (0 .. 10) {
96+
say "a(10^$n) = ", sum_of_prime_powers(powint(10, $n));
97+
}
98+
99+
foreach my $k (1 .. 100) {
100+
my $n = int(rand(1e3)) + 1;
101+
102+
my $x = sum_of_prime_powers($n);
103+
my $y = sum_of_prime_powers_2($n);
104+
my $z = sum_of_prime_powers_3($n);
105+
106+
$x == $y or die "error";
107+
$x == $z or die "error";
108+
}
109+
110+
__END__
111+
a(10^0) = 0
112+
a(10^1) = 38
113+
a(10^2) = 1375
114+
a(10^3) = 82674
115+
a(10^4) = 5850315
116+
a(10^5) = 457028152
117+
a(10^6) = 37610438089
118+
a(10^7) = 3204814813355
119+
a(10^8) = 279250347324393
120+
a(10^9) = 24740607755154524
121+
a(10^10) = 2220853189506845580
122+
a(10^11) = 201467948093608962539
123+
a(10^12) = 18435613572072500152927

README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,6 @@ A nice collection of day-to-day Perl scripts.
502502
* [BPSW primality test](./Math/BPSW_primality_test.pl)
503503
* [BPSW primality test mpz](./Math/BPSW_primality_test_mpz.pl)
504504
* [Brazilian primes constant](./Math/brazilian_primes_constant.pl)
505-
* [Brilliant numbers count](./Math/brilliant_numbers_count.pl)
506505
* [Brown numbers](./Math/brown_numbers.pl)
507506
* [Carmichael factorization method](./Math/carmichael_factorization_method.pl)
508507
* [Carmichael factorization method generalized](./Math/carmichael_factorization_method_generalized.pl)
@@ -544,19 +543,23 @@ A nice collection of day-to-day Perl scripts.
544543
* [Continued fractions prime constant](./Math/continued_fractions_prime_constant.pl)
545544
* [Convergent series](./Math/convergent_series.pl)
546545
* [Cosmic calendar](./Math/cosmic_calendar.pl)
546+
* [Count of brilliant numbers](./Math/count_of_brilliant_numbers.pl)
547547
* [Count of cube-full numbers](./Math/count_of_cube-full_numbers.pl)
548548
* [Count of integers with gpf of n equals p](./Math/count_of_integers_with_gpf_of_n_equals_p.pl)
549549
* [Count of integers with lpf of n equals p](./Math/count_of_integers_with_lpf_of_n_equals_p.pl)
550550
* [Count of k-almost primes](./Math/count_of_k-almost_primes.pl)
551551
* [Count of k-omega primes](./Math/count_of_k-omega_primes.pl)
552552
* [Count of k-powerfree numbers](./Math/count_of_k-powerfree_numbers.pl)
553553
* [Count of k-powerful numbers](./Math/count_of_k-powerful_numbers.pl)
554+
* [Count of perfect powers](./Math/count_of_perfect_powers.pl)
555+
* [Count of prime power](./Math/count_of_prime_power.pl)
554556
* [Count of rough numbers](./Math/count_of_rough_numbers.pl)
555557
* [Count of smooth numbers](./Math/count_of_smooth_numbers.pl)
556558
* [Count of smooth numbers mpz](./Math/count_of_smooth_numbers_mpz.pl)
557559
* [Count of smooth numbers mpz 2](./Math/count_of_smooth_numbers_mpz_2.pl)
558560
* [Count of smooth numbers with k factors](./Math/count_of_smooth_numbers_with_k_factors.pl)
559561
* [Count of squarefree k-almost primes](./Math/count_of_squarefree_k-almost_primes.pl)
562+
* [Count of squarefree numbers](./Math/count_of_squarefree_numbers.pl)
560563
* [Count subtriangles](./Math/count_subtriangles.pl)
561564
* [Cube-full numbers](./Math/cube-full_numbers.pl)
562565
* [Cuboid](./Math/cuboid.pl)
@@ -868,7 +871,6 @@ A nice collection of day-to-day Perl scripts.
868871
* [Prime functions in terms of zeros of zeta](./Math/prime_functions_in_terms_of_zeros_of_zeta.pl)
869872
* [Prime numbers generator](./Math/prime_numbers_generator.pl)
870873
* [Prime omega function generalized](./Math/prime_omega_function_generalized.pl)
871-
* [Prime power count](./Math/prime_power_count.pl)
872874
* [Prime quadratic polynomial analyzer](./Math/prime_quadratic_polynomial_analyzer.pl)
873875
* [Prime quadratic polynomials](./Math/prime_quadratic_polynomials.pl)
874876
* [Prime summation](./Math/prime_summation.pl)
@@ -956,7 +958,6 @@ A nice collection of day-to-day Perl scripts.
956958
* [Squarefree almost primes from factor list](./Math/squarefree_almost_primes_from_factor_list.pl)
957959
* [Squarefree almost primes in range](./Math/squarefree_almost_primes_in_range.pl)
958960
* [Squarefree almost primes in range from factor list](./Math/squarefree_almost_primes_in_range_from_factor_list.pl)
959-
* [Squarefree count](./Math/squarefree_count.pl)
960961
* [Squarefree divisors](./Math/squarefree_divisors.pl)
961962
* [Squarefree fermat overpseudoprimes in range](./Math/squarefree_fermat_overpseudoprimes_in_range.pl)
962963
* [Squarefree fermat pseudoprimes in range](./Math/squarefree_fermat_pseudoprimes_in_range.pl)
@@ -975,8 +976,10 @@ A nice collection of day-to-day Perl scripts.
975976
* [Sum of digits subquadratic algorithm](./Math/sum_of_digits_subquadratic_algorithm.pl)
976977
* [Sum of digits subquadratic algorithm mpz](./Math/sum_of_digits_subquadratic_algorithm_mpz.pl)
977978
* [Sum of natural powers in constant base](./Math/sum_of_natural_powers_in_constant_base.pl)
979+
* [Sum of perfect powers](./Math/sum_of_perfect_powers.pl)
978980
* [Sum of prime-power exponents of factorial](./Math/sum_of_prime-power_exponents_of_factorial.pl)
979981
* [Sum of prime-power exponents of product of binomials](./Math/sum_of_prime-power_exponents_of_product_of_binomials.pl)
982+
* [Sum of prime powers](./Math/sum_of_prime_powers.pl)
980983
* [Sum of primes generalized](./Math/sum_of_primes_generalized.pl)
981984
* [Sum of sigma](./Math/sum_of_sigma.pl)
982985
* [Sum of sigma 2](./Math/sum_of_sigma_2.pl)

0 commit comments

Comments
 (0)