Programmin-in-Python

Farthest Co-Prime within (2, 250)

Dec 19th, 2020 (edited)
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.82 KB | None | 0 0
  1. def common(arr1, arr2):
  2.     T3 = ()
  3.  
  4.     for i in arr1:
  5.         for j in arr2:
  6.             if i == j:
  7.                 T3 += (i,)
  8.  
  9.     return T3
  10.  
  11. def factors(n):
  12.     T1 = (1,)
  13.  
  14.     for i in range(2,n+1):
  15.         if n%i == 0:
  16.             T1 += (i,)
  17.  
  18.     return T1
  19.  
  20. def allCP(arr):
  21.     D1 = dict()
  22.     res = ()
  23.  
  24.     for i in arr:
  25.         factor_i = factors(i)
  26.         for j in range(2,251):
  27.             factor_j = factors(j)
  28.             common_f = common(factor_i, factor_j)
  29.  
  30.             if (len(common_f) == 1) and (1 in common_f):
  31.                 res += (j,)
  32.         else:
  33.             D1[i] = res
  34.             res = ()
  35.  
  36.     return D1
  37.  
  38. def farthestCP(arr):
  39.     D1 = allCP(arr)
  40.     val = [i for i in range(2,251)]
  41.     middle = len(val)//2
  42.     final = []
  43.  
  44.     for i in D1:
  45.         if i <= val[middle]:
  46.             farCP = max(D1[i])
  47.             final.append(farCP)
  48.         else:
  49.             farCP = min(D1[i])
  50.             final.append(farCP)
  51.  
  52.     print(final)
  53.  
  54. farthestCP([60,246,75,103,155,110])
Add Comment
Please, Sign In to add comment