Skip to content

Instantly share code, notes, and snippets.

@righ1113
Last active July 5, 2023 19:57
Show Gist options
  • Save righ1113/89ebead2489038ee73207258df8cc76f to your computer and use it in GitHub Desktop.
Save righ1113/89ebead2489038ee73207258df8cc76f to your computer and use it in GitHub Desktop.
並列計算 & 多倍長 in Rust
// main.rsにコピペする
// Cargo.toml
// [dependencies]
// num-traits = "0.2.7"
// num-bigint = "0.2"
extern crate num_traits;
extern crate num_bigint;
use num_traits::{Zero, One};
use num_bigint::BigUint;
use std::thread;
fn v(x: u8) -> BigUint {
match x {
0 => Zero::zero(), //let f0: BigUint = Zero::zero(); f0,
_ => One ::one(), //let f1: BigUint = One ::one(); f1,
}
}
fn ack(m: u8, n: BigUint) -> BigUint {
if m == 0 {
n + v(1)
} else if n == v(0) {
ack(m - 1, v(1))
} else {
ack(m - 1, ack(m, n - v(1)))
}
}
fn main() {
// スレッド生成器の準備
let builder = thread::Builder::new();
// スタックサイズを指定してスレッドを生成する
let th = builder.stack_size(100000000);
// 実行 cargo run --release で10分くらい
let handle = th.spawn(|| {
println!("ack(4, 2じゃなくて1): {}", ack(4, v(1)));
}).unwrap();
let _ = handle.join();
}
// main.rsにコピペする
use std::thread;
fn fib(n: u32) -> u32 {
match n {
0 => 0,
1 => 1,
_ => fib(n - 2) + fib(n - 1),
}
}
fn main() {
let mut handles = vec![];
for x in 39..43 {
let handle = thread::spawn(move || {
println!("Result: {} {}", x, fib(x));
});
handles.push(handle);
}
for handle in handles {
handle.join().unwrap();
}
println!("hello from main thread!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment