from django.shortcuts import render
from django.http import HttpResponse

def functions(request):
    return render(request, "Parameter/index.html")

def GCDPost(request):
    try:
        if request.method=="POST":
            data = request.POST
            x = int(data.get("x", 1))
            y = int(data.get("y", 1))
            return HttpResponse(gcd(x, y))
        else:
            return render(request, "Parameter/GCDPost.html")
    except:
        return HttpResponse("😱")  #\U0001F631

def GCD(request):
    x = int(request.GET.get('x', 1))
    y = int(request.GET.get('y', 1))
    return HttpResponse(gcd(x,y))

def LCM(request):
    x = int(request.GET.get('x', 1))
    y = int(request.GET.get('y', 1))
    return HttpResponse((x*y)//gcd(x,y))

def Fibonacci(request):
    n = int(request.GET.get('n', 2))
    s = "<table border='1'><tr>"
    for i in range(1, n+1):
        s += f"<th>{i}</th>"
    s += "</tr><tr>"
    x, y = 0, 1
    for i in range(1, n+1):
        s += f"<th>{x}</th>"
        x, y = y, y + x
    s += "</tr></table>"
    return HttpResponse(s)

def Primes(request):
    n = int(request.GET.get('n', 2))
    s = "<ol>"
    i = 1
    while True:
      if n<1: break
      if isPrime(i):
          s += f"<li>{i}</li>"
          n -= 1
      i += 1
    s += "</ol>"
    return HttpResponse(s)

def gcd(x:int, y:int)->int:
    while y!=0: x , y = y , x % y
    return x

def isPrime(n:int)->bool:
    x = 0
    if n<0: n = -n
    for x in range(2, n+1):
        if (n%x)==0: break
    return x==n