Skip to main content

malachite_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.4146825098511116602481096221542");
51    /// assert_eq!(o, Less);
52    ///
53    /// let (pc, o) = Float::prime_constant_prec_round(100, Ceiling);
54    /// assert_eq!(pc.to_string(), "0.4146825098511116602481096221546");
55    /// assert_eq!(o, Greater);
56    /// ```
57    pub fn prime_constant_prec_round(prec: u64, rm: RoundingMode) -> (Self, Ordering) {
58        // Strictly speaking, this call violates the preconditions for
59        // `non_dyadic_from_bits_prec_round`, because the iterator passed in is finite. But since we
60        // know exactly how many bits `non_dyadic_from_bits_prec_round` will read, we can get away
61        // with this.
62        Self::non_dyadic_from_bits_prec_round(
63            prime_indicator_sequence_less_than_or_equal_to(if rm == Nearest {
64                prec + 2
65            } else {
66                prec + 1
67            }),
68            prec,
69            rm,
70        )
71    }
72
73    /// Returns an approximation of the prime constant, with the given precision and rounded to the
74    /// nearest [`Float`] of that precision. An [`Ordering`] is also returned, indicating whether
75    /// the rounded value is less than or greater than the exact value of the constant. (Since the
76    /// constant is irrational, the rounded value is never equal to the exact value.)
77    ///
78    /// The prime constant is the real number whose $n$th bit is prime if and only if $n$ is prime.
79    /// That is,
80    /// $$
81    /// \rho = \sum_{p\ \text{prime}\}2^{-p}+\varepsilon.
82    /// $$
83    /// - $|\varepsilon| < 2^{-p-2}$.
84    ///
85    /// The constant is irrational. It is unknown whether it is transcendental; see
86    /// <https://mathoverflow.net/questions/114905>.
87    ///
88    /// The output has precision `prec`.
89    ///
90    /// # Worst-case complexity
91    /// $T(n) = O(n)$
92    ///
93    /// $M(n) = O(n)$
94    ///
95    /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
96    ///
97    /// # Panics
98    /// Panics if `prec` is zero.
99    ///
100    /// # Examples
101    /// ```
102    /// use malachite_float::Float;
103    /// use std::cmp::Ordering::*;
104    ///
105    /// let (pc, o) = Float::prime_constant_prec(1);
106    /// assert_eq!(pc.to_string(), "0.5");
107    /// assert_eq!(o, Greater);
108    ///
109    /// let (pc, o) = Float::prime_constant_prec(10);
110    /// assert_eq!(pc.to_string(), "0.4146");
111    /// assert_eq!(o, Less);
112    ///
113    /// let (pc, o) = Float::prime_constant_prec(100);
114    /// assert_eq!(pc.to_string(), "0.4146825098511116602481096221542");
115    /// assert_eq!(o, Less);
116    /// ```
117    #[inline]
118    pub fn prime_constant_prec(prec: u64) -> (Self, Ordering) {
119        Self::prime_constant_prec_round(prec, Nearest)
120    }
121}