0

enter image description here

--t.lua
function fact(n)
    if n == 0 then
        return 1
    else
        return n * fact(n-1)
    end
end

for i=1,100,1 do
    print(i,fact(i))
end
# t.py
fact = lambda n:1 if n == 0 else n * fact(n-1)

for i in range(1, 100):
    print(i, fact(i))

When I write a factorial code in Lua and in Python, I found that output was different.

JJJ
  • 32,902
  • 20
  • 89
  • 102
hys
  • 73
  • 5
  • 4
    Please copy-paste the outputs (and the expected output) to the question as text. Those screenshots are illegible. – JJJ Sep 20 '17 at 09:16

3 Answers3

8

Lua as usually configured uses your platform's usual double-precision floating point format to store all numbers (this means all number types). For most desktop platforms today, that will be the 64-bit IEEE-754 format. The conventional wisdom is that integers in the range -1E15 to +1E15 can be safely assumed to be represented exactly. To deal with huge numbers in Lua, key words are "bignum" and "arbitrary precision numbers". You can use pure-Lua modules. for example (bignum and lua-nums) and C-based module lmapm. Also read this thread.

Python supports a such-known "bignum" integer type which can work with arbitrarily large numbers. In Python 2.5+, this type is called long and is separate from the int type, but the interpreter will automatically use whichever is more appropriate. In Python 3.0+, the int type has been dropped completely. In Python usually you don't need to use special tools to deal with huge numbers.

Dmitry
  • 2,026
  • 1
  • 18
  • 22
  • Slightly inexact: _"Lua […uses floats…] to store all numbers […]."_ – Since 5.3, Lua uses both floats _and integers_, converting as needed for operations (and if you click on OP's picture, you see the signed integers wrapping around). (That doesn't really help with this particular problem as it only moves the boundary from 2^53 to 2^64, but it's valuable in other situations.) – nobody Sep 20 '17 at 21:17
0

You have an overflow on your first image because values are to big to be stored on that var.

VMRuiz
  • 1,931
  • 12
  • 20
0

This is basic example with lbn library library

local bn = require "bn"

function bn_fact(n)
  if n:tonumber() == 0 then return 1 end
  return n * bn_fact(n-1)
end

function fact(n)
  return bn_fact(bn.number(n))
end

for i=1,100,1 do
    print(i,fact(i))
end

Output for some values

30  265252859812191058636308480000000
31  8222838654177922817725562880000000
32  263130836933693530167218012160000000
33  8683317618811886495518194401280000000
moteus
  • 2,187
  • 1
  • 13
  • 16
  • module 'bn' not found:, how to use bn library?what i should do? – hys Sep 20 '17 at 09:55
  • Add link to library. But you can use any other big number libraries. – moteus Sep 20 '17 at 10:03
  • 1
    @moteus - Why on earth do you believe that a Lua programmer must be a C programmer and must be able to build C library for his platform? OP may be a Lua/C newbie, and building external C library may be not a simple task. And not all Lua environments allow you to use external libraries. – Egor Skriptunoff Sep 20 '17 at 10:25