Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from math import log2, pow, floor
- from random import randrange
- BEGIN = 2**10
- END = 2**20
- def decimalToBinary(n):
- if n == 0:
- # print("Binary : 0 (No specified number of digits)")
- return 0
- elif n > END:
- print("Cannot make calculations with numbers higher than 2^20")
- return -1000
- else:
- numOfDigits = int(log2(n)) + 1
- digits = list()
- sum = n
- for i in range(numOfDigits-1, -1, -1):
- if sum >= pow(2, i):
- digits.append(1)
- sum -= pow(2, i)
- else:
- digits.append(0)
- for i in range(len(digits)):
- digits[i] = str(digits[i])
- return "".join(digits)
- def completeWithZeros(digits, desiredLength):
- n = len(digits)
- result = ""
- remainder = desiredLength - n
- for i in range(remainder):
- result += "0"
- result += digits
- return result
- def hack(n, desiredLength):
- digits = decimalToBinary(n)
- result = completeWithZeros(digits, desiredLength)
- print(result)
- def printTime(millis):
- seconds = millis / 1000
- precision = 2
- multiplier = 10 ** precision
- secsPrinted = floor(multiplier * seconds) / multiplier
- r = randrange(BEGIN, END)
- print("It took " + str(secsPrinted) + " seconds for hacking " + str(r) + " archives.")
- # MAIN FUNCTION
- from timeit import default_timer as timer
- start = timer()
- numOfSamples = 2 * (10**5)
- desiredLength = 20
- for i in range(numOfSamples):
- rand = randrange(BEGIN, END)
- hack(rand, desiredLength)
- end = timer()
- millis = 1000 * (end - start)
- printTime(millis)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement