Julia: Product of Factorials - Wrong value computed by Julia

Created on 5 Jul 2017  路  7Comments  路  Source: JuliaLang/julia

Dear Sir,

Julia is computing wrong answer for the following:
1! x 2! x 3! x 4! x 5! x 6! x 7! x 8! x 9!

$ julia
               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: http://docs.julialang.org
   _ _   _| |_  __ _   |  Type "help()" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.3.11 (2015-07-27 06:18 UTC)
 _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org release
|__/                   |  x86_64-linux-gnu

julia> ## compute :: 1! x 2! x 3! x 4! x 5! x 6! x 7! x 8! x 9!

julia> 1*2*6*24*120*720*5040*40320*362880
8705808953839190016

julia> ## this is the WRONG answer

julia>

Now, let us run this in Python to see the correct answer ::

$ python
Python 2.7.10 |Anaconda 2.3.0 (64-bit)| (default, May 28 2015, 17:02:03) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
>>>  ## compute :: 1! x 2! x 3! x 4! x 5! x 6! x 7! x 8! x 9!
... 
>>> 1*2*6*24*120*720*5040*40320*362880
1834933472251084800000L
>>> ## this is the CORRECT answer
... 
>>> 

Debugging:
To debug this problem, let us try to see after which product of factorial, julia has started to output the wrong answer. We can see that from the computation of product of the first nine factorials ( 1! x 2! x 3! x 4! x 5! x 6! x 7! x 8! x 9! ) - Julia Language has started to compute the wrong answer. It is evident from the below demo. Please correct this problem.

$ julia
               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: http://docs.julialang.org
   _ _   _| |_  __ _   |  Type "help()" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.3.11 (2015-07-27 06:18 UTC)
 _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org release
|__/                   |  x86_64-linux-gnu

julia> ###########################################
       ## Writing a program to print - product of factorials of numbers less than 10. That is
       ##  1! x 2! 
       ##  1! x 2! x 3! 
       ##  1! x 2! x 3! x 4!
       ##  1! x 2! x 3! x 4! x 5! 
       ##  1! x 2! x 3! x 4! x 5! x 6! 
       ##  1! x 2! x 3! x 4! x 5! x 6! x 7!
       ##  1! x 2! x 3! x 4! x 5! x 6! x 7! x 8! 
       ##  1! x 2! x 3! x 4! x 5! x 6! x 7! x 8! x 9!
       ###########################################

       prodf = 1
1

julia> for i = 1:9
         fact = 1
         for j = 1:i
           fact = fact*j
         end # for j
         print("\n"*string(i)*"! = "*string(fact)*"\n")
         prodf = prodf*fact
         for j = 1:i
           print(string(j)*"! x ")
         end # for j
         print("1 = "*string(prodf)*"\n")
       end # for i

1! = 1
1! x 1 = 1

2! = 2
1! x 2! x 1 = 2

3! = 6
1! x 2! x 3! x 1 = 12

4! = 24
1! x 2! x 3! x 4! x 1 = 288

5! = 120
1! x 2! x 3! x 4! x 5! x 1 = 34560

6! = 720
1! x 2! x 3! x 4! x 5! x 6! x 1 = 24883200

7! = 5040
1! x 2! x 3! x 4! x 5! x 6! x 7! x 1 = 125411328000

8! = 40320
1! x 2! x 3! x 4! x 5! x 6! x 7! x 8! x 1 = 5056584744960000

9! = 362880
1! x 2! x 3! x 4! x 5! x 6! x 7! x 8! x 9! x 1 = 8705808953839190016

julia> 

Requesting to provide correction to the problem ASAP..

Most helpful comment

Doing this is essentially converting the incorrect/overflow Int value to a BigInt:

Julia-0.6.0> big(1*2*6*24*120*720*5040*40320*362880)
8705808953839190016

Rather, what you want is to do the calculations with BigInts:

Julia-0.6.0> big(1)*2*6*24*120*720*5040*40320*362880
1834933472251084800000

Here, each value in the product is promoted to BigInt before multiplication.

All 7 comments

Also, I have tried this on the latest stable release (0.6.0). Wrong answer received again.

$ julia
               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: https://docs.julialang.org
   _ _   _| |_  __ _   |  Type "?help" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.6.0 (2017-06-19 13:05 UTC)
 _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org/ release
|__/                   |  x86_64-pc-linux-gnu

julia> ## compute :: 1! x 2! x 3! x 4! x 5! x 6! x 7! x 8! x 9!

julia> 1*2*6*24*120*720*5040*40320*362880
8705808953839190016

julia> ## again, this is the WRONG answer

julia> 

I tried this on Python. Python has computed the right answer for this problem:

$ python
Python 2.7.10 |Anaconda 2.3.0 (64-bit)| (default, May 28 2015, 17:02:03) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
>>> ## compute :: 1! x 2! x 3! x 4! x 5! x 6! x 7! x 8! x 9!
... 
>>> 1*2*6*24*120*720*5040*40320*362880
1834933472251084800000L
>>> ## this is the RIGHT answer
... 
>>> 

Essentially dup of #22539.

See the manual on integer overflow: https://docs.julialang.org/en/stable/manual/integers-and-floating-point-numbers/#Overflow-behavior-1

Also see https://docs.julialang.org/en/stable/manual/faq/#Why-does-Julia-use-native-machine-integer-arithmetic?-1 for more explanation.

Dear Sir,

I have referred to #22539 and followed the Instructions in it. and I am still receiving wrong answer from Julia

$ julia
               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: https://docs.julialang.org
   _ _   _| |_  __ _   |  Type "?help" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.6.0 (2017-06-19 13:05 UTC)
 _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org/ release
|__/                   |  x86_64-pc-linux-gnu

julia> ## compute :: 1! x 2! x 3! x 4! x 5! x 6! x 7! x 8! x 9!

julia> big(1*2*6*24*120*720*5040*40320*362880)
8705808953839190016

julia> BigInt(1*2*6*24*120*720*5040*40320*362880)
8705808953839190016

julia> ## this is the WRONG answer - correct answer is 1834933472251084800000

julia> myBigVar = big(1*2*6*24*120*720*5040*40320*362880)
8705808953839190016

julia> myBigVar = BigInt(1*2*6*24*120*720*5040*40320*362880)
8705808953839190016

julia> myBigVar
8705808953839190016

julia> ## this is the WRONG answer - correct answer is 1834933472251084800000

julia> typeof(myBigVar)
BigInt


Please use Discourse for usage questions.

Doing this is essentially converting the incorrect/overflow Int value to a BigInt:

Julia-0.6.0> big(1*2*6*24*120*720*5040*40320*362880)
8705808953839190016

Rather, what you want is to do the calculations with BigInts:

Julia-0.6.0> big(1)*2*6*24*120*720*5040*40320*362880
1834933472251084800000

Here, each value in the product is promoted to BigInt before multiplication.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dpsanders picture dpsanders  路  3Comments

tkoolen picture tkoolen  路  3Comments

iamed2 picture iamed2  路  3Comments

wilburtownsend picture wilburtownsend  路  3Comments

sbromberger picture sbromberger  路  3Comments