当前位置:网站首页>Go程序员进化史
Go程序员进化史
2022-08-10 20:28:00 【用户9959267】
“写 n! 的若干种进化(退化)史
初级程序员
package fac
func Factorial(n int) int {
res := 1
for i := 1; i <= n; i++ {
res *= i
}
return res
}
学了递归的程序员
package fac
func Factorial(n int) int {
if n == 0 {
return 1
} else {
return Factorial(n - 1) * n
}
}
学了泛型的程序员
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的程序员
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的程序员
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程序员
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
}
高级程序员
package fac
// Factorial returns n!.
func Factorial(n int) int {
res := 1
for i := 1; i <= n; i++ {
res *= i
}
return res
}
Rob Pike
package fac
// Factorial returns n!.
func Factorial(n int) int {
res := 1
for i := 1; i <= n; i++ {
res *= i
}
return res
}
边栏推荐
- @Autowired annotation --required a single bean, but 2 were found causes and solutions
- Demis Hassabis:AI 的强大,超乎我们的想象
- leetcode 547.省份数量 并查集
- cordova installation error Command failed: powershell solution
- 参天生长大模型:昇腾AI如何强壮模型开发与创新之根?
- Linux服务器安装Redis,详细步骤。
- 验证码倒计时自定义hooks
- 不止跑路,拯救误操作rm -rf /*的小伙儿
- ACM MM 2022 统一归一化:加速Transformer工业部署的归一化方法
- C 语言 时间函数使用技巧(汇总)
猜你喜欢
三子棋的设计和代码
(10) Sequence and deserialization of image data
Public Key Retrieval is not allowed(不允许公钥检索)【解决办法】
【图像分类】2017-MobileNetV1 CVPR
[mysql] 深入分析MySQL版本控制MVCC规则
ACM MM 2022 统一归一化:加速Transformer工业部署的归一化方法
Heme - gold nanoparticles (Heme - AuNP) composite nanometer enzyme | gold nanoparticles nuclear porous hollow carbon nanometer spherical shell (Au @ HCNs) nano enzyme
爱丁堡大学最新《因果机器学习: 医疗健康与精准医疗应用》2022综述
Transferrin (TF) Modified Paclitaxel (PTX) Liposomes (TF-PTX-LP) | Transferrin (Tf) Modified Curcumin Liposomes
C语言系列——猜名次、猜凶手、打印杨辉三角
随机推荐
ACM MM 2022 统一归一化:加速Transformer工业部署的归一化方法
转铁蛋白(TF)修饰紫杉醇(PTX)脂质体(TF-PTX-LP)|转铁蛋白(Tf)修饰姜黄素脂质体
[SemiDrive source code analysis] [MailBox inter-core communication] 52 - DCF Notify implementation principle analysis and code combat
Demis Hassabis:AI 的强大,超乎我们的想象
QSslSocket has not been declared
姜还是老的辣,看看老战哥的老底儿和严谨劲儿
【毕业设计】基于STM32的天气预报盒子 - 嵌入式 单片机 物联网
WCF and TCP message communication practice, c # 】 【 realize group chat function
详叙c中的分支与循环
Implementation of graceful exit in Golang
报错:runtime error: reference binding to null pointer of type ‘std::vector<int, std::allocator<int>>‘
【语义分割】2016-SegNet TPAMI
A fullGC problem troubleshooting caused by groovy
Pt/CeO2 monatomic nanoparticles enzyme | H - rGO - Pt @ Pd NPs enzyme | carbon nanotube load platinum nanoparticles peptide modified nano enzyme | leukemia antagonism FeOPtPEG composite nano enzyme
The 2021 ICPC Asia Shanghai Regional Programming Contest D、E
redis如何查看key的有效期
二级指针的简单理解
图扑智慧电力可视化大屏,赋能虚拟电厂精准减碳
Metasploit——渗透攻击模块(Exploit)
线性结构----链表