TutorialEdge

Challenge 12 - Armstrong Numbers

Welcome, everyone to the discussion thread for the 12th Go challenge posted to the site :tada:

Feel free to show us how you solved the challenge by posting your solutions below!

package main

import "fmt"
import "math"

type MyInt int

func (nptr *MyInt) IsArmstrong() bool {
  n := *nptr
  if n > 999 && n < 100 {
    return false
  }
  
  n1 := float64(n / 100)
  n2 := float64((n%100) / 10)
  n3 := float64(n % 10)
  
  calculated := math.Pow(n1, 3) + math.Pow(n2, 3) + math.Pow(n3, 3)
  return float64(n) == calculated
}

func main() {
  fmt.Println("Armstrong Numbers")

  var num1 MyInt = 371
  fmt.Println(num1.IsArmstrong())
}
func (n *MyInt) IsArmstrong() bool {
	a := *n / 100
	b := *n % 100 / 10
	c := *n % 10
	return *n == a*a*a+b*b*b+c*c*c
}