其他分享
首页 > 其他分享> > Golang 003. 分解阶乘的质因数

Golang 003. 分解阶乘的质因数

作者:互联网

【基础入门题】Golang 003. 分解阶乘的质因数

分解 n! 的质因数,写成连乘的样式,因数多于一个用^m表示个数。
如:Factor(6) = 2^4x3^2x5; Factor(8) = 2^7x3^2x5x7


方法一:

package main

import (
	"fmt"
	"strconv"
)

func Factorial(n int) int {
	result := 1
	for i := 1; i <= n; i++ {
		result *= i
	}
	return result
}

func Factor(n int) string {
	result := ""
	n = Factorial(n)
	for i := 2; i <= n; i++ {
		count := 0
		for n%i == 0 {
			count++
			n /= i
		}
		if count > 0 {
			if count == 1 {
				result += "x" + strconv.Itoa(i)
			} else {
				result += "x" + strconv.Itoa(i) + "^" + strconv.Itoa(count)
			}
		}
	}
	return result[1:]
}

func main() {

	fmt.Printf("Factor(%d) = %s\n", 6, Factor(6))
	fmt.Printf("Factor(%d) = %s\n", 8, Factor(8))

}

方法二:

package main

import (
	"fmt"
	"strconv"
)

func Factorial(n int) int {
	result := 1
	for i := 1; i <= n; i++ {
		result *= i
	}
	return result
}

func Factor(n int) string {
	result := ""
	n = Factorial(n)
	for i := 2; i <= n; i++ {
		count := 0
		for n%i == 0 {
			count++
			n /= i
			if count == 1 {
				result += "x" + strconv.Itoa(i)
			}
		}
		if count > 1 {
			result += "^" + strconv.Itoa(count)
		}
	}
	return result[1:]
}

func main() {

	fmt.Printf("Factor(%d) = %s\n", 6, Factor(6))
	fmt.Printf("Factor(%d) = %s\n", 8, Factor(8))
	fmt.Printf("Factor(%d) = %s\n", 10, Factor(10))

	/* Out:
	Factor(6) = 2^4x3^2x5
	Factor(8) = 2^7x3^2x5x7
	Factor(10) = 2^8x3^4x5^2x7
	*/

}

方法三:【优化】合并阶乘函数、减少循环次数

package main

import (
	"fmt"
	"strconv"
)

func Factor(m int) string {
	n := 1
	for i := 1; i <= m; i++ {
		n *= i
	}
	result := ""
	for i := 2; i <= m; i++ {
		count := 0
		for n%i == 0 {
			count++
			n /= i
			if count == 1 {
				result += "x" + strconv.Itoa(i)
			}
		}
		if count > 1 {
			result += "^" + strconv.Itoa(count)
		}
	}
	return result[1:]
}

func main() {

	fmt.Printf("Factor(%d) = %s\n", 6, Factor(6))
	fmt.Printf("Factor(%d) = %s\n", 8, Factor(8))
	fmt.Printf("Factor(%d) = %s\n", 10, Factor(10))

	/* Out:
	Factor(6) = 2^4x3^2x5
	Factor(8) = 2^7x3^2x5x7
	Factor(10) = 2^8x3^4x5^2x7
	*/

}

方法四: 使用数组,追加后连接

package main

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

func Factor(m int) string {
	n := 1
	res := []string{}
	for i := 1; i <= m; i++ {
		n *= i
	}
	for i := 2; i <= m; i++ {
		count := 0
		for n%i == 0 {
			count++
			n /= i
		}
		if count == 1 {
			res = append(res, strconv.Itoa(i))
		} else if count > 1 {
			res = append(res, strconv.Itoa(i)+"^"+strconv.Itoa(count))
		}
	}

	return strings.Join(res, "x")
}

func main() {

	fmt.Printf("Factor(%d) = %s\n", 6, Factor(6))
	fmt.Printf("Factor(%d) = %s\n", 8, Factor(8))
	fmt.Printf("Factor(%d) = %s\n", 10, Factor(10))

}

方法五:使用bytes.Buffer

package main

import (
	"fmt"
	"bytes"
	"strconv"
)

func Factor(m int) string {
	var n int = 1
	var buf bytes.Buffer
	for i := 1; i <= m; i++ {
		n *= i
	}
	for i := 2; i <= m; i++ {
		count := 0
		for n%i == 0 {
			count++
			n /= i
			if count == 1 {
				buf.WriteString("x" + strconv.Itoa(i))
			}
		}
		if count > 1 {
			buf.WriteString("^" + strconv.Itoa(count))
		}
	}
	result := buf.String()
	return result[1:]
}

func main() {

	fmt.Printf("Factor(%d) = %s\n", 6, Factor(6))
	fmt.Printf("Factor(%d) = %s\n", 8, Factor(8))
	fmt.Printf("Factor(%d) = %s\n", 10, Factor(10))

}

方法六:和方法五类似,使用strings.Builder

package main

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

func Factor(m int) string {
	var n int = 1
	var buf strings.Builder
	for i := 1; i <= m; i++ {
		n *= i
	}
	for i := 2; i <= m; i++ {
		count := 0
		for n%i == 0 {
			count++
			n /= i
			if count == 1 {
				buf.WriteString("x" + strconv.Itoa(i))
			}
		}
		if count > 1 {
			buf.WriteString("^" + strconv.Itoa(count))
		}
	}
	result := buf.String()
	return result[1:]
}

func main() {

	fmt.Printf("Factor(%d) = %s\n", 6, Factor(6))
	fmt.Printf("Factor(%d) = %s\n", 8, Factor(8))
	fmt.Printf("Factor(%d) = %s\n", 10, Factor(10))

}

 欢迎加入CSDN社区! ​icon-default.png?t=LA92http://​https://bbs.csdn.net/forums/PythonTogether

标签:003,fmt,Factor,strconv,Golang,result,func,Printf,阶乘
来源: https://blog.csdn.net/boysoft2002/article/details/121647299