fork download
  1. def create_square_roots_table(modulus=18):
  2. """
  3. """
  4. print("Елемент | 1-й корінь | 2-й корінь")
  5. print("--------+------------+------------")
  6.  
  7. # Словник для зберігання квадратних коренів
  8. roots_dict = {}
  9.  
  10. # Знаходимо всі квадрати та їх корені
  11. for x in range(modulus):
  12. square = (x * x) % modulus
  13. if square not in roots_dict:
  14. roots_dict[square] = []
  15. roots_dict[square].append(x)
  16.  
  17. # Виводимо таблицю для елементів, що мають квадратні корені
  18. for element in sorted(roots_dict.keys()):
  19. roots = sorted(roots_dict[element])
  20. if len(roots) == 1:
  21. print(f"{element:7} | {roots[0]:10} | n/a")
  22. else:
  23. print(f"{element:7} | {roots[0]:10} | {roots[1]:10}")
  24.  
  25. def main():
  26. print("Квадратні корені ")
  27. print("=============================================")
  28. create_square_roots_table(18)
  29.  
  30. if __name__ == "__main__":
  31. main()
Success #stdin #stdout 0.12s 14136KB
stdin
Standard input is empty
stdout
Квадратні корені 
=============================================
Елемент | 1-й корінь | 2-й корінь
--------+------------+------------
      0 |          0 |          6
      1 |          1 |         17
      4 |          2 |         16
      7 |          5 |         13
      9 |          3 |          9
     10 |          8 |         10
     13 |          7 |         11
     16 |          4 |         14