This commit is contained in:
Preston Baxter 2024-08-22 16:27:40 -05:00
parent 6623e60c0a
commit d9f34743c2
2 changed files with 63 additions and 0 deletions

22
cmd/20/main.go Normal file
View File

@ -0,0 +1,22 @@
package main
import(
"fmt"
ds "euler/pkg/datastructures"
)
func main() {
bi := ds.NewBigInt(1)
for i := 1; i <= 100; i += 1 {
bi.Multiply(i)
}
fmt.Printf("%s\n", bi.Stringify())
var sum uint64 = 0
for _, digit := range bi {
sum += uint64(digit)
}
fmt.Printf("sum: %d\n", sum)
}

View File

@ -0,0 +1,41 @@
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
}