parse big integer

This commit is contained in:
Sam Hadow 2025-05-18 19:24:32 +02:00
parent d858373b51
commit 16520d06f5
2 changed files with 16 additions and 9 deletions

View File

@ -2,6 +2,7 @@ use rug::Integer;
use std::fs;
use std::io;
use std::path::Path;
use std::str::FromStr;
pub struct AgcdInput {
pub noise_bits: usize,
@ -39,10 +40,20 @@ pub fn parse_file(path: &Path) -> io::Result<AgcdInput> {
// Parse numbers from remaining lines
let numbers: Vec<Integer> = lines
.filter(|line| !line.trim().starts_with("//") && !line.trim().is_empty()) // ignore comment and empty lines
.filter_map(|line| line.trim().parse::<u64>().ok())
.map(Integer::from)
.collect();
.filter(|line| {
let t = line.trim();
!t.starts_with("//") && !t.is_empty()
})
.map(|line| {
let s = line.trim();
Integer::from_str(s).map_err(|e| {
io::Error::new(
io::ErrorKind::InvalidData,
format!("Failed to parse integer '{}': {}", s, e),
)
})
})
.collect::<Result<_, io::Error>>()?;
if numbers.len() < 2 {
return Err(io::Error::new(

View File

@ -34,11 +34,7 @@ fn main() -> std::io::Result<()> {
let path = path.as_deref().unwrap_or(Path::new("./input.txt"));
let input = parse_file(path)?;
let result = agcd(input.numbers, input.noise_bits);
println!(
"Approximate GCD with noise_bits={}: {}",
input.noise_bits, result
);
agcd(input.numbers, input.noise_bits);
}
}
Ok(())