Created
December 18, 2018 02:43
-
-
Save parzibyte/11d33564225dd85ab6be3297c9eb5aaa to your computer and use it in GitHub Desktop.
Narcisista en Rust created by parzibyte - https://repl.it/@parzibyte/Narcisista-en-Rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Algoritmo para determinar si un número es narcisista en Rust | |
@author parzibyte | |
*/ | |
fn main() { | |
let numeros = [1, 2, 153, 20, 371, 407, 201, 60]; | |
for &numero in numeros.iter(){ | |
println!("¿El número {} es narcisista? {}", numero, es_narcisista(numero)); | |
} | |
} | |
fn es_narcisista(numero: u64) -> bool{ | |
let numero_como_cadena = numero.to_string(); | |
let longitud = numero_como_cadena.len() as u32; | |
let mut suma: u64 = 0; | |
for caracter in numero_como_cadena.chars(){ | |
suma += caracter.to_digit(10).unwrap().pow(longitud) as u64; | |
} | |
return suma == numero; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment