-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
45 lines (39 loc) · 1.17 KB
/
main.rs
File metadata and controls
45 lines (39 loc) · 1.17 KB
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
use std::time::Instant;
use fraction;
use num_bigint::BigUint;
use num_traits::Zero;
use num_traits::One;
type F = fraction::BigFraction;
fn sqrt_series(length: u64) -> F {
return F::from(BigUint::from(1u64)) +
(F::from(BigUint::from(1u64)) / sqrt2_series_helper(length))
}
fn sqrt2_series_helper(length: u64) -> F {
if length == 0 {
return F::from(BigUint::from(2u64))
} else {
return F::from(BigUint::from(2u64)) +
(F::from(BigUint::from(1u64)) / sqrt2_series_helper(length - 1))
}
}
fn digit_length(x: &BigUint) -> u64 {
x.to_string().chars().count() as u64
}
fn main() {
let start = Instant::now();
let mut num = 0;
for l in 0..1000 {
println!("{}", l);
let expansion = sqrt_series(l);
let numer_length = digit_length(expansion.numer().unwrap());
let denom_length = digit_length(expansion.denom().unwrap());
if numer_length > denom_length {
num += 1;
}
}
let elapsed = Instant::now() - start;
println!(
"Number of expansions with more digits in numerator than denominator = {} in 0..1000 (elapsed={elapsed:?})",
num
);
}