Advertisement
makispaiktis

PseudoHack

Sep 17th, 2020 (edited)
1,741
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.63 KB | None | 0 0
  1. from math import log2, pow, floor
  2. from random import randrange
  3. BEGIN = 2**10
  4. END = 2**20
  5.  
  6. def decimalToBinary(n):
  7.     if n == 0:
  8.         # print("Binary : 0 (No specified number of digits)")
  9.         return 0
  10.     elif n > END:
  11.         print("Cannot make calculations with numbers higher than 2^20")
  12.         return -1000
  13.     else:
  14.         numOfDigits = int(log2(n)) + 1
  15.         digits = list()
  16.         sum = n
  17.         for i in range(numOfDigits-1, -1, -1):
  18.             if sum >= pow(2, i):
  19.                 digits.append(1)
  20.                 sum -= pow(2, i)
  21.             else:
  22.                 digits.append(0)
  23.         for i in range(len(digits)):
  24.             digits[i] = str(digits[i])
  25.         return "".join(digits)
  26.  
  27. def completeWithZeros(digits, desiredLength):
  28.     n = len(digits)
  29.     result = ""
  30.     remainder = desiredLength - n
  31.     for i in range(remainder):
  32.         result += "0"
  33.     result += digits
  34.     return result
  35.  
  36. def hack(n, desiredLength):
  37.     digits = decimalToBinary(n)
  38.     result = completeWithZeros(digits, desiredLength)
  39.     print(result)
  40.  
  41. def printTime(millis):
  42.     seconds = millis / 1000
  43.     precision = 2
  44.     multiplier = 10 ** precision
  45.     secsPrinted = floor(multiplier * seconds) / multiplier
  46.     r = randrange(BEGIN, END)
  47.     print("It took " + str(secsPrinted) + " seconds for hacking " + str(r) + " archives.")
  48.  
  49. # MAIN FUNCTION
  50. from timeit import default_timer as timer
  51. start = timer()
  52. numOfSamples = 2 * (10**5)
  53. desiredLength = 20
  54. for i in range(numOfSamples):
  55.     rand = randrange(BEGIN, END)
  56.     hack(rand, desiredLength)
  57. end = timer()
  58. millis = 1000 * (end - start)
  59. printTime(millis)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement