TutorialEdge

Challenge 16 - Odd or Even Factors

Welcome, everyone to the discussion thread for the 16th 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"

func OddEvenFactors(num int) string {
  factors := CheckFactors(num)
  if len(factors) % 2 == 0 {
    return "even"
  }
  return "odd"
}

func CheckFactors(num int) []int {
  factors := []int {1, num}
  limit := int(math.Ceil(float64(num)/2))
  for i := 2; i <= limit; i++ {
    if num % i == 0 {
      factors = append(factors, i) 
    }
  }
  return factors
}

func main() {
  fmt.Println("Odd or Even Factors")

  numFactors := OddEvenFactors(23)
  fmt.Println(numFactors) // "even"

  numFactors = OddEvenFactors(36)
  fmt.Println(numFactors) // "odd"
}
package main

import "fmt"

func OddEvenFactors(num int) string {
	fac := getFactors(num)
	if len(fac)%2 == 0 {
		return "even"
	}
	return "odd"
}

func getFactors(num int) []int {
	var factors []int
	factors = append(factors, 1)

out:
	for i := 2; i <= num; i++ {
		if num%i == 0 {
			factors = append(factors, i)
			num = num / i
			goto out
		}
	}
	return factors
}

func main() {
	fmt.Println("Odd or Even Factors")

	numFactors := OddEvenFactors(23)
	fmt.Println(numFactors) // "even"

	numFactors = OddEvenFactors(36)
	fmt.Println(numFactors) // "odd"

	fmt.Println(getFactors(36))
}