Advertisement
Toxotsist

fast mul and pow

Feb 28th, 2022
824
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.15 KB | None | 0 0
  1. from math import *
  2. import random
  3. def fast_mul(x, y):
  4.     v = x * y
  5.     b = False
  6.     if x < 0 and x < 0:
  7.         b = True
  8.     z = 0
  9.     q = 0
  10.     if x % 2 != 0:
  11.         q += 1
  12.         z += y
  13.     #print(x, y, z)
  14.     while x != 0 and x != -1:
  15.         if x > 0:
  16.             x = floor(x / 2)
  17.         else:
  18.             x = ceil(x / 2)
  19.         y = y * 2
  20.         if x % 2 != 0:
  21.             q += 1
  22.             z += y
  23.         #print(x, y, z)
  24.     if b:
  25.         z *= -1
  26.     #print(x, y, z)
  27.  
  28.     try:
  29.         assert z == v
  30.     except AssertionError:
  31.         print(False)
  32.         print(z, v)
  33.     else:
  34.         return z
  35.  
  36. #print(fast_mul(0,5))
  37.  
  38. def fast_pow(y, x):
  39.     v = pow(y, x)
  40.     b = False
  41.     c = False
  42.     if x != y:
  43.         c == True
  44.     if y % 2 == 0 and x < 0:
  45.         b = True
  46.     z = 1
  47.     q = 0
  48.     if x % 2 != 0:
  49.         q += 1
  50.         z *= y
  51.     #print(x, y, z)
  52.     while x != 0 and x != -1:
  53.         if x > 0:
  54.             x = floor(x / 2)
  55.         else:
  56.             x = ceil(x / 2)
  57.         y = fast_mul(y, y)
  58.         if x % 2 != 0:
  59.             q += 1
  60.             z *= y
  61.         print(x, y, z)
  62.     if b:
  63.         z *= -1
  64.     #if c:
  65.        # z = z - 1
  66.     #print(x, y, z)
  67.     try:
  68.         assert z == v
  69.     except AssertionError:
  70.         print(False)
  71.     else:
  72.         return print(z)
  73.  
  74. #fast_pow(7, 0)
  75.  
  76. def fast_mul_gen(y):
  77.     x = random.randint(2, 50)
  78.     v = x * y
  79.     b = False
  80.     print("" + str(x) + " умножаем на " + str(y))
  81.     if x < 0 and x < 0:
  82.         b = True
  83.     z = 0
  84.     q = 0
  85.     buf = 0
  86.     if x % 2 != 0:
  87.         q += 1
  88.         z += y
  89.         print("Z = " + str(buf) + " + " + str(y))
  90.         buf = y
  91.     while x != 0 and x != -1:
  92.         if x > 0:
  93.             x = floor(x / 2)
  94.         else:
  95.             x = ceil(x / 2)
  96.         y = y + y
  97.         if x % 2 != 0:
  98.             q += 1
  99.             z += y
  100.             print("Z = " + str(buf) + " + " + str(y))
  101.             buf = z
  102.     if b:
  103.         z *= -1
  104.  
  105.     try:
  106.         assert z == v
  107.     except AssertionError:
  108.         print(False)
  109.         print(z, v)
  110.     else:
  111.         return print("Z = " + str(z))
  112.  
  113. fast_mul_gen(4)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement