How to use big integer in Go

2 Answers

0 votes
package main

import (
    "fmt"
    "math/big"
)

func main() {
    x := new(big.Int)
    
    x.Exp(big.NewInt(3), big.NewInt(242), nil)

    fmt.Println("x = ", x)
}

 
 
/*
run:
 
x =  29063214161986986067637023528620257232321357468243916695175073145996989031241146647825183302277227705597018408555209
 
*/

 



answered May 6 by avibootz
0 votes
package main

import (
    "fmt"
    "math/big"
)

func main() {
    // Create big integers
    a := new(big.Int)
    a.Exp(big.NewInt(3), big.NewInt(42), nil)

    b := new(big.Int)
    b.SetString("98765432109876543210", 10)

    // Perform arithmetic operations
    sum := new(big.Int).Add(a, b)
    diff := new(big.Int).Sub(a, b)
    product := new(big.Int).Mul(a, b)
    quotient := new(big.Int).Div(b, a)

    // Print results
    fmt.Println("a:", a)
    fmt.Println("b:", b)
    fmt.Println("Sum:", sum)
    fmt.Println("Difference:", diff)
    fmt.Println("Product:", product)
    fmt.Println("Quotient:", quotient)
}

 
 
/*
run:
 
a: 109418989131512359209
b: 98765432109876543210
Sum: 208184421241388902419
Difference: 10653557021635815999
Product: 10806813742599703257919201397276729920890
Quotient: 0

*/

 



answered May 6 by avibootz
...