Created
May 20, 2021 03:26
-
-
Save wimglenn/ea4a58b69339bccef13fa9468030b744 to your computer and use it in GitHub Desktop.
fill in presized lists instead of appending
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def duel(a, b): | |
fa = 16807 | |
fb = 48271 | |
d = 0x7fffffff | |
count1 = 0 | |
a4 = 5000000*[0] | |
b8 = 5000000*[0] | |
na = nb = 0 | |
for i in range(40000000): | |
a = (a * fa) % d | |
b = (b * fb) % d | |
a16 = a & 0xffff | |
b16 = b & 0xffff | |
count1 += a16 == b16 | |
if na < 5000000 and not a16 & 0b11: | |
a4[na] = a16 | |
na += 1 | |
if nb < 5000000 and not b16 & 0b111: | |
b8[nb] = b16 | |
nb += 1 | |
while nb < 5000000: | |
b = (b * fb) % d | |
b16 = b & 0xffff | |
if not b16 & 0b111: | |
b8[nb] = b16 | |
nb += 1 | |
count2 = sum(a4[i] == b8[i] for i in range(5000000)) | |
return count1, count2 | |
assert duel(783, 325) == (650, 336) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment