def binary_partitions(n):
 from math import ceil, log
 pow = int((2 ** ceil(log(n, 2))))
 sum = 0
 partition = []
 while pow:
  if ((sum + pow) <= n):
   partition.append(pow)
   sum += pow
  pow >>= 1
 last_num = ((len(partition) - 1) - (n & 1))
 while (last_num >= 0):
  (yield partition)
  if (partition[last_num] == 2):
   partition[last_num] = 1
   partition.append(1)
   last_num -= 1
   continue
  partition.append(1)
  partition[last_num] >>= 1
  x = partition[(last_num + 1)] = partition[last_num]
  last_num += 1
  while (x > 1):
   if (x <= ((len(partition) - last_num) - 1)):
    del partition[((- x) + 1):]
    last_num += 1
    partition[last_num] = x
   else:
    x >>= 1
 (yield ([1] * n))

def binary_partitions ( n ) : from math import ceil , log pow = int ( ( 2 ** ceil ( log ( n , 2 ) ) ) ) sum = 0 partition = [ ] while pow : if ( ( sum + pow ) <= n ) : partition . append ( pow ) sum += pow pow >>= 1 last_num = ( ( len ( partition ) - 1 ) - ( n & 1 ) ) while ( last_num >= 0 ) : ( yield partition ) if ( partition [ last_num ] == 2 ) : partition [ last_num ] = 1 partition . append ( 1 ) last_num -= 1 continue partition . append ( 1 ) partition [ last_num ] >>= 1 x = partition [ ( last_num + 1 ) ] = partition [ last_num ] last_num += 1 while ( x > 1 ) : if ( x <= ( ( len ( partition ) - last_num ) - 1 ) ) : del partition [ ( ( - x ) + 1 ) : ] last_num += 1 partition [ last_num ] = x else : x >>= 1 ( yield ( [ 1 ] * n ) )
generates the binary partition of n .