Search code examples
pythonalgorithmpascals-triangle

Sum of Pascal's Triangle rows


I implemented an algorithm for finding sums of Pascal's Triangle rows, but it's slowly for the contest. My program passed 4 test cases, but failed in the next case with a runtime error. Can I make my code faster?

import math

n = int(input())

for i in range(n):
    print int(math.pow(2, (int(input())-1)))

Input Format is the first line contains the number of test cases T. Then T test cases follow:

2
1
3

Solution

  • Math.pow works with floats, so solution may be inexact for large exponent. Moreover, for values over 1023, it throws OverflowError.

    Use x ** y operator instead, or builtin pow function.