Advertisement
Toumo

contador_congruentes.py

Oct 16th, 2022 (edited)
1,063
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.78 KB | Source Code | 0 0
  1. # Contador de enteros congruentes r (d) en rango
  2.  
  3. def congruentesEnRango(r: int, d: int, m: int, M: int,
  4.                        strictGreater: bool = False,
  5.                        strictLess: bool = False) -> list:
  6.     """
  7.    Requiere r en N0, d en N, m, M en Z y m < M.
  8.    Devuelve la lista de enteros con congruencia r módulo d en el intervalo entre m y M.
  9.    Si este intervalo es abierto o cerrado en uno o ambos de sus extremos queda determinado por strictGreater y strictLess.
  10.    Por defecto, el intervalo es [m; M].
  11.    """
  12.     m += strictGreater
  13.     m += (m%d > r%d)*d - (m%d) + r%d
  14.     M -= strictLess
  15.     c = (M-m >= 0) * ((M-m)//d + 1)
  16.     return (c > 0) * [m + i*d for i in range(c)]
  17.  
  18. def nCongruentesEnRango(r: int, d: int, m: int, M: int,
  19.                         strictGreater: bool = False,
  20.                         strictLess: bool = False) -> int:
  21.     """
  22.    Requiere r en N0, d en N, m, M en Z y m < M.
  23.    Devuelve la cantidad de enteros con congruencia r módulo d en el intervalo entre m y M.
  24.    Si este intervalo es abierto o cerrado en uno o ambos de sus extremos queda determinado por strictGreater y strictLess.
  25.    Por defecto, el intervalo es [m; M].
  26.    """
  27.     m += strictGreater
  28.     m += (m%d > r%d)*d - (m%d) + r%d
  29.     M -= strictLess
  30.     return (M-m >= 0) * ((M-m)//d + 1)
  31.  
  32. def nParesEnRango(m: int, M: int,
  33.                   strictGreater: bool = False,
  34.                   strictLess: bool = False) -> int:
  35.     """
  36.    Requiere r en N0, d en N, m, M en Z y m < M.
  37.    Devuelve la cantidad de enteros pares en el intervalo entre m y M.
  38.    Si este intervalo es abierto o cerrado en uno o ambos de sus extremos queda determinado por strictGreater y strictLess.
  39.    Por defecto, el intervalo es [m; M].
  40.    """
  41.     return nCongruentesEnRango(0, 2, m, M, strictGreater, strictLess)
  42.  
  43. def nImparesEnRango(m: int, M: int,
  44.                     strictGreater: bool = False,
  45.                     strictLess: bool = False) -> int:
  46.     """
  47.    Requiere r en N0, d en N, m, M en Z y m < M.
  48.    Devuelve la cantidad de enteros impares en el intervalo entre m y M.
  49.    Si este intervalo es abierto o cerrado en uno o ambos de sus extremos queda determinado por strictGreater y strictLess.
  50.    Por defecto, el intervalo es [m; M].
  51.    """
  52.     return nCongruentesEnRango(1, 2, m, M, strictGreater, strictLess)
  53.  
  54. if __name__ == '__main__':
  55.     m, M, strictGreater, strictLess = 100, 200, True, False # Rango (100, 200]
  56.     listaPares   = congruentesEnRango(0, 2, m, M, strictGreater, strictLess)
  57.     listaImpares = congruentesEnRango(1, 2, m, M, strictGreater, strictLess)
  58.     nPares       = nParesEnRango(m, M, strictGreater, strictLess)
  59.     nImpares     = nImparesEnRango(m, M, strictGreater, strictLess)
  60.     print(nPares, nImpares, listaPares, listaImpares)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement