Skip to content

Instantly share code, notes, and snippets.

@thejsj
Last active December 22, 2015 12:29
Show Gist options
  • Save thejsj/6472637 to your computer and use it in GitHub Desktop.
Save thejsj/6472637 to your computer and use it in GitHub Desktop.
Given any random number (even in this case), find the two closest integers divisible by 2, or the two integers closes to the square root divisible by 2
import math
import random
def calc(num):
resultFound = False
for i in range(1,100):
ii = math.pow(2, i)
s = math.sqrt(num)
r = int(round(s / ii) * ii)
if r > 0:
rr = int(num / r)
if r % 2 is 0 and rr % 2 == 0:
return r, rr
resultFound = True
break
if not resultFound:
return False
for i in range(100):
print calc(random.randrange(16,160,16))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment