Euler/pkg/datastructures/bigint.go

42 lines
604 B
Go
Raw Permalink Normal View History

2024-08-22 17:27:40 -04:00
package datastructures
import "fmt"
type BigInt []int
func NewBigInt(i int) BigInt {
num := i
bi := []int{}
for num > 0 {
bi = append(bi, num % 10)
num = num / 10
}
return bi
}
func (bi *BigInt) Multiply(i int) BigInt {
var carry int = 0
for index, digit := range *bi {
product := (digit * i) + carry
carry = product / 10
(*bi)[index] = product % 10
}
for carry > 0 {
*bi = append(*bi, carry % 10)
carry = carry / 10
}
return *bi
}
func (bi BigInt) Stringify() string {
out := ""
for i := len(bi)-1; i >= 0; i -= 1 {
out += fmt.Sprintf("%d", bi[i])
}
return out
}