Skip to content

Instantly share code, notes, and snippets.

View aershov24's full-sized avatar
🇦🇺

Alex Ershov aershov24

🇦🇺
View GitHub Profile
@aershov24
aershov24 / Markdium-Java.java
Created October 13, 2020 05:28
Markdium-14 Fibonacci Interview Questions (SOLVED) To Brush Before Coding Interview
public class Fibonacci_Search
{
static int kk = -1, nn = -1;
static int fib[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,
377, 610, 98, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368,
75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309,
3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986,
102334155, 165580141 };
static int fibsearch(int a[], int n, long x)
@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){
let arr = [0, 1]
for (let i = 2; i < n + 1; i++){
arr.push(arr[i - 2] + arr[i -1])
}
return arr[n]
}
@aershov24
aershov24 / Markdium-JavaScript.js
Created October 13, 2020 05:28
Markdium-14 Fibonacci Interview Questions (SOLVED) To Brush Before Coding Interview
Fibonacci(0) = 0,
Fibonacci(1) = 1,
Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2)
@aershov24
aershov24 / Markdium-C#.cs
Created October 13, 2020 05:28
Markdium-14 Fibonacci Interview Questions (SOLVED) To Brush Before Coding Interview
#include
#include
#include "search.h"
typedef struct FibonacciValues
{
// Fibonacci number: F(n) = F(n-1) + F(n-2)
int Fn_2; // F(n-2)
int Fn_1; // F(n-1)
int Fn; // F(n)
@aershov24
aershov24 / Markdium-C#.cs
Created October 13, 2020 05:28
Markdium-14 Fibonacci Interview Questions (SOLVED) To Brush Before Coding Interview
bool isFibonacci(int w)
{
double X1 = 5 * Math.Pow(w, 2) + 4;
double X2 = 5 * Math.Pow(w, 2) - 4;
long X1_sqrt = (long)Math.Sqrt(X1);
long X2_sqrt = (long)Math.Sqrt(X2);
return (X1_sqrt*X1_sqrt == X1) || (X2_sqrt*X2_sqrt == X2) ;
}
@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-Python.py
Created October 13, 2020 05:28
Markdium-14 Fibonacci Interview Questions (SOLVED) To Brush Before Coding Interview
def F(n):
if n == 0: return 0
elif n == 1: return 1
else: return F(n-1)+F(n-2)
@aershov24
aershov24 / Markdium-Python.py
Created October 13, 2020 05:28
Markdium-14 Fibonacci Interview Questions (SOLVED) To Brush Before Coding Interview
def fib_iterative(n):
a, b = 0, 1
while n > 0:
a, b = b, a + b
n -= 1
return a
@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){
let arr = [0, 1];
for (let i = 2; i < n + 1; i++){
arr.push(arr[i - 2] + arr[i -1])
}
return arr[n]
}
@aershov24
aershov24 / Markdium-C#.cs
Created October 13, 2020 05:28
Markdium-14 Fibonacci Interview Questions (SOLVED) To Brush Before Coding Interview
static double inverseSqrt5 = 1 / Math.Sqrt(5);
static double phi = (1 + Math.Sqrt(5)) / 2;
/* should use
const double inverseSqrt5 = 0.44721359549995793928183473374626
const double phi = 1.6180339887498948482045868343656
*/
static int Fibonacci(int n) {
return (int)Math.Floor(Math.Pow(phi, n) * inverseSqrt5 + 0.5);
}