aboutsummaryrefslogtreecommitdiff
path: root/entries/shayanh
diff options
context:
space:
mode:
authorBraxton Hall2022-10-24 00:37:00 +0000
committerGitHub2022-10-24 00:37:00 +0000
commit6917a3eb3526a413faf4b1f7b3342afe6fca86c6 (patch)
tree84007eaff557e96580cb824fa7ec7ac659f63db9 /entries/shayanh
parent2dbd7c53ce88c10e1b47d0c7a878ccb68513a00b (diff)
parent5373b614a4cde708e59c449fd878b2e8b940f83d (diff)
Merge pull request #11 from shayanh/main
added shayanh's entry
Diffstat (limited to 'entries/shayanh')
-rw-r--r--entries/shayanh/matrix.go40
1 files changed, 40 insertions, 0 deletions
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]
+}