Skip to main content
Commonmark migration
Source Link

#335. Python 2 (Cython), 620 bytes, A000157

335. Python 2 (Cython), 620 bytes, A000157

from itertools import*
from math import*
from functools import*
n=int(input())+1#offset 1
a=0#answer
for p in permutations(range(n)):
 for i in range(2**n):#inversion
  v=[0]*(2**n)#visited
  c=0#number of cycles
  e=1#all is even
  for x in range(2**n):
   if v[x]:continue
   w=1#1 if this cycle is even
   while 1>v[x]:
    v[x]=1;w^=1
    x=reduce(lambda x,y:x+x+y,[1&(x>>s)for s in p])^i
   e&=w;c+=1
  a+=2**c*(1+e)
print(a//(2**n*factorial(n)*4))

#Come on... it's not that hard.
#Time complexity: 4**n*factorial(n)
#Memory complexity: 2**n
#(which is actually order of magnitude
#faster than the naive algorithm)

Try it online!

Next sequence


Originally intended to work in Python3, but Python 3 (Cython) raises an error "deallocating None".

Sorry for using Python, but programming on mobile is not easy. Posted 2 days ago in chat while waiting for someone else to post, probably with another language.

The next sequence has some chemistry-related things, but the recurrence relation is easy to implement. Ideally I hope someone will explain what the sequence is about. (I don't even explain most of my own answers...)

(Side note: Syntax highlighting fails for some comments)

#335. Python 2 (Cython), 620 bytes, A000157

from itertools import*
from math import*
from functools import*
n=int(input())+1#offset 1
a=0#answer
for p in permutations(range(n)):
 for i in range(2**n):#inversion
  v=[0]*(2**n)#visited
  c=0#number of cycles
  e=1#all is even
  for x in range(2**n):
   if v[x]:continue
   w=1#1 if this cycle is even
   while 1>v[x]:
    v[x]=1;w^=1
    x=reduce(lambda x,y:x+x+y,[1&(x>>s)for s in p])^i
   e&=w;c+=1
  a+=2**c*(1+e)
print(a//(2**n*factorial(n)*4))

#Come on... it's not that hard.
#Time complexity: 4**n*factorial(n)
#Memory complexity: 2**n
#(which is actually order of magnitude
#faster than the naive algorithm)

Try it online!

Next sequence


Originally intended to work in Python3, but Python 3 (Cython) raises an error "deallocating None".

Sorry for using Python, but programming on mobile is not easy. Posted 2 days ago in chat while waiting for someone else to post, probably with another language.

The next sequence has some chemistry-related things, but the recurrence relation is easy to implement. Ideally I hope someone will explain what the sequence is about. (I don't even explain most of my own answers...)

(Side note: Syntax highlighting fails for some comments)

335. Python 2 (Cython), 620 bytes, A000157

from itertools import*
from math import*
from functools import*
n=int(input())+1#offset 1
a=0#answer
for p in permutations(range(n)):
 for i in range(2**n):#inversion
  v=[0]*(2**n)#visited
  c=0#number of cycles
  e=1#all is even
  for x in range(2**n):
   if v[x]:continue
   w=1#1 if this cycle is even
   while 1>v[x]:
    v[x]=1;w^=1
    x=reduce(lambda x,y:x+x+y,[1&(x>>s)for s in p])^i
   e&=w;c+=1
  a+=2**c*(1+e)
print(a//(2**n*factorial(n)*4))

#Come on... it's not that hard.
#Time complexity: 4**n*factorial(n)
#Memory complexity: 2**n
#(which is actually order of magnitude
#faster than the naive algorithm)

Try it online!

Next sequence


Originally intended to work in Python3, but Python 3 (Cython) raises an error "deallocating None".

Sorry for using Python, but programming on mobile is not easy. Posted 2 days ago in chat while waiting for someone else to post, probably with another language.

The next sequence has some chemistry-related things, but the recurrence relation is easy to implement. Ideally I hope someone will explain what the sequence is about. (I don't even explain most of my own answers...)

(Side note: Syntax highlighting fails for some comments)

and fix a typo in the comment (won't change the bytecount or affect how the program runs)
Source Link
user202729
  • 17.4k
  • 2
  • 37
  • 70

#335. Python 2 (Cython), 620 bytes, A000157

from itertools import*
from math import*
from functools import*
n=int(input())+1#offset 1
a=0#answer
for p in permutations(range(n)):
 for i in range(2**n):#inversion
  v=[0]*(2**n)#visited
  c=0#number of cycles
  e=1#all is even
  for x in range(2**n):
   if v[x]:continue
   w=1#1 if this cycle is even
   while 1>v[x]:
    v[x]=1;w^=1
    x=reduce(lambda x,y:x+x+y,[1&(x>>s)for s in p])^i
   e&=w;c+=1
  a+=2**c*(1+e)
print(a//(2**n*factorial(n)*4))

#Come on... it's not that hard.
#Time complexity: 4**n*factorial(n)
#Memory complexity: 2**n
#(which is actually order of magnitude
#faster than the maivenaive algorithm)

Try it online!

Next sequence


Originally intended to work in Python3, but Python 3 (Cython) raises an error "deallocating None".

Sorry for using Python, but programming on mobile is not easy. Posted 2 days ago in chat while waiting for someone else to post, probably with another language.

Can anyone help me adding the header?

The next sequence has some chemistry-related things, but the recurrence relation is easy to implement. Ideally I hope someone will explain what the sequence is about. (I don't even explain most of my own answers...)

(Side note: Syntax highlighting fails for some comments)

#335. Python 2 (Cython), 620 bytes, A000157

from itertools import*
from math import*
from functools import*
n=int(input())+1#offset 1
a=0#answer
for p in permutations(range(n)):
 for i in range(2**n):#inversion
  v=[0]*(2**n)#visited
  c=0#number of cycles
  e=1#all is even
  for x in range(2**n):
   if v[x]:continue
   w=1#1 if this cycle is even
   while 1>v[x]:
    v[x]=1;w^=1
    x=reduce(lambda x,y:x+x+y,[1&(x>>s)for s in p])^i
   e&=w;c+=1
  a+=2**c*(1+e)
print(a//(2**n*factorial(n)*4))

#Come on... it's not that hard.
#Time complexity: 4**n*factorial(n)
#Memory complexity: 2**n
#(which is actually order of magnitude
#faster than the maive algorithm)

Try it online!

Next sequence


Originally intended to work in Python3, but Python 3 (Cython) raises an error "deallocating None".

Sorry for using Python, but programming on mobile is not easy. Posted 2 days ago in chat while waiting for someone else to post, probably with another language.

Can anyone help me adding the header?

The next sequence has some chemistry-related things, but the recurrence relation is easy to implement. Ideally I hope someone will explain what the sequence is about. (I don't even explain most of my own answers...)

(Side note: Syntax highlighting fails for some comments)

#335. Python 2 (Cython), 620 bytes, A000157

from itertools import*
from math import*
from functools import*
n=int(input())+1#offset 1
a=0#answer
for p in permutations(range(n)):
 for i in range(2**n):#inversion
  v=[0]*(2**n)#visited
  c=0#number of cycles
  e=1#all is even
  for x in range(2**n):
   if v[x]:continue
   w=1#1 if this cycle is even
   while 1>v[x]:
    v[x]=1;w^=1
    x=reduce(lambda x,y:x+x+y,[1&(x>>s)for s in p])^i
   e&=w;c+=1
  a+=2**c*(1+e)
print(a//(2**n*factorial(n)*4))

#Come on... it's not that hard.
#Time complexity: 4**n*factorial(n)
#Memory complexity: 2**n
#(which is actually order of magnitude
#faster than the naive algorithm)

Try it online!

Next sequence


Originally intended to work in Python3, but Python 3 (Cython) raises an error "deallocating None".

Sorry for using Python, but programming on mobile is not easy. Posted 2 days ago in chat while waiting for someone else to post, probably with another language.

The next sequence has some chemistry-related things, but the recurrence relation is easy to implement. Ideally I hope someone will explain what the sequence is about. (I don't even explain most of my own answers...)

(Side note: Syntax highlighting fails for some comments)

fix header and add next sequence
Source Link
Christian Sievers
  • 7.1k
  • 1
  • 19
  • 25

Python 2 (Cython), 620 bytes

#335. Python 2 (Cython), 620 bytes, A000157

from itertools import*
from math import*
from functools import*
n=int(input())+1#offset 1
a=0#answer
for p in permutations(range(n)):
 for i in range(2**n):#inversion
  v=[0]*(2**n)#visited
  c=0#number of cycles
  e=1#all is even
  for x in range(2**n):
   if v[x]:continue
   w=1#1 if this cycle is even
   while 1>v[x]:
    v[x]=1;w^=1
    x=reduce(lambda x,y:x+x+y,[1&(x>>s)for s in p])^i
   e&=w;c+=1
  a+=2**c*(1+e)
print(a//(2**n*factorial(n)*4))

#Come on... it's not that hard.
#Time complexity: 4**n*factorial(n)
#Memory complexity: 2**n
#(which is actually order of magnitude
#faster than the maive algorithm)

Try it online!

Next sequence


Originally intended to work in Python3, but Python 3 (Cython) raises an error "deallocating None".

Sorry for using Python, but programming on mobile is not easy. Posted 2 days ago in chat while waiting for someone else to post, probably with another language.

Can anyone help me adding the header?

The next sequence has some chemistry-related things, but the recurrence relation is easy to implement. Ideally I hope someone will explain what the sequence is about. (I don't even explain most of my own answers...)

(Side note: Syntax highlighting fails for some comments)

Python 2 (Cython), 620 bytes

from itertools import*
from math import*
from functools import*
n=int(input())+1#offset 1
a=0#answer
for p in permutations(range(n)):
 for i in range(2**n):#inversion
  v=[0]*(2**n)#visited
  c=0#number of cycles
  e=1#all is even
  for x in range(2**n):
   if v[x]:continue
   w=1#1 if this cycle is even
   while 1>v[x]:
    v[x]=1;w^=1
    x=reduce(lambda x,y:x+x+y,[1&(x>>s)for s in p])^i
   e&=w;c+=1
  a+=2**c*(1+e)
print(a//(2**n*factorial(n)*4))

#Come on... it's not that hard.
#Time complexity: 4**n*factorial(n)
#Memory complexity: 2**n
#(which is actually order of magnitude
#faster than the maive algorithm)

Try it online!


Originally intended to work in Python3, but Python 3 (Cython) raises an error "deallocating None".

Sorry for using Python, but programming on mobile is not easy. Posted 2 days ago in chat while waiting for someone else to post, probably with another language.

Can anyone help me adding the header?

The next sequence has some chemistry-related things, but the recurrence relation is easy to implement. Ideally I hope someone will explain what the sequence is about. (I don't even explain most of my own answers...)

(Side note: Syntax highlighting fails for some comments)

#335. Python 2 (Cython), 620 bytes, A000157

from itertools import*
from math import*
from functools import*
n=int(input())+1#offset 1
a=0#answer
for p in permutations(range(n)):
 for i in range(2**n):#inversion
  v=[0]*(2**n)#visited
  c=0#number of cycles
  e=1#all is even
  for x in range(2**n):
   if v[x]:continue
   w=1#1 if this cycle is even
   while 1>v[x]:
    v[x]=1;w^=1
    x=reduce(lambda x,y:x+x+y,[1&(x>>s)for s in p])^i
   e&=w;c+=1
  a+=2**c*(1+e)
print(a//(2**n*factorial(n)*4))

#Come on... it's not that hard.
#Time complexity: 4**n*factorial(n)
#Memory complexity: 2**n
#(which is actually order of magnitude
#faster than the maive algorithm)

Try it online!

Next sequence


Originally intended to work in Python3, but Python 3 (Cython) raises an error "deallocating None".

Sorry for using Python, but programming on mobile is not easy. Posted 2 days ago in chat while waiting for someone else to post, probably with another language.

Can anyone help me adding the header?

The next sequence has some chemistry-related things, but the recurrence relation is easy to implement. Ideally I hope someone will explain what the sequence is about. (I don't even explain most of my own answers...)

(Side note: Syntax highlighting fails for some comments)

Source Link
user202729
  • 17.4k
  • 2
  • 37
  • 70
Loading