Integer roots: Difference between revisions
Added Rust solution
m (→{{header|Phix}}: moe comment) |
(Added Rust solution) |
||
Line 929:
{{out}}<pre>First 2,001 digits of the square root of two:
14142135623730950488016887242096(...)46758516447107578486024636008</pre>
=={{header|Rust}}==
The rug crate provides the functionality required for this task.
<lang rust>// [dependencies]
// rug = "1.9"
fn shorten(s: &str, digits: usize) -> String {
if s.len() <= digits + 3 {
return String::from(s);
}
format!("{}...{}", &s[0..digits/2], &s[s.len()-digits/2..])
}
fn main() {
use rug::{ops::Pow, Integer};
let x = Integer::from(8);
let r = Integer::from(x.root_ref(3));
println!("Integer cube root of {}: {}", x, r);
let x = Integer::from(9);
let r = Integer::from(x.root_ref(3));
println!("Integer cube root of {}: {}", x, r);
let mut x = Integer::from(100).pow(2000);
x *= 2;
let s = Integer::from(x.root(2)).to_string();
println!("First {} digits of the square root of 2:\n{}", s.len(), shorten(&s, 70));
let mut x = Integer::from(100).pow(3000);
x *= 2;
let s = Integer::from(x.root(3)).to_string();
println!("First {} digits of the cube root of 2:\n{}", s.len(), shorten(&s, 70));
}</lang>
{{out}}
<pre>
Integer cube root of 8: 2
Integer cube root of 9: 2
First 2001 digits of the square root of 2:
14142135623730950488016887242096980...32952546758516447107578486024636008
First 2001 digits of the cube root of 2:
12599210498948731647672106072782283...28546452083111122546828353183047061
</pre>
=={{header|Scala}}==
|