Aufgaben2_choenes_tkruemmel

This commit is contained in:
Christoph Hönes 2016-11-06 23:48:18 +01:00
parent 05dc8c0d3c
commit 860acf0fdc
2 changed files with 50 additions and 2 deletions

View File

@ -2,7 +2,29 @@
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() {

View File

@ -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]);
}