前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Go程序员进化史

Go程序员进化史

作者头像
薯条的编程修养
发布2022-08-10 19:31:44
2240
发布2022-08-10 19:31:44
举报
文章被收录于专栏:薯条的编程修养

“写 n! 的若干种进化(退化)史

初级程序员

代码语言:javascript
复制
package fac

func Factorial(n int) int {
 res := 1

 for i := 1; i <= n; i++ {
  res *= i
 }

 return res
}

学了递归的程序员

代码语言:javascript
复制
package fac

func Factorial(n int) int {
 if n == 0 {
  return 1
 } else {
  return Factorial(n - 1) * n
 }
}

学了泛型的程序员

代码语言:javascript
复制
package fac

func Factorial(n interface{}) interface{} {
 v, valid := n.(int)
 if !valid {
  return 0
 }

 res := 1

 for i := 1; i <= v; i++ {
  res *= i
 }

 return res
}

学了Goroutine的程序员

代码语言:javascript
复制
package fac

import "sync"

func Factorial(n int) int {
 var (
  left, right = 1, 1
  wg sync.WaitGroup
 )

 wg.Add(2)

 pivot := n / 2

 go func() {
  for i := 1; i < pivot; i++ {
   left *= i
  }

  wg.Done()
 }()

 go func() {
  for i := pivot; i <= n; i++ {
   right *= i
  }

  wg.Done()
 }()

 wg.Wait()

 return left * right
}

学了Channel的程序员

代码语言:javascript
复制
package fac

func Factorial(n int) <-chan int {
 ch := make(chan int)

 go func() {
  prev := 1

  for i := 1; i <= n; i++ {
   v := prev * i

   ch <- v

   prev = v
  }

  close(ch)
 }()

 return ch
}

刚转Go的Java程序员

代码语言:javascript
复制
package fac

/**
 * @see https://en.wikipedia.org/wiki/Factorial
 */
type IFactorial interface {
 CalculateFactorial() int
}

// FactorialImpl implements IFactorial.
var _ IFactorial = (*FactorialImpl)(nil)

/**
 * Used to find factorial of the n.
 */
type FactorialImpl struct {
 /**
  * The n.
  */
 n int
}

/**
 * Constructor of the FactorialImpl.
 *
 * @param n the n.
 */
func NewFactorial(n int) *FactorialImpl {
 return &FactorialImpl{
  n: n,
 }
}

/**
 * Gets the n to use in factorial function.
 *
 * @return int.
 */
func (this *FactorialImpl) GetN() int {
 return this.n
}

/**
 * Sets the n to use in factorial function.
 *
 * @param n the n.
 * @return void.
 */
func (this *FactorialImpl) SetN(n int) {
 this.n = n
}

/**
 * Returns factorial of the n.
 *
 * @todo remove "if" statement. Maybe we should use a factory or somthing?
 *
 * @return int.
 */
func (this *FactorialImpl) CalculateFactorial() int {
 if this.n == 0 {
  return 1
 }

 n := this.n
 this.n = this.n - 1

 return this.CalculateFactorial() * n
}

高级程序员

代码语言:javascript
复制
package fac

// Factorial returns n!.
func Factorial(n int) int {
 res := 1

 for i := 1; i <= n; i++ {
  res *= i
 }

 return res
}

Rob Pike

代码语言:javascript
复制
package fac

// Factorial returns n!.
func Factorial(n int) int {
 res := 1

 for i := 1; i <= n; i++ {
  res *= i
 }

 return res
}
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-04-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 薯条的编程修养 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 初级程序员
  • 学了递归的程序员
  • 学了泛型的程序员
  • 学了Goroutine的程序员
  • 学了Channel的程序员
  • 刚转Go的Java程序员
  • 高级程序员
  • Rob Pike
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档