SHARE
TWEET

Q3 A B C

a guest Jan 17th, 2020 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # part A
  2.  
  3.  
  4. def commonFactor(n1, n2):
  5.     l = []
  6.     gcd = 1
  7.     i = min(n1, n2)
  8.  
  9.     while(i >= 1):
  10.         if(n1 % i == 0 and n2 % i == 0):
  11.             gcd = i
  12.             break
  13.         i -= 1
  14.     if(gcd == 1):
  15.         l.append(1)
  16.         print(l)
  17.     for j in range(2, gcd + 1):
  18.         if(gcd % j == 0):
  19.             l.append(j)
  20.     return l
  21.  
  22.  
  23. # part B
  24.  
  25.  
  26. def primeFactor(n):
  27.     l = []
  28.     i = 2
  29.     start = n
  30.     while(i <= n):
  31.         if(n % i == 0):
  32.             isprime = True
  33.             j = 2
  34.             while(j < i):
  35.                 if(i % j == 0):
  36.                     isprime = False
  37.                 j += 1
  38.             if(isprime == True):
  39.                 l.append(i)
  40.                 n /= i
  41.         else:
  42.             i += 1
  43.     print(start, '=', l[0], end=" ")
  44.     for j in range(1, len(l)):
  45.         print('*', l[j], end=" ")
  46.     print()
  47.     return l
  48.  
  49.  
  50. # part C
  51.  
  52.  
  53. def LCM(numbers):
  54.     mx = 0
  55.     for i in numbers:
  56.         if(mx < i):
  57.             mx = i
  58.     j = 1
  59.     while(True):
  60.         cur = mx * j
  61.         cnt = 0
  62.         for i in numbers:
  63.             if(cur % i == 0):
  64.                 cnt += 1
  65.         if(cnt == len(numbers)):
  66.             return cur
  67.             break
  68.         j += 1
  69.  
  70.  
  71. print("Part a")
  72. print(commonFactor(12, 21))
  73. print(commonFactor(60, 336))
  74. print("==========")
  75. print("Part b")
  76. print(primeFactor(24))
  77. print(primeFactor(31))
  78. print(primeFactor(420))
  79. print("==========")
  80. print("Part c")
  81. print(LCM([3, 4, 6, 10]))
  82. print(LCM([2, 3, 8, 12, 16, 21]))
  83. print("==========")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top