mirror of
https://github.com/LukasKalbertodt/programmieren-in-rust.git
synced 2024-11-18 02:48:58 +01:00
Aufgaben2_choenes_tkruemmel
This commit is contained in:
parent
05dc8c0d3c
commit
860acf0fdc
@ -2,8 +2,30 @@
|
||||
|
||||
fn main() {
|
||||
|
||||
println!("1");
|
||||
for prime in 2..21 {
|
||||
if is_prime(prime){
|
||||
println!("{}*", prime);
|
||||
} else {
|
||||
println!("{}", prime);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn is_prime(prime: i32) -> bool {
|
||||
let mut iterator = 2;
|
||||
// testet ob ohne Rest teilbar, wenn nicht = Primzahl
|
||||
while iterator <= prime / 2 {
|
||||
if prime % iterator == 0 {
|
||||
return false;
|
||||
}
|
||||
iterator += 1;
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn small_primes() {
|
||||
assert!(is_prime(2));
|
||||
|
@ -2,11 +2,36 @@ fn main() {
|
||||
let mut arr = [61, 21, 27, 79, 57, 60, 46, 42, 27, 92, 66, 26];
|
||||
|
||||
sort(&mut arr);
|
||||
// TODO: print `arr`
|
||||
println!("{:?}", arr);
|
||||
}
|
||||
|
||||
// TODO: write `sort()` function
|
||||
|
||||
fn sort(arr: &mut [i32]) {
|
||||
let mut min;
|
||||
let mut temp = 0;
|
||||
let mut pointer = 0;
|
||||
let length = arr.len();
|
||||
|
||||
for counter in 0..length {
|
||||
min = arr[counter];
|
||||
// läuft unsortierten teil des arrays ab
|
||||
for counter2 in 0..length {
|
||||
let iter = counter2 + counter;
|
||||
if iter > length - 1{
|
||||
break;
|
||||
}
|
||||
|
||||
if arr[iter] <= min {
|
||||
min = arr[iter];
|
||||
temp = arr[counter];
|
||||
pointer = iter;
|
||||
}
|
||||
// tausche min an die richtige Stelle
|
||||
arr[counter] = min;
|
||||
arr[pointer] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sort_array() {
|
||||
@ -24,3 +49,4 @@ fn sort_array() {
|
||||
90, 91, 92, 94, 95, 96, 98,
|
||||
] as &[u64]);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user