Skip to content

Instantly share code, notes, and snippets.

View aershov24's full-sized avatar
🇦🇺

Alex Ershov aershov24

🇦🇺
View GitHub Profile
@aershov24
aershov24 / Markdium-JavaScript.js
Created October 13, 2020 05:28
Markdium-14 Fibonacci Interview Questions (SOLVED) To Brush Before Coding Interview
// Iterative Solution for Fibonacci Numbers
function *fibonacci(n) {
const infinite = !n && n !== 0;
let current = 0;
let next = 1;
while (infinite || n--) {
yield current;
[current, next] = [next, current + next];
}
@aershov24
aershov24 / Markdium-Python.py
Created October 13, 2020 05:28
Markdium-14 Fibonacci Interview Questions (SOLVED) To Brush Before Coding Interview
def F():
a,b = 0,1
while True:
yield a
a, b = b, a + b
def SubFib(startNumber, endNumber):
for cur in F():
if cur > endNumber: return
if cur >= startNumber:
@aershov24
aershov24 / Markdium-JavaScript.js
Created October 13, 2020 05:28
Markdium-14 Fibonacci Interview Questions (SOLVED) To Brush Before Coding Interview
function fibs(n){
let [a, b] = [0, 1]
while (n > 0){
[a, b] = [b, a + b]
n -= 1
}
return a
}
@aershov24
aershov24 / Markdium-Java.java
Created October 13, 2020 05:28
Markdium-14 Fibonacci Interview Questions (SOLVED) To Brush Before Coding Interview
private static final int FIB_0 = 0;
private static final int FIB_1 = 1;
private int calcFibonacci(final int target) {
if (target == 0) { return FIB_0; }
if (target == 1) { return FIB_1; }
return calcFibonacci(target, 1, FIB_1, FIB_0);
}
@aershov24
aershov24 / Markdium-Java.java
Created October 13, 2020 05:28
Markdium-14 Fibonacci Interview Questions (SOLVED) To Brush Before Coding Interview
static int fibMemo[];
public static int fibByRecMemo(int num) {
if (num == 0) {
fibMemo[0] = 0;
return 0;
}
if (num == 1 || num == 2) {
@aershov24
aershov24 / Markdium-JavaScript.js
Created October 13, 2020 05:28
Markdium-14 Fibonacci Interview Questions (SOLVED) To Brush Before Coding Interview
function fib(n, a = 0, b = 1){
if (n > 0) {
return fib(n - 1, b, a + b)
}
return a
}
@aershov24
aershov24 / Markdium-Java.java
Created October 13, 2020 05:28
Markdium-14 Fibonacci Interview Questions (SOLVED) To Brush Before Coding Interview
class Solution {
int fib(int N) {
if (N <= 1) {
return N;
}
int[][] A = new int[][]{{1, 1}, {1, 0}};
matrixPower(A, N-1);
return A[0][0];
}
@aershov24
aershov24 / Markdium-JavaScript.js
Created October 13, 2020 05:28
Markdium-14 Fibonacci Interview Questions (SOLVED) To Brush Before Coding Interview
const sqrt = Math.sqrt;
const pow = Math.pow;
const fibCalc = n => Math.round(
(1 / sqrt(5)) *
(
pow(((1 + sqrt(5)) / 2), n) -
pow(((1 - sqrt(5)) / 2), n)
)
);
@aershov24
aershov24 / Markdium-Python.py
Created October 13, 2020 05:28
Markdium-14 Fibonacci Interview Questions (SOLVED) To Brush Before Coding Interview
def SubFib(startNumber, endNumber):
n = 0
cur = f(n)
while cur <= endNumber:
if startNumber <= cur:
print cur
n += 1
cur = f(n)
@aershov24
aershov24 / Markdium-Java.java
Created October 13, 2020 05:28
Markdium-14 Fibonacci Interview Questions (SOLVED) To Brush Before Coding Interview
private static long fibonacci(int n) {
double pha = pow(1 + sqrt(5), n);
double phb = pow(1 - sqrt(5), n);
double div = pow(2, n) * sqrt(5);
return (long)((pha - phb) / div);
}