malachite_float/float/constants/prime_constant.rs
1// Copyright © 2026 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use crate::Float;
10use core::cmp::Ordering;
11use malachite_base::num::factorization::primes::prime_indicator_sequence_less_than_or_equal_to;
12use malachite_base::rounding_modes::RoundingMode::{self, *};
13
14impl Float {
15 /// Returns an approximation of the prime constant, with the given precision and rounded using
16 /// the given [`RoundingMode`]. An [`Ordering`] is also returned, indicating whether the rounded
17 /// value is less than or greater than the exact value of the constant. (Since the constant is
18 /// irrational, the rounded value is never equal to the exact value.)
19 ///
20 /// The prime constant is the real number whose $n$th bit is prime if and only if $n$ is prime.
21 /// That is,
22 /// $$
23 /// \rho = \sum_{p\ \text{prime}\}2^{-p}+\varepsilon.
24 /// $$
25 /// - If $m$ is not `Nearest`, then $|\varepsilon| < 2^{-p-1}$.
26 /// - If $m$ is `Nearest`, then $|\varepsilon| < 2^{-p-2}$.
27 ///
28 /// The constant is irrational. It is unknown whether it is transcendental; see
29 /// <https://mathoverflow.net/questions/114905>.
30 ///
31 /// The output has precision `prec`.
32 ///
33 /// # Worst-case complexity
34 /// $T(n) = O(n)$
35 ///
36 /// $M(n) = O(n)$
37 ///
38 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
39 ///
40 /// # Panics
41 /// Panics if `prec` is zero or if `rm` is `Exact`.
42 ///
43 /// # Examples
44 /// ```
45 /// use malachite_base::rounding_modes::RoundingMode::*;
46 /// use malachite_float::Float;
47 /// use std::cmp::Ordering::*;
48 ///
49 /// let (pc, o) = Float::prime_constant_prec_round(100, Floor);
50 /// assert_eq!(pc.to_string(), "0.41468250985111166024810962215420");
51 /// assert_eq!(o, Less);
52 ///
53 /// let (pc, o) = Float::prime_constant_prec_round(100, Ceiling);
54 /// assert_eq!(pc.to_string(), "0.41468250985111166024810962215460");
55 /// assert_eq!(o, Greater);
56 /// ```
57 #[inline]
58 pub fn prime_constant_prec_round(prec: u64, rm: RoundingMode) -> (Self, Ordering) {
59 // Strictly speaking, this call violates the preconditions for
60 // `non_dyadic_from_bits_prec_round`, because the iterator passed in is finite. But since we
61 // know exactly how many bits `non_dyadic_from_bits_prec_round` will read, we can get away
62 // with this.
63 Self::non_dyadic_from_bits_prec_round(
64 prime_indicator_sequence_less_than_or_equal_to(if rm == Nearest {
65 prec + 2
66 } else {
67 prec + 1
68 }),
69 prec,
70 rm,
71 )
72 }
73
74 /// Returns an approximation of the prime constant, with the given precision and rounded to the
75 /// nearest [`Float`] of that precision. An [`Ordering`] is also returned, indicating whether
76 /// the rounded value is less than or greater than the exact value of the constant. (Since the
77 /// constant is irrational, the rounded value is never equal to the exact value.)
78 ///
79 /// The prime constant is the real number whose $n$th bit is prime if and only if $n$ is prime.
80 /// That is,
81 /// $$
82 /// \rho = \sum_{p\ \text{prime}\}2^{-p}+\varepsilon.
83 /// $$
84 /// - $|\varepsilon| < 2^{-p-2}$.
85 ///
86 /// The constant is irrational. It is unknown whether it is transcendental; see
87 /// <https://mathoverflow.net/questions/114905>.
88 ///
89 /// The output has precision `prec`.
90 ///
91 /// # Worst-case complexity
92 /// $T(n) = O(n)$
93 ///
94 /// $M(n) = O(n)$
95 ///
96 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
97 ///
98 /// # Panics
99 /// Panics if `prec` is zero.
100 ///
101 /// # Examples
102 /// ```
103 /// use malachite_float::Float;
104 /// use std::cmp::Ordering::*;
105 ///
106 /// let (pc, o) = Float::prime_constant_prec(1);
107 /// assert_eq!(pc.to_string(), "0.50");
108 /// assert_eq!(o, Greater);
109 ///
110 /// let (pc, o) = Float::prime_constant_prec(10);
111 /// assert_eq!(pc.to_string(), "0.41455");
112 /// assert_eq!(o, Less);
113 ///
114 /// let (pc, o) = Float::prime_constant_prec(100);
115 /// assert_eq!(pc.to_string(), "0.41468250985111166024810962215420");
116 /// assert_eq!(o, Less);
117 /// ```
118 #[inline]
119 pub fn prime_constant_prec(prec: u64) -> (Self, Ordering) {
120 Self::prime_constant_prec_round(prec, Nearest)
121 }
122}