-1

For project Euler problem 21, I am trying to implement Thabit's theorem, obtaining the PQR values with this function:

def getPQR(n): 
  pqr = []

  p = (3 * 2^(n - 1) - 1)
  pqr.append(p)

  q = (3 * 2^n) - 1
  pqr.append(q)

  r = (9 * 2^((2*n) - 1) - 1)
  pqr.append(r)

  return pqr 

For n = 2, the function should return [5, 11, 71], but it instead returns [6, 3, 16]

Any help as to what I'm missing would be greatly appreciated! Thank you!

1 Answers1

1

^ is a bitwise operator in Python. ** is the operator you're looking for - to raise a base to an exponent.

So, you can modify your code as such:

def getPQR(n): 
  pqr = []

  p = (3 * 2**(n - 1) - 1)
  pqr.append(p)

  q = (3 * 2**n) - 1
  pqr.append(q)

  r = (9 * 2**((2*n) - 1) - 1)
  pqr.append(r)

  return pqr
metro
  • 508
  • 2
  • 9