Greeting fellas.
I was performing the algorithm intended to obtain the number of partitions of a integer. When I tested my program, margin errors started to occur when n > 257. I actually had the same problem on another matter while working with factorials, but my thoughts were on the algorithm itself having some flaws. I know that python 3.5.x doesn't really have limits on number size.
I even tried to do the sum the old fashioned way with every sum being from:
from decimal import Decimal
but it didn't change the result (only added a longer computational time).
#! /usr/bin/python3.5
from unittest import TestCase as Test
def number_partition(n):
"""
Building the 'Euler' triangle such as
n
p(n,j) = sum p(n-j,i)
i=j
"""
if n < 0: return 0
if n is 0: return 1
p = []
for i in range(n):
p.append([])
for j in range(i+1):
if j is i: p[i].append(1) ; break
p[i].append(sum(p[i-j-1][j:]))
return sum(p[n-1])
if __name__ == '__main__':
# number_partition(257)
Test.assertEqual(Test(), number_partition(257), 394723676655357) # Pass
Test.assertEqual(Test(), number_partition(258), 425933084409356) # returns 425933084409355
Test.assertEqual(Test(), number_partition(259), 459545750448675) # returns 459545750448673
So is the algorithm flawed, or is there a Python functionnality I didn't get ?
Thank you in advance for your time.