Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created December 18, 2018 02:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/11d33564225dd85ab6be3297c9eb5aaa to your computer and use it in GitHub Desktop.
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
/*
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