I'm trying to solve a problem in TalentBuddy using Python
The problem is :
Given a number N. Print to the standard output the total number of subsets that can be formed using the {1,2..N} set, but making sure that none of the subsets contain any two consecutive integers. The final count might be very large, this is why you must print the result modulo 524287.
I've worked the code. All of the tests are OK, except Test 6. I got OverFlowError
when the test is submitting 10000000
as the argument of my function. I don't know what should I do to resolve this error
My code :
import math
def count_subsets(n):
step1 = (1 / math.sqrt(5)) * (((1 + math.sqrt(5)) / 2) ** (n + 2))
step2 = (1 / math.sqrt(5)) * (((1 - math.sqrt(5)) / 2) ** (n + 2))
res = step1 - step2
print int(res) % 524287
I guess this is taking up memory a lot. I wrote this after I found a mathematical formula to the same topic on the Internet.
I guess my code isn't Pythonic at all.
How to do this, the "Pythonic" way? How to resolve the OverFlowError
?
EDIT: In the problem, I've given the example input 3
, and the result (output) is 5
.
Explanation: The 5 sets are, {}, {1}, {2}, {3}, {1,3}
.
However, in Test 6, the problem I've given are:
Summary for test #6
Input test:
[10000000]
Expected output:
165366
Your output:
Traceback (most recent call last):
On line 4, in function count_subsets:
step1 = (1 / math.sqrt(5)) * (((1 + math.sqrt(5)) / 2) ** (n + 2))
OverflowError: