Initial commit

This commit is contained in:
2024-03-11 12:43:52 +02:00
parent 0cc291f3d8
commit a57340adb7
55 changed files with 2961 additions and 1 deletions

7
Rust/Makefile Normal file
View File

@@ -0,0 +1,7 @@
compile:
cargo build --release --manifest-path code_cycle/Cargo.toml
cargo build --release --manifest-path code_unoptimised/Cargo.toml
clean:
cargo clean --manifest-path code_cycle/Cargo.toml
cargo clean --manifest-path code_unoptimised/Cargo.toml

1
Rust/code_cycle/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

7
Rust/code_cycle/Cargo.lock generated Normal file
View File

@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "code_cycle"
version = "0.1.0"

View File

@@ -0,0 +1,19 @@
[package]
name = "code_cycle"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[profile.dev.package."*"] # +
opt-level = "z" # Optimize library for size
[profile.release]
#opt-level = 'z' # Optimize for size
opt-level = 3 # Optimize for speed
lto = true # Enable link-time optimization
codegen-units = 1 # Reduce number of codegen units to increase optimizations
panic = 'abort' # Abort on panic
strip = true # Strip symbols from binary*
[dependencies]

View File

@@ -0,0 +1,18 @@
fn main() {
let total_attempts: i32 = 256*256*256;
let mut successful_attempts: i32 = 0;
for a in 0..256 {
for b in 0..256 {
for c in 0..256 {
if a + b + c > 300 {
successful_attempts += 1;
}
}
}
}
println!("Iterations: {}", total_attempts);
println!("Valid sums: {}", successful_attempts);
println!("Probability: {}", successful_attempts as f32 / total_attempts as f32);
}

1
Rust/code_unoptimised/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

75
Rust/code_unoptimised/Cargo.lock generated Normal file
View File

@@ -0,0 +1,75 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "code_unoptimised"
version = "0.1.0"
dependencies = [
"rand",
]
[[package]]
name = "getrandom"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "libc"
version = "0.2.149"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a08173bc88b7955d1b3145aa561539096c421ac8debde8cbc3612ec635fee29b"
[[package]]
name = "ppv-lite86"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
]
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"

View File

@@ -0,0 +1,20 @@
[package]
name = "code_unoptimised"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[profile.dev.package."*"] # +
opt-level = "z" # Optimize library for size
[profile.release]
#opt-level = 'z' # Optimize for size
opt-level = 3 # Optimize for speed
lto = true # Enable link-time optimization
codegen-units = 1 # Reduce number of codegen units to increase optimizations
panic = 'abort' # Abort on panic
strip = true # Strip symbols from binary*
[dependencies]
rand = "0.8.5"

View File

@@ -0,0 +1,22 @@
fn main() {
let total_attempts: i32 = 15_000_000;
let mut successful_attempts: i32 = 0;
for _ in 0..total_attempts {
let a = rand::random::<u8>() as u16;
let b = rand::random::<u8>() as u16;
let c = rand::random::<u8>() as u16;
if a + b + c > 300 {
successful_attempts += 1;
}
}
println!("Iterations: {}", total_attempts);
println!("Valid sums: {}", successful_attempts);
println!(
"Probability: {}",
successful_attempts as f32 / total_attempts as f32
);
}