Advertisement
jbn6972

Untitled

Aug 14th, 2022
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.55 KB | None | 0 0
  1. def count_set_bits(n):
  2.     count = 0
  3.     while n:
  4.         n &= n - 1
  5.         count += 1
  6.     return count
  7.  
  8. def Zseq(P,Q,N):
  9.     values = [0 for  in range(N+1)]
  10.     values[0] = 2
  11.     bit_count = {}
  12.     bit_count[0] = count_set_bits(0)
  13.  
  14.     for i in range(1,N+1):
  15.         # print(values[i-1])
  16.         if values[i-1] in bit_count:
  17.             count = bit_count[values[i-1]]
  18.         else:
  19.             count = count_set_bits(values[i-1])
  20.             bit_count[values[i-1]] = count
  21.  
  22.         values[i] =  P * count + Q
  23.     return count_setbits(values[N])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement