Skip to content

Instantly share code, notes, and snippets.

@marcodali
Last active April 6, 2024 08:19
Show Gist options
  • Save marcodali/78d6f79230d5cb644a6318e056a45d81 to your computer and use it in GitHub Desktop.
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 😏
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