Created
October 4, 2023 12:32
-
-
Save kusa-mochi/ce23c214d8222b612e4f079f488124b5 to your computer and use it in GitHub Desktop.
calc conway-chain.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math/big" | |
) | |
// コンウェイのチェーン表記で a -> b -> c を計算する。 | |
func chain(a big.Int, b big.Int, c big.Int) big.Int { | |
if b.Cmp(big.NewInt(1)) == 0 { // b == 1 | |
return a | |
} | |
if c.Cmp(big.NewInt(1)) == 0 { // c == 1 | |
return *new(big.Int).Exp(&a, &b, nil) // a^b | |
} | |
// a -> (a -> (b-1) -> c) -> (c-1) | |
return chain( | |
a, | |
chain( | |
a, | |
*new(big.Int).Add(&b, big.NewInt(-1)), | |
c, | |
), | |
*new(big.Int).Add(&c, big.NewInt(-1)), | |
) | |
} | |
func main() { | |
var ( | |
a int64 = 3 | |
b int64 = 3 | |
c int64 = 2 | |
) | |
result := chain(*big.NewInt(a), *big.NewInt(b), *big.NewInt(c)) | |
fmt.Println(result.String()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment