lua实现的大数运算,代码超短,眼下仅仅实现的加减乘运算

------------------------------------------------
--name: bigInt
--create: 2015-4-1
--author: 闲云
--blog: blog.csdn.net/xianyun2009
--QQ: 836663997
--QQ group: 362337463
------------------------------------------------
local mod = 10000 function show(a)
print(get(a))
end
function get(a)
s = {a[#a]}
for i=#a-1, 1, -1 do
table.insert(s, string.format("%04d", a[i]))
end
return table.concat(s, "")
end
function create(s)
if s["xyBitInt"] == true then return s end
n, t, a = math.floor(#s/4), 1, {}
a["xyBitInt"] = true
if #s%4 ~= 0 then a[n + 1], t = tonumber(string.sub(s, 1, #s%4), 10), #s%4 + 1 end
for i = n, 1, -1 do a[i], t= tonumber(string.sub(s, t, t + 3), 10), t + 4 end
return a
end
function add(a, b)
a, b, c, t = create(a), create(b), create("0"), 0
for i = 1, math.max(#a,#b) do
t = t + (a[i] or 0) + (b[i] or 0)
c[i], t = t%mod, math.floor(t/mod)
end
while t ~= 0 do c[#c + 1], t = t%mod, math.floor(t/mod) end
return c
end
function sub(a, b)
a, b, c, t = create(a), create(b), create("0"), 0
for i = 1, #a do
c[i] = a[i] - t - (b[i] or 0)
if c[i] < 0 then t, c[i] = 1, c[i] + mod else t = 0 end
end
return c
end
function by(a, b)
a, b, c, t = create(a), create(b), create("0"), 0
for i = 1, #a do
for j = 1, #b do
t = t + (c[i + j - 1] or 0) + a[i] * b[j]
c[i + j - 1], t = t%mod, math.floor(t / mod)
end
if t ~= 0 then c[i + #b], t = t + (c[i + #b] or 0), 0 end
end
return c
end

把以上代码保存到文件  bigInt.lua

演示样例:

新建文件 example.lua 内容例如以下:

require("bigInt")

show(add("987654321", "123456789"))
show(sub("987654321", "123456789"))
show(by("987654321", "123456789"))

以后用到的地方就能够直接这样简单的调用

lua实现大数运算的更多相关文章

  1. 大数运算(python2)

    偶然又遇到了一道大数题,据说python大数运算好屌,试了一发,果然方便-1 a = int( raw_input() ); //注意这里是按行读入的,即每行只读一个数 b = int( raw_in ...

  2. 收藏的一段关于java大数运算的代码

    收藏的一段关于java大数运算的代码: package study_02.number; import java.math.BigDecimal; import java.math.BigIntege ...

  3. [PKU2389]Bull Math (大数运算)

    Description Bulls are so much better at math than the cows. They can multiply huge integers together ...

  4. java 大数运算[转]

    用JAVA 实现算术表达式(1234324234324 + 8938459043545)/5 + 343434343432.59845 因为JAVA语言中的long 定义的变量值的最大数受到限制,例如 ...

  5. A+B大数运算

    基础加法大数运算: [https://vjudge.net/problem/HDU-1002] 题目: 输入两个长度不超过1000的整数求出sum. 思路: 由于数字很大不能直接加,用字符串形式输入, ...

  6. HOJ 2148&POJ 2680(DP递推,加大数运算)

    Computer Transformation Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4561 Accepted: 17 ...

  7. 九度OJ 1051:数字阶梯求和 (大数运算)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6718 解决:2280 题目描述: 给定a和n,计算a+aa+aaa+a...a(n个a)的和. 输入: 测试数据有多组,输入a,n(1&l ...

  8. 九度OJ 1119:Integer Inquiry(整数相加) (大数运算)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:679 解决:357 题目描述: One of the first users of BIT's new supercomputer was ...

  9. 大数运算之 Java BigInteger 的基本用法

    大数运算之 Java BigInteger 的基本用法 在程序设计竞赛中会遇到高精度运算的问题,C++没有高精度运算,只能手动模拟人工运算,手动实现高精度,而 java.math 包中的 BigInt ...

随机推荐

  1. Linux学习总结(15)——提高 Vim 和 Shell 效率的 9 个建议

    你上一次使用 CAPSLOCK 键是什么时候?很久没有了对不对?噢,我也是,它已经被遗忘了,它浪费了键盘上一个黄金位置.让我们把它重映射成 Control 键来发挥它的作用吧!这里告诉了你在不同的操作 ...

  2. like

    5.在WHERE中使用like做模糊查询    %符号表示0到多个任意字符    _符号表示1个任意字符     //查询名字中含有O字符的员工信息   select empno,ename   fr ...

  3. ASP.NET-关于Global.asax的作用

    这个文件相当于一个应用程序量级的 全局文件,比如你想写一个变量在项目中的所有文件中都能读取是就写在这里面 Application["name"] = "zhangran& ...

  4. 【网络协议】TCP协议简单介绍

        本文仅仅是对TCP协议做个简要的介绍.     TCP协议,即传输控制协议.与UDP协议同处于传输层,相同使用相同的网络层,但TCP提供了一种可靠的.面向连接的传输数据服务,它会在两个使用TC ...

  5. hello world to php( mac 配置 xmapp virtual host)

    一.安装xmapp.安装完以后查看,服务是否都能启动(数据库和server) 二.配置自己的virtualhost       1.系统host文件加入server的域名(在浏览器中输入域名后会先通过 ...

  6. poj3249 Test for job 【图的DAG dp】

    #include <cstdio> #include <cstdlib> #include <iostream> #include <algorithm> ...

  7. dom 编程(html和xml)

    html dom与xml dom关系: 什么是 DOM? DOM 是 W3C(万维网联盟)的标准. DOM 定义了訪问 HTML 和 XML 文档的标准: "W3C 文档对象模型 (DOM) ...

  8. myeclipse配置内存

    1.javaee项目假设耗费的内存过大,须要配置内存大小: 下图是配置tomcat结果:Optional program arguments: -Xms512M -Xmx512M -XX:PermSi ...

  9. 12.boost有向图无向图(矩阵法)

    #include <iostream> #include <boost/config.hpp> //图 #include <boost/graph/adjacency_m ...

  10. oracle 11g rac for linux add node (oracle 11g rac 节点添加)

    说明: Adding Oracle RAC to Nodes with Oracle Clusterware Installed步骤来自ORACLE 官方文档: https://docs.oracle ...