/
16.go
86 lines (70 loc) · 2.77 KB
/
16.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"fmt"
"math"
)
func mul(a interface{}, b interface{}) []int {
f := a.([]int)
g := b.([]int)
if len(f) < len(g) {
t := f
f = g
g = t
}
//fmt.Println("f: ", f, "g: ", g)
//result := make([]int, (len(f)+len(g))*2)
// optimization, take care of memory allocation
result := make([]int, len(f)+len(g))
for j := len(g) - 1; j >= 0; j-- {
//initially pos (index of result ) has the same relative postion as j in array
pos := len(result) - (len(g) - j)
for i := len(f) - 1; i >= 0; i-- {
temp := g[j] * f[i]
result[pos] += temp % 10
result[pos-1] += temp / 10
pos--
}
// fmt.Println(result)
}
for i := len(result) - 1; i > 0; i-- {
temp := result[i]
result[i] = temp % 10
result[i-1] += temp / 10
}
return result
}
func main() {
a := []int{2, 3, 4, 5, 6}
b := []int{4, 5, 3, 1}
result := mul(a, b)
// fmt.Println(result)
sum := 0
inc := 1
for i := len(result) - 1; i > 0; i-- {
sum += result[i] * inc
inc = inc * 10
}
fmt.Println(sum)
// 2**50
c := []int{1, 1, 2, 5, 8, 9, 9, 9, 0, 6, 8, 4, 2, 6, 2, 4}
//d := []int{1, 1, 2, 5, 8, 9, 9, 9, 0, 6, 8, 4, 2, 6, 2, 4}
res := make([]int, 1000)
res[999] = 1
for i := 0; i < 20; i++ {
res = mul(res, c)
}
sum = 0
for i := len(res) - 1; i >= 0; i-- {
sum += res[i]
}
fmt.Println("sum: ", sum)
// what the fuck, there is math lib :(
o := math.Pow(2, 1000)
fmt.Printf("%.1f", o)
}
/*
result same as python3 result 2**1000
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0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