new files
This commit is contained in:
parent
3af7344581
commit
86c034bb8b
1364 changed files with 21352 additions and 0 deletions
9
Task/Ackermann-function/Go/ackermann-function-1.go
Normal file
9
Task/Ackermann-function/Go/ackermann-function-1.go
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
func Ackermann(m, n uint) uint {
|
||||
switch {
|
||||
case m == 0:
|
||||
return n + 1
|
||||
case n == 0:
|
||||
return Ackermann(m - 1, 1)
|
||||
}
|
||||
return Ackermann(m - 1, Ackermann(m, n - 1))
|
||||
}
|
||||
15
Task/Ackermann-function/Go/ackermann-function-2.go
Normal file
15
Task/Ackermann-function/Go/ackermann-function-2.go
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
func Ackermann2(m, n uint) uint {
|
||||
switch {
|
||||
case m == 0:
|
||||
return n + 1
|
||||
case m == 1:
|
||||
return n + 2
|
||||
case m == 2:
|
||||
return 2*n + 3
|
||||
case m == 3:
|
||||
return 8 << n - 3
|
||||
case n == 0:
|
||||
return Ackermann2(m - 1, 1)
|
||||
}
|
||||
return Ackermann2(m - 1, Ackermann2(m, n - 1))
|
||||
}
|
||||
53
Task/Ackermann-function/Go/ackermann-function-3.go
Normal file
53
Task/Ackermann-function/Go/ackermann-function-3.go
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
var one = big.NewInt(1)
|
||||
var two = big.NewInt(2)
|
||||
var three = big.NewInt(3)
|
||||
var eight = big.NewInt(8)
|
||||
var u uint
|
||||
var uBits = int(unsafe.Sizeof(u))*8 - 1
|
||||
|
||||
func Ackermann2(m, n *big.Int) *big.Int {
|
||||
if m.Cmp(three) <= 0 {
|
||||
switch m.Int64() {
|
||||
case 0:
|
||||
return new(big.Int).Add(n, one)
|
||||
case 1:
|
||||
return new(big.Int).Add(n, two)
|
||||
case 2:
|
||||
r := new(big.Int).Lsh(n, 1)
|
||||
return r.Add(r, three)
|
||||
case 3:
|
||||
if n.BitLen() > uBits {
|
||||
panic("way too big")
|
||||
}
|
||||
r := new(big.Int).Lsh(eight, uint(n.Int64()))
|
||||
return r.Sub(r, three)
|
||||
}
|
||||
}
|
||||
if n.BitLen() == 0 {
|
||||
return Ackermann2(new(big.Int).Sub(m, one), one)
|
||||
}
|
||||
return Ackermann2(new(big.Int).Sub(m, one),
|
||||
Ackermann2(m, new(big.Int).Sub(n, one)))
|
||||
}
|
||||
|
||||
func main() {
|
||||
show(0, 0)
|
||||
show(1, 2)
|
||||
show(2, 4)
|
||||
show(3, 100)
|
||||
show(4, 1)
|
||||
show(4, 3)
|
||||
}
|
||||
|
||||
func show(m, n int64) {
|
||||
fmt.Printf("A(%d, %d) = ", m, n)
|
||||
fmt.Println(Ackermann2(big.NewInt(m), big.NewInt(n)))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue