1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright © 2026 Mikhail Hogrefe
//
// This file is part of Malachite.
//
// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
use crate::{Float, floor_and_ceiling};
use core::cmp::Ordering::{self, *};
use malachite_base::num::basic::integers::PrimitiveInt;
use malachite_base::rounding_modes::RoundingMode::{self, *};
use malachite_nz::platform::Limb;
impl Float {
/// Returns an approximation of Ramanujan's constant, $e^{\pi\sqrt{163}}$, with the given
/// precision and rounded using the given [`RoundingMode`]. An [`Ordering`] is also returned,
/// indicating whether the rounded value is less than or greater than the exact value of the
/// constant. (Since the constant is irrational, the rounded value is never equal to the exact
/// value.)
///
/// $$
/// x = e^{\pi\sqrt{163}}+\varepsilon.
/// $$
/// - If $m$ is not `Nearest`, then $|\varepsilon| < 2^{-p+58}$.
/// - If $m$ is `Nearest`, then $|\varepsilon| < 2^{-p+57}$.
///
/// The constant is irrational and transcendental. It is famously close to an integer: $e^{\pi
/// \sqrt{163}} \approx 262{,}537{,}412{,}640{,}768{,}744 - 7.5 \times 10^{-13}$.
///
/// The output has precision `prec`.
///
/// # Worst-case complexity
/// $T(n) = O(n^{3/2} \log n \log\log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
///
/// # Panics
/// Panics if `prec` is zero or if `rm` is `Exact`.
///
/// # Examples
/// ```
/// use malachite_base::rounding_modes::RoundingMode::*;
/// use malachite_float::Float;
/// use std::cmp::Ordering::*;
///
/// let (ramanujans_constant, o) = Float::ramanujans_constant_prec_round(100, Floor);
/// assert_eq!(
/// ramanujans_constant.to_string(),
/// "262537412640768743.99999999999909"
/// );
/// assert_eq!(o, Less);
///
/// let (ramanujans_constant, o) = Float::ramanujans_constant_prec_round(100, Ceiling);
/// assert_eq!(
/// ramanujans_constant.to_string(),
/// "262537412640768743.99999999999932"
/// );
/// assert_eq!(o, Greater);
/// ```
pub fn ramanujans_constant_prec_round(prec: u64, rm: RoundingMode) -> (Self, Ordering) {
let mut working_prec = prec + 10;
let mut increment = Limb::WIDTH;
loop {
let (pi_lo, pi_hi) = floor_and_ceiling(Self::pi_prec_round(working_prec, Floor));
let (sqrt_163_lo, sqrt_163_hi) = floor_and_ceiling(
const { Self::const_from_unsigned(163) }.sqrt_prec_round(working_prec, Floor),
);
// pi and sqrt(163) are positive, so pi * sqrt(163) is bracketed by the products of the
// corresponding bounds.
//
// exp is increasing, so exp(arg_lo) <= exp(pi * sqrt(163)) <= exp(arg_hi).
let (ramanujans_constant_lo, mut o_lo) = Self::from_float_prec_round(
pi_lo.mul_round(sqrt_163_lo, Floor).0.exp_round(Floor).0,
prec,
rm,
);
let (ramanujans_constant_hi, mut o_hi) = Self::from_float_prec_round(
pi_hi.mul_round(sqrt_163_hi, Ceiling).0.exp_round(Ceiling).0,
prec,
rm,
);
if o_lo == Equal {
o_lo = o_hi;
}
if o_hi == Equal {
o_hi = o_lo;
}
if o_lo == o_hi && ramanujans_constant_lo == ramanujans_constant_hi {
return (ramanujans_constant_lo, o_lo);
}
working_prec += increment;
increment = working_prec >> 1;
}
}
/// Returns an approximation of Ramanujan's constant, $e^{\pi\sqrt{163}}$, with the given
/// precision and rounded to the nearest [`Float`] of that precision. An [`Ordering`] is also
/// returned, indicating whether the rounded value is less than or greater than the exact value
/// of the constant. (Since the constant is irrational, the rounded value is never equal to the
/// exact value.)
///
/// $$
/// x = e^{\pi\sqrt{163}}+\varepsilon.
/// $$
/// - $|\varepsilon| < 2^{-p+57}$.
///
/// The constant is irrational and transcendental. It is famously close to an integer: $e^{\pi
/// \sqrt{163}} \approx 262{,}537{,}412{,}640{,}768{,}744 - 7.5 \times 10^{-13}$.
///
/// The output has precision `prec`.
///
/// # Worst-case complexity
/// $T(n) = O(n^{3/2} \log n \log\log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
///
/// # Panics
/// Panics if `prec` is zero.
///
/// # Examples
/// ```
/// use malachite_float::Float;
/// use std::cmp::Ordering::*;
///
/// let (ramanujans_constant, o) = Float::ramanujans_constant_prec(1);
/// assert_eq!(ramanujans_constant.to_string(), "2.9e17");
/// assert_eq!(o, Greater);
///
/// let (ramanujans_constant, o) = Float::ramanujans_constant_prec(10);
/// assert_eq!(ramanujans_constant.to_string(), "2.6262e17");
/// assert_eq!(o, Greater);
///
/// let (ramanujans_constant, o) = Float::ramanujans_constant_prec(97);
/// assert_eq!(
/// ramanujans_constant.to_string(),
/// "262537412640768744.0000000000000"
/// );
/// assert_eq!(o, Greater);
///
/// let (ramanujans_constant, o) = Float::ramanujans_constant_prec(100);
/// assert_eq!(
/// ramanujans_constant.to_string(),
/// "262537412640768743.99999999999932"
/// );
/// assert_eq!(o, Greater);
/// ```
#[inline]
pub fn ramanujans_constant_prec(prec: u64) -> (Self, Ordering) {
Self::ramanujans_constant_prec_round(prec, Nearest)
}
}