parent
07cd40b7f0
commit
07d56e0ccb
@ -0,0 +1,7 @@ |
|||||||
|
fn main() { |
||||||
|
println!("- Hola, mundo!"); |
||||||
|
print!("- My name is {}, James {}.\n- Hello, {}{}{}!", "Bond", "Bond", -2+2, 0, 3+2*2); |
||||||
|
println!(); |
||||||
|
println!("- Hasta la vista, Baby!\t\tI'll be back..."); |
||||||
|
println!("{}", if true {"- Lo dudo!\t\t\tBye!"} else {"- Obviamente!"}) |
||||||
|
} |
||||||
@ -0,0 +1,66 @@ |
|||||||
|
use std::io; |
||||||
|
use std::io::Write; |
||||||
|
|
||||||
|
fn main() { |
||||||
|
println!("************************************************************"); |
||||||
|
println!("Se ingresan dos valores enteros, se muestra su producto."); |
||||||
|
println!("Se utiliza el algoritmo de 'multiplicacion por duplicacion'."); |
||||||
|
println!("(Metodo campesino ruso de multiplicacion)"); |
||||||
|
println!("************************************************************"); |
||||||
|
|
||||||
|
print!("x: "); |
||||||
|
io::stdout().flush().expect("Error de escritura!"); |
||||||
|
|
||||||
|
let mut renglon: String = String::new(); |
||||||
|
io::stdin() |
||||||
|
.read_line(&mut renglon) |
||||||
|
.expect("Error de lectura!"); |
||||||
|
let mut x: i64 = renglon |
||||||
|
.trim() |
||||||
|
.parse::<i64>() |
||||||
|
.expect("Se esperaba un numero entero!"); |
||||||
|
|
||||||
|
let mut x_cambio: bool = false; |
||||||
|
if x < 0 { |
||||||
|
x = -x; |
||||||
|
x_cambio = true; |
||||||
|
} |
||||||
|
|
||||||
|
print!("y: "); |
||||||
|
io::stdout().flush().expect("Error de escritura!"); |
||||||
|
|
||||||
|
renglon = String::new(); |
||||||
|
io::stdin() |
||||||
|
.read_line(&mut renglon) |
||||||
|
.expect("Error de lectura!"); |
||||||
|
let mut y: i64 = renglon |
||||||
|
.trim() |
||||||
|
.parse::<i64>() |
||||||
|
.expect("Se esperaba un numero entero!"); |
||||||
|
|
||||||
|
let mut y_cambio: bool = false; |
||||||
|
if y < 0 { |
||||||
|
y = -y; |
||||||
|
y_cambio = true; |
||||||
|
} |
||||||
|
|
||||||
|
let mut prod: i64 = 0; |
||||||
|
|
||||||
|
while y > 0 { |
||||||
|
if y % 2 != 0 { |
||||||
|
prod += x; |
||||||
|
} |
||||||
|
x *= 2; |
||||||
|
y /= 2; |
||||||
|
} |
||||||
|
|
||||||
|
if x_cambio { |
||||||
|
prod = -prod; |
||||||
|
} |
||||||
|
|
||||||
|
if y_cambio { |
||||||
|
prod = -prod; |
||||||
|
} |
||||||
|
|
||||||
|
println!("x*y={}", prod); |
||||||
|
} |
||||||
@ -0,0 +1,92 @@ |
|||||||
|
use std::io; |
||||||
|
use std::io::Write; |
||||||
|
use std::process; |
||||||
|
|
||||||
|
fn dividir(x: i64, y: i64, q: &mut i64, r: &mut i64) { |
||||||
|
if y == 0 { |
||||||
|
println!("ERROR: Division por cero!"); |
||||||
|
process::exit(1); |
||||||
|
} |
||||||
|
|
||||||
|
*q = 0; |
||||||
|
*r = x; |
||||||
|
if *r < 0 { |
||||||
|
*r = -*r; |
||||||
|
} |
||||||
|
|
||||||
|
let v: i64; |
||||||
|
let mut w: i64; |
||||||
|
if y >= 0 { |
||||||
|
v = y; |
||||||
|
w = y; |
||||||
|
} else { |
||||||
|
v = -y; |
||||||
|
w = -y; |
||||||
|
} |
||||||
|
|
||||||
|
while w <= *r { |
||||||
|
w *= 2; |
||||||
|
} |
||||||
|
|
||||||
|
while w > v { |
||||||
|
*q *= 2; |
||||||
|
w /= 2; |
||||||
|
if w <= *r { |
||||||
|
*r -= w; |
||||||
|
*q += 1; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
if x < 0 { |
||||||
|
*r = -*r; |
||||||
|
*q = -*q; |
||||||
|
} |
||||||
|
|
||||||
|
if y < 0 { |
||||||
|
*q = -*q; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
fn mostrar_salida(cociente: i64, resto: i64) { |
||||||
|
println!("Cociente: {}", cociente); |
||||||
|
println!("Resto: {}", resto); |
||||||
|
} |
||||||
|
|
||||||
|
fn main() { |
||||||
|
println!("**************************************************************"); |
||||||
|
println!("Se ingresan dos valores enteros, se muestra su cociente."); |
||||||
|
println!("Se utiliza el algoritmo 'desplazar y restar' (shift-subtract)."); |
||||||
|
println!("**************************************************************"); |
||||||
|
|
||||||
|
print!("x: "); |
||||||
|
io::stdout().flush().expect("Error de escritura!"); |
||||||
|
|
||||||
|
let mut renglon: String = String::new(); |
||||||
|
io::stdin() |
||||||
|
.read_line(&mut renglon) |
||||||
|
.expect("Error de lectura!"); |
||||||
|
let x: i64 = renglon |
||||||
|
.trim() |
||||||
|
.parse::<i64>() |
||||||
|
.expect("Se esperaba un numero entero!"); |
||||||
|
|
||||||
|
print!("y: "); |
||||||
|
io::stdout().flush().expect("Error de escritura!"); |
||||||
|
|
||||||
|
renglon = String::new(); |
||||||
|
io::stdin() |
||||||
|
.read_line(&mut renglon) |
||||||
|
.expect("Error de lectura!"); |
||||||
|
let y: i64 = renglon |
||||||
|
.trim() |
||||||
|
.parse::<i64>() |
||||||
|
.expect("Se esperaba un numero entero!"); |
||||||
|
|
||||||
|
let mut q: i64 = 0; |
||||||
|
let mut r: i64 = 0; |
||||||
|
|
||||||
|
dividir(x, y, &mut q, &mut r); |
||||||
|
|
||||||
|
mostrar_salida(q, r); |
||||||
|
} |
||||||
|
|
||||||
@ -0,0 +1,56 @@ |
|||||||
|
use std::io; |
||||||
|
use std::io::Write; |
||||||
|
use std::process; |
||||||
|
|
||||||
|
fn mcd(mut x: i64, mut y: i64) -> i64 { |
||||||
|
if x <= 0 || y <= 0 { |
||||||
|
println!("ERROR: El algoritmo requiere dos numeros enteros positivos!"); |
||||||
|
process::exit(1); |
||||||
|
} |
||||||
|
|
||||||
|
while x != y { |
||||||
|
if x < y { |
||||||
|
y -= x; |
||||||
|
} |
||||||
|
if y < x { |
||||||
|
x -= y; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
x |
||||||
|
} |
||||||
|
|
||||||
|
fn main() { |
||||||
|
println!("******************************************************************************"); |
||||||
|
println!("Se ingresan dos valores enteros positivos, se muestra su maximo comun divisor."); |
||||||
|
println!("Se utiliza el algoritmo de Euclides."); |
||||||
|
println!("******************************************************************************"); |
||||||
|
|
||||||
|
print!("x: "); |
||||||
|
io::stdout().flush().expect("Error de escritura!"); |
||||||
|
|
||||||
|
let mut renglon: String = String::new(); |
||||||
|
io::stdin() |
||||||
|
.read_line(&mut renglon) |
||||||
|
.expect("Error de lectura!"); |
||||||
|
let x: i64 = renglon |
||||||
|
.trim() |
||||||
|
.parse::<i64>() |
||||||
|
.expect("Se esperaba un numero entero!"); |
||||||
|
|
||||||
|
print!("y: "); |
||||||
|
io::stdout().flush().expect("Error de escritura!"); |
||||||
|
|
||||||
|
renglon = String::new(); |
||||||
|
io::stdin() |
||||||
|
.read_line(&mut renglon) |
||||||
|
.expect("Error de lectura!"); |
||||||
|
let y: i64 = renglon |
||||||
|
.trim() |
||||||
|
.parse::<i64>() |
||||||
|
.expect("Se esperaba un numero entero!"); |
||||||
|
|
||||||
|
print!("{} es el MCD entre ", mcd(x, y)); |
||||||
|
|
||||||
|
println!("{} y {}", x, y); |
||||||
|
} |
||||||
@ -0,0 +1,54 @@ |
|||||||
|
use std::io; |
||||||
|
use std::io::Write; |
||||||
|
|
||||||
|
const TRES: i64 = 3; |
||||||
|
|
||||||
|
fn es_impar_primo(n: i64) -> bool { |
||||||
|
let mut es_p: bool = true; |
||||||
|
let limite: i64 = f64::sqrt(n as f64) as i64; |
||||||
|
let mut d: i64 = TRES; |
||||||
|
while d <= limite && es_p { |
||||||
|
if n % d == 0 { |
||||||
|
es_p = false; |
||||||
|
} |
||||||
|
d += 2; |
||||||
|
} |
||||||
|
|
||||||
|
es_p |
||||||
|
} |
||||||
|
|
||||||
|
fn main() { |
||||||
|
println!("*********************************************************************************************"); |
||||||
|
println!("Se ingresa un valor entero positivo, se muestran los numeros primos menores que ese valor."); |
||||||
|
println!("Se utiliza una funcion booleana para determinar si un numero impar mayor que 1 es primo o no."); |
||||||
|
println!("*********************************************************************************************"); |
||||||
|
|
||||||
|
print!("x: "); |
||||||
|
io::stdout().flush().expect("Error de escritura!"); |
||||||
|
|
||||||
|
let mut renglon: String = String::new(); |
||||||
|
io::stdin() |
||||||
|
.read_line(&mut renglon) |
||||||
|
.expect("Error de lectura!"); |
||||||
|
let x: i64 = renglon |
||||||
|
.trim() |
||||||
|
.parse::<i64>() |
||||||
|
.expect("Se esperaba un numero entero!"); |
||||||
|
|
||||||
|
if x <= 2 { |
||||||
|
print!("No hay numeros primos menores que {}", x); |
||||||
|
} else { |
||||||
|
print!("Numeros primos menores que {}: 2", x); |
||||||
|
|
||||||
|
let mut n: i64 = TRES; |
||||||
|
|
||||||
|
while n < x { |
||||||
|
if es_impar_primo(n) { |
||||||
|
print!(" {}", n); |
||||||
|
} |
||||||
|
n += 2; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
println!(); |
||||||
|
} |
||||||
@ -0,0 +1,68 @@ |
|||||||
|
use std::io; |
||||||
|
use std::io::Write; |
||||||
|
|
||||||
|
fn raiz_cuadrada_de_positivo(x: f64) -> f64 { |
||||||
|
if x == 1.0 { |
||||||
|
return 1.0; |
||||||
|
} |
||||||
|
|
||||||
|
let mut izq: f64; |
||||||
|
let mut der: f64; |
||||||
|
|
||||||
|
if x < 1.0 { |
||||||
|
izq = x; |
||||||
|
der = 1.0; |
||||||
|
} else { |
||||||
|
izq = 1.0; |
||||||
|
der = x; |
||||||
|
} |
||||||
|
|
||||||
|
let mut r: f64 = (izq + der) / 2.0; |
||||||
|
|
||||||
|
while f64::abs(x - r * r) > 0.0000000001 { |
||||||
|
if r * r < x { |
||||||
|
izq = r; |
||||||
|
} else { |
||||||
|
der = r; |
||||||
|
} |
||||||
|
|
||||||
|
r = (izq + der) / 2.0; |
||||||
|
} |
||||||
|
|
||||||
|
r |
||||||
|
} |
||||||
|
|
||||||
|
fn main() { |
||||||
|
println!("**********************************************************"); |
||||||
|
println!("Se ingresa un valor numerico, se muestra su raiz cuadrada."); |
||||||
|
println!("Se utiliza el algoritmo de la biseccion."); |
||||||
|
println!("**********************************************************"); |
||||||
|
|
||||||
|
print!("x: "); |
||||||
|
io::stdout().flush().expect("Error de escritura!"); |
||||||
|
|
||||||
|
let mut renglon: String = String::new(); |
||||||
|
io::stdin() |
||||||
|
.read_line(&mut renglon) |
||||||
|
.expect("Error de lectura!"); |
||||||
|
let x: f64 = renglon.trim().parse::<f64>().expect("Se esperaba un numero!"); |
||||||
|
|
||||||
|
if x == 0.0 { |
||||||
|
println!("La raiz cuadrada de 0 es 0.00000000"); |
||||||
|
} else { |
||||||
|
let rc: f64 = raiz_cuadrada_de_positivo(f64::abs(x)); |
||||||
|
|
||||||
|
if x < 0.0 { |
||||||
|
if x == -1.0 { |
||||||
|
println!("Las raices cuadradas de -1 son +i y -i"); |
||||||
|
} else { |
||||||
|
println!( |
||||||
|
"Las raices cuadradas de {} son +{:.8}i y -{:.8}i", |
||||||
|
x, rc, rc |
||||||
|
); |
||||||
|
} |
||||||
|
} else { |
||||||
|
println!("Las raices cuadradas de {} son +{:.8} y -{:.8}", x, rc, rc); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,37 @@ |
|||||||
|
fn mostrar_espacios(n: i64) { |
||||||
|
let mut i: i64 = 0; |
||||||
|
|
||||||
|
while i < n { |
||||||
|
print!(" "); |
||||||
|
i += 1; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
fn main() { |
||||||
|
println!("*******************************************************"); |
||||||
|
println!("Se muestra una sinusoide graficada mediante asteriscos."); |
||||||
|
println!("El programa no utiliza 'use'."); |
||||||
|
println!("*******************************************************"); |
||||||
|
|
||||||
|
println!("x\t sin(x)"); |
||||||
|
|
||||||
|
let mut a: f64 = 0.0; |
||||||
|
|
||||||
|
let lim: f64 = 8.0 * f64::atan(1.0); |
||||||
|
|
||||||
|
while a < lim { |
||||||
|
let s: f64 = f64::sin(a); |
||||||
|
if s >= 0.0 { |
||||||
|
print!("{:.2}\t {:.5}", a, s); |
||||||
|
} else { |
||||||
|
print!("{:.2}\t{:.5}", a, s); |
||||||
|
} |
||||||
|
mostrar_espacios((25.0 + 24.0 * s) as i64); |
||||||
|
println!("*"); |
||||||
|
a += 0.1; |
||||||
|
} |
||||||
|
|
||||||
|
print!("{:.2}\t {:.5}", lim, 0.0); |
||||||
|
mostrar_espacios(25); |
||||||
|
println!("*"); |
||||||
|
} |
||||||
@ -0,0 +1,35 @@ |
|||||||
|
use std::io; |
||||||
|
use std::io::Write; |
||||||
|
|
||||||
|
fn factorial(n: i64) -> i64 { |
||||||
|
if n < 2 { |
||||||
|
1 |
||||||
|
} else { |
||||||
|
n * factorial(n - 1) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
fn main() { |
||||||
|
println!("****************************************************"); |
||||||
|
println!("Se ingresa un valor entero, se muestra su factorial."); |
||||||
|
println!("Se utiliza un algoritmo recursivo."); |
||||||
|
println!("****************************************************"); |
||||||
|
|
||||||
|
print!("n: "); |
||||||
|
io::stdout().flush().expect("Error de escritura!"); |
||||||
|
|
||||||
|
let mut renglon: String = String::new(); |
||||||
|
io::stdin() |
||||||
|
.read_line(&mut renglon) |
||||||
|
.expect("Error de lectura!"); |
||||||
|
let n: i64 = renglon |
||||||
|
.trim() |
||||||
|
.parse::<i64>() |
||||||
|
.expect("Se esperaba un numero entero!"); |
||||||
|
|
||||||
|
if n < 0 { |
||||||
|
println!("ERROR: El algoritmo requiere un numero entero no negativo!"); |
||||||
|
} else { |
||||||
|
println!("{}! es {}", n, factorial(n)); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,54 @@ |
|||||||
|
use std::io; |
||||||
|
use std::io::Write; |
||||||
|
|
||||||
|
fn potencia_rec(x: f64, n: i64) -> f64 { |
||||||
|
if n == 0 { |
||||||
|
return 1.0; |
||||||
|
} |
||||||
|
if n % 2 == 0 { |
||||||
|
let m: f64 = potencia_rec(x, n / 2); |
||||||
|
m * m |
||||||
|
} else { |
||||||
|
x * potencia_rec(x, n - 1) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
fn potencia(x: f64, n: i64) -> f64 { |
||||||
|
let p: f64; |
||||||
|
if n < 0 { |
||||||
|
p = potencia_rec(x, -n); |
||||||
|
1.0 / p |
||||||
|
} else { |
||||||
|
potencia_rec(x, n) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
fn main() { |
||||||
|
println!("*******************************************************************************************"); |
||||||
|
println!("Se ingresan el valor de una base y el valor entero de un exponente, se muestra la potencia."); |
||||||
|
println!("Se utiliza un algoritmo recursivo."); |
||||||
|
println!("*******************************************************************************************"); |
||||||
|
|
||||||
|
print!("b: "); |
||||||
|
io::stdout().flush().expect("Error de escritura!"); |
||||||
|
|
||||||
|
let mut renglon: String = String::new(); |
||||||
|
io::stdin() |
||||||
|
.read_line(&mut renglon) |
||||||
|
.expect("Error de lectura!"); |
||||||
|
let b: f64 = renglon.trim().parse::<f64>().expect("Se esperaba un numero!"); |
||||||
|
|
||||||
|
print!("e: "); |
||||||
|
io::stdout().flush().expect("Error de escritura!"); |
||||||
|
|
||||||
|
renglon = String::new(); |
||||||
|
io::stdin() |
||||||
|
.read_line(&mut renglon) |
||||||
|
.expect("Error de lectura!"); |
||||||
|
let e: i64 = renglon |
||||||
|
.trim() |
||||||
|
.parse::<i64>() |
||||||
|
.expect("Se esperaba un numero entero!"); |
||||||
|
|
||||||
|
println!("{} elevado a la {} es {}", b, e, potencia(b, e)); |
||||||
|
} |
||||||
@ -0,0 +1,59 @@ |
|||||||
|
use std::io; |
||||||
|
use std::io::Write; |
||||||
|
|
||||||
|
fn entero_a_hexa(n: i64) -> String { |
||||||
|
let mut hexa: String = String::from("0"); |
||||||
|
|
||||||
|
if n != 0 { |
||||||
|
hexa = String::new(); |
||||||
|
} |
||||||
|
|
||||||
|
let digitos: String = String::from("0123456789ABCDEF"); |
||||||
|
|
||||||
|
let mut cociente: i64 = n; |
||||||
|
|
||||||
|
while cociente != 0 { |
||||||
|
let resto: i64 = cociente % 16; |
||||||
|
|
||||||
|
let ch: char = digitos.as_str().chars().nth(resto as usize).unwrap(); |
||||||
|
|
||||||
|
hexa = format!("{}{}", ch, hexa); |
||||||
|
|
||||||
|
cociente /= 16; |
||||||
|
} |
||||||
|
|
||||||
|
hexa |
||||||
|
} |
||||||
|
|
||||||
|
fn main() { |
||||||
|
println!("*********************************************************************************************"); |
||||||
|
println!("Se muestran 16 numeros en decimal y en hexadecimal, al presionar Enter se muestran 16 mas."); |
||||||
|
println!("Se sale del programa escribiendo 'salir' y presionando Enter."); |
||||||
|
println!("*********************************************************************************************"); |
||||||
|
|
||||||
|
let mut opcion: String = String::new(); |
||||||
|
|
||||||
|
let mut num: i64 = 0; |
||||||
|
|
||||||
|
while opcion != "salir" && opcion != "SALIR" { |
||||||
|
println!("\nDEC\tHEX"); |
||||||
|
println!("---\t---"); |
||||||
|
|
||||||
|
let mut cont: i64 = 0; |
||||||
|
|
||||||
|
while cont < 16 { |
||||||
|
println!("{}\t{}", num, entero_a_hexa(num)); |
||||||
|
num += 1; |
||||||
|
cont += 1; |
||||||
|
} |
||||||
|
|
||||||
|
print!("Presione Enter o escriba 'salir' y presione Enter: "); |
||||||
|
io::stdout().flush().expect("Error de escritura!"); |
||||||
|
|
||||||
|
opcion = String::new(); |
||||||
|
io::stdin() |
||||||
|
.read_line(&mut opcion) |
||||||
|
.expect("Error de lectura!"); |
||||||
|
opcion = opcion.trim().to_string(); |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue