some new things, mostly not working or finished
This commit is contained in:
46
fib/fib.c
Normal file
46
fib/fib.c
Normal file
@@ -0,0 +1,46 @@
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
|
||||
// tiny struct to hold a 2x2 matrix
|
||||
typedef struct {
|
||||
uint64_t m00, m01, m10, m11;
|
||||
} Matrix2x2;
|
||||
|
||||
// multiply two 2x2 matrices together
|
||||
Matrix2x2 multiply(Matrix2x2 a, Matrix2x2 b) {
|
||||
// standard 2x2 matrix multiplication
|
||||
return (Matrix2x2) {
|
||||
a.m00* b.m00 + a.m01 * b.m10, // top-left
|
||||
a.m00* b.m01 + a.m01 * b.m11, // top-right
|
||||
a.m10* b.m00 + a.m11 * b.m10, // bottom-left
|
||||
a.m10* b.m01 + a.m11 * b.m11 // bottom-right
|
||||
};
|
||||
}
|
||||
|
||||
// raise a matrix to the power n using binary exponentiation
|
||||
Matrix2x2 matrix_pow(Matrix2x2 base, int n) {
|
||||
Matrix2x2 result = { 1, 0, 0, 1 }; // start with identity
|
||||
while (n > 0) {
|
||||
if (n % 2 == 1) result = multiply(result, base); // if odd, multiply once
|
||||
base = multiply(base, base); // square the base
|
||||
n /= 2; // integer divide n by 2
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// get nth fibonacci number using matrix exponentiation
|
||||
uint64_t fibonacci_matrix(int n) {
|
||||
if (n == 0) return 0; // edge case
|
||||
Matrix2x2 base = { 1, 1, 1, 0 }; // Fibonacci Q-matrix
|
||||
Matrix2x2 result = matrix_pow(base, n - 1); // raise to (n-1)
|
||||
return result.m00; // top-left is F(n)
|
||||
}
|
||||
|
||||
int main() {
|
||||
int n = 50; // how many numbers to print
|
||||
for (int i = 0; i < n; i++) {
|
||||
printf("%llu ", fibonacci_matrix(i)); // print each fib number
|
||||
}
|
||||
printf("\n"); // newline at end
|
||||
return 0;
|
||||
}
|
||||
22
fib/fib.java
Normal file
22
fib/fib.java
Normal file
@@ -0,0 +1,22 @@
|
||||
public class recursive {
|
||||
|
||||
public static int fibonacci(int max) {
|
||||
|
||||
if (max <= 1) {
|
||||
return max;
|
||||
}
|
||||
|
||||
else {
|
||||
return fibonacci(max - 2) + fibonacci(max - 1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
int max = 10;
|
||||
for (int i = 0; i < max; i++) {
|
||||
System.out.print(fibonacci(i) + " ");
|
||||
}
|
||||
}
|
||||
}
|
||||
41
fib/fib.py
Normal file
41
fib/fib.py
Normal file
@@ -0,0 +1,41 @@
|
||||
# tiny class to hold a 2x2 matrix
|
||||
class Matrix2x2:
|
||||
def __init__(self, m00, m01, m10, m11):
|
||||
self.m00 = m00
|
||||
self.m01 = m01
|
||||
self.m10 = m10
|
||||
self.m11 = m11
|
||||
|
||||
# multiply two 2x2 matrices
|
||||
def multiply(a, b):
|
||||
return Matrix2x2(
|
||||
a.m00 * b.m00 + a.m01 * b.m10, # top-left
|
||||
a.m00 * b.m01 + a.m01 * b.m11, # top-right
|
||||
a.m10 * b.m00 + a.m11 * b.m10, # bottom-left
|
||||
a.m10 * b.m01 + a.m11 * b.m11 # bottom-right
|
||||
)
|
||||
|
||||
# raise a matrix to the power n using binary exponentiation
|
||||
def matrix_pow(base, n):
|
||||
result = Matrix2x2(1, 0, 0, 1) # start with identity matrix
|
||||
while n > 0:
|
||||
if n % 2 == 1: # if n is odd, multiply once
|
||||
result = multiply(result, base)
|
||||
base = multiply(base, base) # square the base
|
||||
n //= 2 # integer division
|
||||
return result
|
||||
|
||||
# get nth fibonacci number using matrix exponentiation
|
||||
def fibonacci_matrix(n):
|
||||
if n == 0:
|
||||
return 0 # edge case
|
||||
base = Matrix2x2(1, 1, 1, 0) # Fibonacci Q-matrix
|
||||
result = matrix_pow(base, n - 1)
|
||||
return result.m00 # top-left is F(n)
|
||||
|
||||
# print first n fibonacci numbers
|
||||
if __name__ == "__main__":
|
||||
n = 50
|
||||
for i in range(n):
|
||||
print(fibonacci_matrix(i), end=" ")
|
||||
print()
|
||||
22
fib/sfib.c
Normal file
22
fib/sfib.c
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <stdio.h>
|
||||
typedef unsigned long long U;
|
||||
|
||||
U fib(int n) {
|
||||
if (n < 2) return n;
|
||||
U a = 1, b = 1, c = 1, d = 0, x = 1, y = 0, z = 0, w = 1, t;
|
||||
while (n) {
|
||||
if (n & 1) {
|
||||
t = x * a + y * c; y = x * b + y * d; x = t;
|
||||
t = z * a + w * c; w = z * b + w * d; z = t;
|
||||
}
|
||||
t = a * a + b * c; b = a * b + b * d; a = t;
|
||||
t = c * a + d * c; d = c * b + d * d; c = t;
|
||||
n >>= 1;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv[]) {
|
||||
for (int i = 0;i < int(argv);i++) printf("%llu ", fib(i));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user