I hardcoded prime numbers but here it is
package utils import "fmt" var primeNumbers = []int{2,3,5,7,11,13,17,19,23} func DecomposeFactorial(number int) string { var totalPrimeFactos []int for number > 1 { primeFactors := findPrimaryNumbers(number) totalPrimeFactos = append(totalPrimeFactos, primeFactors...) number-- } decomposedFactorial := "" for i, pn := range primeNumbers { count := elemCount(totalPrimeFactos, pn) if i != 0 && count > 0 { decomposedFactorial += " * " } if count > 1 { decomposedFactorial += fmt.Sprintf("%d^%d", pn, count) } else if count > 0 { decomposedFactorial += fmt.Sprintf("%d", pn) } } return decomposedFactorial } func findPrimaryNumbers(number int) []int { var primeFactors []int i := 0 for number > 1 { if number % primeNumbers[i] == 0 { primeFactors = append(primeFactors, primeNumbers[i]) number = number / primeNumbers[i] } else { i++ } } return primeFactors } func elemCount(array []int, item int) int { count := 0 for _, l := range array { if l == item { count++ } } return count }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I hardcoded prime numbers but here it is