Last active
April 6, 2024 08:19
-
-
Save marcodali/78d6f79230d5cb644a6318e056a45d81 to your computer and use it in GitHub Desktop.
Código en Golang para descubrir si un numero es Narcisista como yo 😏
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
package main | |
import ( | |
"fmt" | |
"os" | |
"math" | |
"strconv" | |
) | |
func isNarcissistic (n int) bool { | |
stringN := strconv.Itoa(n) | |
length := float64(len(stringN)) | |
sum := 0 | |
for _, char := range stringN { | |
digit, err := strconv.Atoi(string(char)) | |
if err != nil { | |
panic(err) | |
} | |
sum += int(math.Pow(float64(digit), length)) | |
} | |
return sum == n | |
} | |
func main () { | |
var n int | |
fmt.Scan(&n) | |
fmt.Fprintln(os.Stderr, "num=", n) | |
fmt.Println(isNarcissistic(n)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment