Skip to content

Instantly share code, notes, and snippets.

@jm42
Created November 19, 2015 15:09
Show Gist options
  • Save jm42/88b79a8bfaa2065a261b to your computer and use it in GitHub Desktop.
Save jm42/88b79a8bfaa2065a261b to your computer and use it in GitHub Desktop.
Bruteforce al problema de lamparitas
# Hay 1000 lamparitas numeradas del 1 al 1000, las que son numeros primos
# tienen un interruptor que cambia el estado de la lamparita de el y de
# todos sus multiplos.
# Cual es el maximo de lamparitas que pueden haber prendidas?
#def divisores(n):
# d = []
# for i in range(1, n + 1):
# if n % i == 0:
# d.append(i)
# return d
#def es_primo(n):
# return len(divisores(n)) == 2
#def primos_hasta(n):
# p = []
# for i in range(1, n + 1):
# if es_primo(i):
# p.append(i)
# return p
#p = primos_hasta(1000)
p = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67,
71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139,
149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199]
# No van?
t = [211, 223,
227, 229, 233, 239, 241]
p = p + [251, 257, 263, 269, 271, 277, 281, 283, 293,
307, 311, 313, 317, 331]
# Agregan y sacan uno
#v = [337, 347, 349, 353, 359, 367, 373, 379, 383,
# 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463,
# 467, 479, 487, 491, 499]
c = [503, 509, 521, 523, 541, 547, 557, 563, 569,
571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647,
653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743,
751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839,
853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941,
947, 953, 967, 971, 977, 983, 991, 997]
def combina(p, n):
if n == 1:
return [[d] for d in p]
l = []
for d in combina(p, n - 1):
for r in p:
if r not in d:
a = sorted(d + [r])
if not a in l:
l.append(a)
return l
# 2 ** 168 = 374144419156711147060143317175368453031918731001856
# combina(p, 2) = 15865
def main():
s = None
t = 0
for d in combina(p, 3):
d = d + c
l = [False for i in range(1001)]
for i in d:
for m in range(i, 1001, i):
l[m] = not l[m]
h = l.count(True)
if h > t:
s = d
t = h
print(s, t)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment