loading...

re: Write a script to find "Happy Numbers" VIEW POST

FULL DISCUSSION
 

Golang Solution

package main

import (
    "fmt"
    "strconv"
    "strings"
)

func isHappyNumber(num int) bool {
    newNum := 0
    switch num {
    case 1:
        return true
    case 4:
        return false
    default:
        s := strconv.Itoa(num)
        digits := strings.Split(s, "")
        for _, i := range digits {
            n, _ := strconv.Atoi(i)
            newNum += n * n
        }
    }
    return isHappyNumber(newNum)
}

func main() {
    fmt.Println(isHappyNumber(94))
}
code of conduct - report abuse