Skip to content

Commit

Permalink
hard 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Risuntsy committed Feb 25, 2025
1 parent 368a50b commit 258d544
Show file tree
Hide file tree
Showing 933 changed files with 88 additions and 428 deletions.
Binary file removed exercises/easy/algorithm1.pdb
Binary file not shown.
Binary file removed exercises/easy/algorithm10.pdb
Binary file not shown.
Binary file removed exercises/easy/algorithm11.pdb
Binary file not shown.
Binary file removed exercises/easy/algorithm12.pdb
Binary file not shown.
Binary file removed exercises/easy/algorithm13.pdb
Binary file not shown.
Binary file removed exercises/easy/algorithm14.pdb
Binary file not shown.
Binary file removed exercises/easy/algorithm15.pdb
Binary file not shown.
Binary file removed exercises/easy/algorithm16.pdb
Binary file not shown.
Binary file removed exercises/easy/algorithm17.pdb
Binary file not shown.
Binary file removed exercises/easy/algorithm18.pdb
Binary file not shown.
Binary file removed exercises/easy/algorithm19.pdb
Binary file not shown.
Binary file removed exercises/easy/algorithm2.pdb
Binary file not shown.
Binary file removed exercises/easy/algorithm20.pdb
Binary file not shown.
Binary file removed exercises/easy/algorithm3.pdb
Binary file not shown.
Binary file removed exercises/easy/algorithm4.pdb
Binary file not shown.
Binary file removed exercises/easy/algorithm5.pdb
Binary file not shown.
Binary file removed exercises/easy/algorithm6.pdb
Binary file not shown.
Binary file removed exercises/easy/algorithm7.pdb
Binary file not shown.
Binary file removed exercises/easy/algorithm8.pdb
Binary file not shown.
Binary file removed exercises/easy/algorithm9.pdb
Binary file not shown.
1 change: 0 additions & 1 deletion exercises/hard/solutiont1/target/.rustc_info.json

This file was deleted.

3 changes: 0 additions & 3 deletions exercises/hard/solutiont1/target/CACHEDIR.TAG

This file was deleted.

Empty file.
Binary file not shown.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Binary file not shown.

This file was deleted.

Binary file not shown.

This file was deleted.

This file was deleted.

This file was deleted.

Binary file not shown.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Binary file not shown.

This file was deleted.

Empty file.
Empty file.
Empty file.

This file was deleted.

This file was deleted.

6 changes: 0 additions & 6 deletions exercises/hard/solutiont1/target/debug/deps/solutiont1.d

This file was deleted.

Binary file not shown.
Binary file not shown.

This file was deleted.

Binary file not shown.
Binary file not shown.

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Empty file.
1 change: 0 additions & 1 deletion exercises/hard/solutiont1/target/debug/solutiont1.d

This file was deleted.

Binary file not shown.
Binary file not shown.
66 changes: 59 additions & 7 deletions exercises/hard/solutiont2/src/prime_factor.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use std::time::Instant;

pub fn find_max_prime_factor(number: u128) -> u128 {
let mut n = number;

let mut max_factor = 0;

// Remove all factors of 2
while n % 2 == 0 {
max_factor = 2;
n /= 2;
}

// Check for odd factors from 3 to sqrt(n)
let mut factor = 3;
while factor * factor <= n {
while n % factor == 0 {
Expand Down Expand Up @@ -39,12 +39,64 @@ fn is_prime(n: u128) -> bool {
if n % 2 == 0 || n % 3 == 0 {
return false;
}
let mut i = 5;
while i * i <= n {
if n % i == 0 || n % (i + 2) == 0 {
return false;

let bases = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37];
let (s, d) = {
let mut d = n - 1;
let mut s = 0;
while d % 2 == 0 {
d /= 2;
s += 1;
}
(s, d)
};

'outer: for &a in bases.iter() {
if a >= n {
continue;
}
let mut x = mod_pow(a, d, n);
if x == 1 || x == n - 1 {
continue;
}
i += 6;
for _ in 0..s - 1 {
x = mod_pow(x, 2, n);
if x == n - 1 {
continue 'outer;
}
}
return false;
}
true
}

// 分步模乘法防溢出
fn mod_pow(base: u128, exp: u128, modulus: u128) -> u128 {
let mut result = 1;
let mut base = base % modulus;
let mut exp = exp;

while exp > 0 {
if exp % 2 == 1 {
result = mod_mul(result, base, modulus);
}
exp >>= 1;
base = mod_mul(base, base, modulus);
}
result
}

// 核心:将乘法分解为加法避免溢出 <button class="citation-flag" data-index="7">
fn mod_mul(mut a: u128, mut b: u128, modulus: u128) -> u128 {
let mut result = 0;
a %= modulus;
b %= modulus;
while b > 0 {
if b % 2 == 1 {
result = (result + a) % modulus;
}
a = (a << 1) % modulus;
b >>= 1;
}
result
}
2 changes: 1 addition & 1 deletion exercises/hard/solutiont2/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ mod tests {
if duration <= Duration::new(3, 0) && result == *expected {
total_score += 10.0;
} else {
println!("Test failed for input: {}, expected: {}, got: {}, use time: {}", input, expected, result, duration.as_millis());
println!("Test failed for input: {}, expected: {}, got: {}, use time: {} ms", input, expected, result, duration.as_millis());
}
}
println!("Total score: {:.2}", total_score);
Expand Down
1 change: 0 additions & 1 deletion exercises/hard/solutiont2/target/.rustc_info.json

This file was deleted.

3 changes: 0 additions & 3 deletions exercises/hard/solutiont2/target/CACHEDIR.TAG

This file was deleted.

Empty file.
Binary file not shown.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Binary file not shown.

This file was deleted.

This file was deleted.

Binary file not shown.

This file was deleted.

This file was deleted.

This file was deleted.

Binary file not shown.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Binary file not shown.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Binary file not shown.

This file was deleted.

Empty file.
Empty file.
Empty file.

This file was deleted.

Binary file not shown.
Binary file not shown.

This file was deleted.

This file was deleted.

6 changes: 0 additions & 6 deletions exercises/hard/solutiont2/target/debug/deps/solutiont2.d

This file was deleted.

Binary file not shown.
Binary file not shown.

This file was deleted.

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Empty file.
1 change: 0 additions & 1 deletion exercises/hard/solutiont2/target/debug/solutiont2.d

This file was deleted.

Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 258d544

Please sign in to comment.