aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShayan Hosseini2022-10-24 00:25:21 +0000
committerShayan Hosseini2022-10-24 00:25:21 +0000
commite5de46c67d3edeb6e4bc96889a6594ab77f07265 (patch)
treeb8bb04ff568255370f44443f22ff2a96bc42f552
parent4be354380ae27d55b50ca795397e52fced857e53 (diff)
added shayanh's entry
-rw-r--r--README.md3
-rw-r--r--entries/shayanh/matrix.go40
2 files changed, 43 insertions, 0 deletions
diff --git a/README.md b/README.md
index efc713a..be6323b 100644
--- a/README.md
+++ b/README.md
@@ -34,6 +34,9 @@ For a submission to the upcoming "Reclaim your space" exhibition at [Hatch Art G
### [`Tarcisio-Teixeira`](https://github.com/Tarcisio-Teixeira)
- [`logn?`](./entries/Tarcisio-Teixeira/fib.py)
+### [`shayanh`](https://github.com/shayanh)
+- [`matrix`](./entries/shayanh/matrix.go)
+
## Contributing
To contribute just make a PR into the `main` branch!
diff --git a/entries/shayanh/matrix.go b/entries/shayanh/matrix.go
new file mode 100644
index 0000000..3f374cb
--- /dev/null
+++ b/entries/shayanh/matrix.go
@@ -0,0 +1,40 @@
+package main
+
+type fibmat [2][2]int
+
+func matmul(m1 fibmat, m2 fibmat) (m3 fibmat) {
+ for i := 0; i < 2; i++ {
+ for j := 0; j < 2; j++ {
+ for k := 0; k < 2; k++ {
+ m3[i][j] += m1[i][k] * m2[k][j]
+ }
+ }
+ }
+ return
+}
+
+func matpow(m fibmat, n int) fibmat {
+ if n == 0 {
+ return [2][2]int{
+ {1, 0},
+ {0, 1},
+ }
+ } else if n%2 == 0 {
+ t := matpow(m, n/2)
+ return matmul(t, t)
+ } else {
+ t := matpow(m, n-1)
+ return matmul(t, m)
+ }
+}
+
+func fib(n int) int {
+ m := matpow(
+ [2][2]int{
+ {1, 1},
+ {1, 0},
+ },
+ n,
+ )
+ return m[0][1]
+}