reto04
This commit is contained in:
8
ejercicios/reto04/Cargo.toml
Normal file
8
ejercicios/reto04/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "reto04"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
65
ejercicios/reto04/src/main.rs
Normal file
65
ejercicios/reto04/src/main.rs
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Escribe un programa que, dado un número, compruebe y muestre si es primo, fibonacci y par.
|
||||
* Ejemplos:
|
||||
* - Con el número 2, nos dirá: "2 es primo, fibonacci y es par"
|
||||
* - Con el número 7, nos dirá: "7 es primo, no es fibonacci y es impar"
|
||||
*/
|
||||
|
||||
fn esprimo(numero: i32) -> bool {
|
||||
if numero == 0 || numero == 1 {
|
||||
return false;
|
||||
} else {
|
||||
for i in 2..numero {
|
||||
if numero % i == 0 {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
fn esfibonacci(numero: i32) -> bool {
|
||||
let mut vector_numeros = vec![0, 1];
|
||||
let mut suma = vector_numeros[0] + vector_numeros[1];
|
||||
while suma < numero {
|
||||
vector_numeros.push(suma);
|
||||
vector_numeros.drain(0..1);
|
||||
suma = vector_numeros[0] + vector_numeros[1];
|
||||
if suma > numero {
|
||||
return false;
|
||||
}
|
||||
if suma == numero {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
fn espar(numero: i32) -> bool {
|
||||
if numero % 2 == 0 {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let num = 200;
|
||||
let mut texto = String::new();
|
||||
texto += &num.to_string();
|
||||
if esprimo(num) {
|
||||
texto += " es primo";
|
||||
} else {
|
||||
texto += " no es primo";
|
||||
}
|
||||
if esfibonacci(num) {
|
||||
texto += ", fibonacci";
|
||||
} else {
|
||||
texto += ", no es fibonacci";
|
||||
}
|
||||
if espar(num) {
|
||||
texto += " y es par";
|
||||
} else {
|
||||
texto += " y es impar";
|
||||
}
|
||||
println!("{}", texto);
|
||||
}
|
||||
Reference in New Issue
Block a user