Skip to content

Instantly share code, notes, and snippets.

@juan-medina
Created March 13, 2020 14:31
Show Gist options
  • Select an option

  • Save juan-medina/a24c041868356cf680215c6bf7b61928 to your computer and use it in GitHub Desktop.

Select an option

Save juan-medina/a24c041868356cf680215c6bf7b61928 to your computer and use it in GitHub Desktop.
fibonacci jetbrains quest
import math
def fib(n):
F = [[1, 1], [1, 0]];
if (n == 0):
return 0;
power(F, n - 1);
return F[0][0];
def multiply(F, M):
x = F[0][0] * M[0][0] + F[0][1] * M[1][0];
y = F[0][0] * M[0][1] + F[0][1] * M[1][1];
z = F[1][0] * M[0][0] + F[1][1] * M[1][0];
w = F[1][0] * M[0][1] + F[1][1] * M[1][1];
F[0][0] = x;
F[0][1] = y;
F[1][0] = z;
F[1][1] = w;
def power(F, n):
if( n == 0 or n == 1):
return;
M = [[1, 1], [1, 0]];
power(F, int(n / 2));
multiply(F, F);
if (n % 2 != 0):
multiply(F, M);
def findFirstAndLastDigits(n, d):
c = fib(n);
f = c // 10 ** (int(math.log(c, 10)) - d + 1)
l = c % pow(10, d);
return f, l;
print(findFirstAndLastDigits(50000000, 4))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment