0

I'm trying to store first 1000 bernoulli numbers in a dictionary in python. At first I just stored the numbers as it is. So I got an overflow error. Now after going through previous answers I thought of using decimal module.

So here it is

-5218507479961513801890596392421261361036935624312258325065379143295948300812040703848766095836974598734762472300638625802884257082786883956679824964010841565051175167717451747328911935282639583972372470105587187736495055501208701522099921363239317373617854217050435670713936357978555246779460902210809009009539232173 / 2291190

The 260th bernouli number. I was able to store all the previous ones in the dictionary.

This is the sample code I've written.

from decimal import *
d = Decimal
getcontext().prec = 10000
di = {260: d(-5218507479961513801890596392421261361036935624312258325065379143295948300812040703848766095836974598734762472300638625802884257082786883956679824964010841565051175167717451747328911935282639583972372470105587187736495055501208701522099921363239317373617854217050435670713936357978555246779460902210809009009539232173 / 2291190)}

This is the error snap shot

Error image

Is there any better way to handle such huge numbers ? Please tell me if there is some thing that can be done to store these numbers.

Bharat
  • 1,044
  • 15
  • 34
  • 1
    You aren't using the `decimal` module properly. You need to convert your terms to Decimal _before_ you do arithmetic on them. As an alternative, you can use the `fractions` module, and store your Bernoulli numbers as rational numbers – PM 2Ring Nov 18 '17 at 10:34
  • Ohh.. okay so actually what I have done is I used matlab to generate all these numbers. Matlab generates only in the fraction format. So I have to do like that – Bharat Nov 18 '17 at 10:37
  • BTW, you can't get integer overflow in Python, since Python integers grow as required. You're getting _float_ overflow, as that error message states. – PM 2Ring Nov 18 '17 at 10:38
  • @PM2Ring thanks for suggesting me the fractions module. I would use that – Bharat Nov 18 '17 at 10:43
  • The `decimal` module is ok for simple arbitrary precision arithmetic, but it only provides the 4 basic arithmetic functions, plus square roots. If you need more advanced functions, please take a look at the 3rd-party `mpmath` module: it has built-in [Bernoulli number](http://mpmath.org/doc/current/functions/numtheory.html?bernoulli-numbers-and-polynomials#bernoulli-numbers-and-polynomials) functions. – PM 2Ring Nov 18 '17 at 10:48
  • Ohh thanks man but the thing is I'm using these bernoulli numbers to solve a problem in hackerrank which doesn't support mpmath. So I have to rely on this – Bharat Nov 18 '17 at 10:50

1 Answers1

0

You should convert the large number to Decimal before doing the division, i.e.:

(Note the end of brackets)

di = {260: d(-5218507479961513801890596392421261361036935624312258325065379143295948300812040703848766095836974598734762472300638625802884257082786883956679824964010841565051175167717451747328911935282639583972372470105587187736495055501208701522099921363239317373617854217050435670713936357978555246779460902210809009009539232173) / 2291190}
Foxan Ng
  • 6,883
  • 4
  • 34
  • 41