Advertisement
miklis

trupmenu sudetis ir atimtis

Nov 24th, 2015
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. #MBK(a,b) -ieskomas maziausias bendras skaiciu a ir b kartotinis
  2. x="3/5 + 7/20" #ivesk trupmenu suma arba skirtuma
  3.  
  4. primes=[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331]
  5. def factor(x):
  6. RHS=[]
  7. for n in primes:
  8. while x%n==0:
  9. x/=n
  10. RHS.append(n)
  11. if n**2>x: break
  12. if x>1: RHS.append(x)
  13. return RHS
  14.  
  15. def MBK(a,b):
  16. A=factor(a)
  17. B=factor(b)
  18. C=[[n]*max(A.count(n),B.count(n)) for n in sorted(set(A+B))]
  19. C=reduce(lambda x,y: x+y, C)
  20. return reduce(lambda x,y: x*y, C)
  21.  
  22. A=x.index('/')
  23. if '+' in x: B=x.index('+')
  24. if '-' in x: B=x.index('-')
  25. C=B+1+x[B+1:].index('/')
  26. a=int(x[:A])
  27. b=int(x[A+1:B])
  28. c=int(x[B+1:C])
  29. d=int(x[C+1:])
  30. t=MBK(b,d)
  31. a1=t/b
  32. a2=t/d
  33.  
  34. print x+ " = "+str(a1*a)+'/'+str(t)+' '+ x[B]+str(a2*c)+'/'+str(t)
  35. if '+' in x: print " = "+str(a1*a+a2*c)+ "/" + str(t)
  36. if '-' in x: print " = "+str(a1*a-a2*c)+ "/" + str(t)
  37. print "bendras vardiklis: "+str(t)
  38. print "daugiklis pirmai trupmenai:",str(a1)
  39. print "daugiklis antrai trupmenai:",str(a2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement