Skip to content

Instantly share code, notes, and snippets.

@grevych
Created September 17, 2020 01:30
Show Gist options
  • Save grevych/e63cc48dfe00fd038043c599b7b560fa to your computer and use it in GitHub Desktop.
Save grevych/e63cc48dfe00fd038043c599b7b560fa to your computer and use it in GitHub Desktop.
Find if a binary representation of an integer is a repeated (maybe incomplete) substring
def solution(n):
d = [0] * 30
l = 0
while (n > 0):
d[l] = n % 2
n //= 2
l += 1
for p in range(1, l - 1):
ok = True
print(d[:l-p])
for i in range(l - p):
print(i, p, d[:i + p + 1])
if d[i] != d[i + p]:
ok = False
break
if ok and l - p > p:
return p
return -1
print(solution(955))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment