# ==============================================================================
"""MULDIV : table of GCD/LCM (Greatest Common Divisor/Least Common Multiple)"""
# ==============================================================================
__author__  = "Christophe Schlick"
__version__ = "0.0" # skeleton version
__date__    = "2015-09-01"
__usage__   = """
User input : <n> (where n:int > 0)
App output: table of GCD/LCM from 1 to n"""
# ==============================================================================
from ezCLI import *
# ------------------------------------------------------------------------------
def gcd(p:int,q:int)->int:
  """return a Greatest Common Divisor between p and q"""
  while q !=0 :
    r = p % q
    p = q
    q = r

  return p
# ------------------------------------------------------------------------------
def lcm(p:int,q:int)->int:
  """return a Least Common Multiple between p and q"""
  return p*q//gcd(p,q)
# ------------------------------------------------------------------------------
def cell(p:int,q:int)->int:
  """return either the GCD or the LCM according to the cell position (p,q)"""
  # en fonction de p et q, renvoyer gcd(p,q) ou lcm(p,q)
  if p == 1:
    return q

  if p > q:
    return lcm(p,q)
  else:
    return gcd(p,q)
# ------------------------------------------------------------------------------
def muldiv(n:int)->str:
  """return a string containing the GCD/LCM table from 1 to n"""
  table = [[cell(p,q) for q in range(1, n+1)] for p in range(1, n+1)]; #inspect()
  return grid(table) # use the 'grid' function to format 'table' as a grid
# ------------------------------------------------------------------------------
def parser(command):
  """parse 'command' as integer 'n' before calling 'multable(n)'"""
  n = parse(command); #inspect()
  assert type(n) is int and n > 0, "<n> must be a strictly positive integer"
  return muldiv(n)
# ==============================================================================
if __name__ == "__main__":
  userloop(parser, "Enter value for <n>")
# ==============================================================================
