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 ...
随机推荐
- jvm 堆、栈 、方法区概念和联系
一.三者联系 1.堆:解决数据的存储问题( 即 数据怎么放,放到哪 ). 2.栈:解决程序运行的问题( 即 程序如何执行,或者说如何处理数据 ). 3.方法区:辅助堆栈的一块永久区,解决堆栈信息的产生 ...
- 2015 Multi-University Training Contest 4 hdu 5334 Virtual Participation
Virtual Participation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Ot ...
- SQL SERVER-集合操作
内连接 INNER JOIN(等值连接):只显示两个表中联结字段相等的行.这个和用select查询多表是一样的效果,所以很少用到:外连接:LEFT JOIN :以左表为基础,显示左表中的所有 ...
- Spring中 @Autowired标签与 @Resource标签 的区别(转)
spring不但支持自己定义的@Autowired注解,还支持由JSR-250规范定义的几个注解,如:@Resource. @PostConstruct及@PreDestroy. 1. @Autowi ...
- HDU 4320 Contest 3
只需A的全部质因数包含在B中即可. #include <iostream> #include <cstdio> #define LL __int64 #include < ...
- HTML5中x-webkit-speech语音输入功能
如今各大站点都在搜索框中增加了语音输入功能,只是眼下仅仅有Chrome 11及以上版本号才支持. 能够用chrome浏览器在这里试试效果,点击话筒就可以: 实现起来也很easy,为input加入名为 ...
- ORA 12505 Listener does not currently know of SID given in connection descriptor
oracle数据库正常启动后.在本地能够正常訪问,可是远程使用sqldevelop却不能訪问.提示ORA 12505 Listener does not currently know of SID g ...
- 关于输入getline
此函数可读取整行,包括前导和嵌入的空格,并将其存储在字符串对象中. getline 函数如下所示: getline(cin, inputLine); 其中 cin 是正在读取的输入流,而 inputL ...
- python网页问题
#django-admin不是个命令 添加环境变量 D:\Python36\Scripts #localhost加载失败 命令行 python manage.py runserver 0.0.0.0: ...
- (转载) 清理缓存 IPackageStatsObserver
清理缓存 IPackageStatsObserver 2016-04-10 13:40 2288人阅读 评论(0) 收藏 举报 分类: android(59) 版权声明:本文为博主原创文章,未经博 ...