lua实现大数运算
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实现大数运算的更多相关文章
- 大数运算(python2)
偶然又遇到了一道大数题,据说python大数运算好屌,试了一发,果然方便-1 a = int( raw_input() ); //注意这里是按行读入的,即每行只读一个数 b = int( raw_in ...
- 收藏的一段关于java大数运算的代码
收藏的一段关于java大数运算的代码: package study_02.number; import java.math.BigDecimal; import java.math.BigIntege ...
- [PKU2389]Bull Math (大数运算)
Description Bulls are so much better at math than the cows. They can multiply huge integers together ...
- java 大数运算[转]
用JAVA 实现算术表达式(1234324234324 + 8938459043545)/5 + 343434343432.59845 因为JAVA语言中的long 定义的变量值的最大数受到限制,例如 ...
- A+B大数运算
基础加法大数运算: [https://vjudge.net/problem/HDU-1002] 题目: 输入两个长度不超过1000的整数求出sum. 思路: 由于数字很大不能直接加,用字符串形式输入, ...
- HOJ 2148&POJ 2680(DP递推,加大数运算)
Computer Transformation Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4561 Accepted: 17 ...
- 九度OJ 1051:数字阶梯求和 (大数运算)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6718 解决:2280 题目描述: 给定a和n,计算a+aa+aaa+a...a(n个a)的和. 输入: 测试数据有多组,输入a,n(1&l ...
- 九度OJ 1119:Integer Inquiry(整数相加) (大数运算)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:679 解决:357 题目描述: One of the first users of BIT's new supercomputer was ...
- 大数运算之 Java BigInteger 的基本用法
大数运算之 Java BigInteger 的基本用法 在程序设计竞赛中会遇到高精度运算的问题,C++没有高精度运算,只能手动模拟人工运算,手动实现高精度,而 java.math 包中的 BigInt ...
随机推荐
- Maven系列--web.xml 配置详解
一 .web.xml介绍 启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点. 紧接着,容 ...
- DQL命令(查询)
select *或字段1,字段2... from 表名 [where 条件] 提示:*符号表示取表中所有列:没有where语句表示 查询表中所有记录:有wh ...
- Docker可视化管理工具对比(DockerUI、Shipyard、Rancher、Portainer)
1.前言 谈及docker,避免不了需要熟练的记住好多命令及其用法,对于熟悉shell.技术开发人员而言,还是可以接受的,熟练之后,命令行毕竟是很方便的,便于操作及脚本化.但对于命令行过敏.非技术人员 ...
- ASP.NET-Session cooike
Application .Cookie和 Session 两种会话有什么不同 答:Application是用来存取整个网站全局的信息,而Session是用来存取与具体某个访问者关联的信息, Sessi ...
- Android native CursorWindow数据保存原理
我们通过Uri查询数据库所得到的数据集,保存在native层的CursorWindow中.CursorWindow的实质是共享内存的抽象,以实现跨进程数据共享.共享内存所採用的实现方式是文件映射. 在 ...
- Executors线程池关闭时间计算
Executors线程池关闭时间计算 学习了:http://blog.csdn.net/wo541075754/article/details/51564359 https://www.cnblogs ...
- bzoj3626【LNOI2014】LCA
3626: [LNOI2014]LCA Time Limit: 10 Sec Memory Limit: 128 MB Submit: 1266 Solved: 448 [Submit][Stat ...
- zzulioj--1711--漂洋过海来看你(dfs+vector)
1711: 漂洋过海来看你 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 89 Solved: 33 SubmitStatusWeb Board D ...
- 编程语言与Python学习(一)
1.1 编程与编程语言 1.1.1 编程语言 计算机的发明,是为了用机器解放人力,而编程的目的则是将人类的思想流程按照某种能够被计算机识别的表达方式传递给计算机,从而达到让计算机能够像人脑一样自动执行 ...
- CentOS 安装 MySQL8
@Linux 官网:https://dev.mysql.com/doc/refman/8.0/en/binary-installation.html 个人博客:https://www.xingchen ...