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))
}
目录
相关文章
|
机器学习/深度学习 算法 Go
Golang每日一练(leetDay0061) 表列序号、阶乘后的零
Golang每日一练(leetDay0061) 表列序号、阶乘后的零
181 0
|
JavaScript 前端开发 Go
Golang语言[6] 递增的三元子序列/笨阶乘/矩阵查找/直方图的水量 |Go主题月(中)
给你一个整数数组 nums ,判断这个数组中是否存在长度为 3 的递增子序列。 如果存在这样的三元组下标 (i, j, k) 且满足 i &lt; j &lt; k ,使得 nums[i] &lt; nums[j] &lt; nums[k] ,返回 true ;否则,返回 false 。
203 0
|
JavaScript 前端开发 算法
Golang语言[6] 递增的三元子序列/笨阶乘/矩阵查找/直方图的水量 |Go主题月(上)
给你一个整数数组 nums ,判断这个数组中是否存在长度为 3 的递增子序列。 如果存在这样的三元组下标 (i, j, k) 且满足 i &lt; j &lt; k ,使得 nums[i] &lt; nums[j] &lt; nums[k] ,返回 true ;否则,返回 false 。
164 0
【Go语言入门100题】013 计算阶乘和 (10 分) Go语言|Golang
L1-013 计算阶乘和 (10 分) Go语言|Golang 对于给定的正整数N,需要你计算 S=1!+2!+3!+...+N!。
259 0
|
2月前
|
存储 安全 Java
【Golang】(4)Go里面的指针如何?函数与方法怎么不一样?带你了解Go不同于其他高级语言的语法
结构体可以存储一组不同类型的数据,是一种符合类型。Go抛弃了类与继承,同时也抛弃了构造方法,刻意弱化了面向对象的功能,Go并非是一个传统OOP的语言,但是Go依旧有着OOP的影子,通过结构体和方法也可以模拟出一个类。
228 1
|
Go
Golang语言之管道channel快速入门篇
这篇文章是关于Go语言中管道(channel)的快速入门教程,涵盖了管道的基本使用、有缓冲和无缓冲管道的区别、管道的关闭、遍历、协程和管道的协同工作、单向通道的使用以及select多路复用的详细案例和解释。
671 4
Golang语言之管道channel快速入门篇
|
Go
Golang语言文件操作快速入门篇
这篇文章是关于Go语言文件操作快速入门的教程,涵盖了文件的读取、写入、复制操作以及使用标准库中的ioutil、bufio、os等包进行文件操作的详细案例。
270 4
Golang语言文件操作快速入门篇
|
Go
Golang语言之gRPC程序设计示例
这篇文章是关于Golang语言使用gRPC进行程序设计的详细教程,涵盖了RPC协议的介绍、gRPC环境的搭建、Protocol Buffers的使用、gRPC服务的编写和通信示例。
570 3
Golang语言之gRPC程序设计示例
|
安全 Go
Golang语言goroutine协程并发安全及锁机制
这篇文章是关于Go语言中多协程操作同一数据问题、互斥锁Mutex和读写互斥锁RWMutex的详细介绍及使用案例,涵盖了如何使用这些同步原语来解决并发访问共享资源时的数据安全问题。
329 4