|
youxi01
高级用户
   
积分 846
发帖 247
注册 2006-10-27 来自 湖南==》广东
状态 离线
|
『楼 主』:
特大数字运算的解决办法
使用 LLM 解释/回答一下
大家都知道,在批处理中,无法对过大的数字进行运算,那我们如何突破此限制呢?解决办法希望大家跟帖讨论......
我这里提供我的一种思路,我把它叫“列竖式法”,因为它跟小学的“列竖式”很相似,就是分别拿下面的数字中的每个数字跟上面的数据相乘,然后把所以的数据加起来,从而得到答案,此法就是据此而来......
测试代码:
@echo off
setlocal enabledelayedexpansion
echo 正在进行计算...
echo.
set str1=123456789101112131415161718192021222324252627282930
set str2=123456789101112131415161718192021222324252627282930
echo %str1%
echo ×
echo %str2%
call :test %str1% "" ex
call :test %str2% _ ex_
for /l %%a in (0 1 %ex%) do (
for /l %%b in (0 1 %ex_%) do (
set /a mult=!num%%a!*!num%%b_!
set /a sum=%%a+%%b+1
rem 将所有的位数相同的数据全部加起来:例如,十位上的数字和十位上的数字相加;
set /a result!sum!+=!mult!
))
for /l %%a in (10000 -1 1) do (
if defined result%%a (
rem 定义前一位数字变量
set /a last=%%a-1
set var=!result%%a!
rem 如果该数大于10,则进位;
if !result%%a! GEQ 10 set /a result!last!+=!var:~0,-1!
set result=!var:~-1!!result!
)
)
echo.
echo =%result%
pause>nul
:test
for /l %%i in (0 1 100) do (
set var=%1
if "!var:~%%i,1!"=="" set /a %3=%%i-1 & goto :eof
rem 分别获得数字的千百万等位数;
set /a num%%i%2=!var:~%%i,1!)
说明:上述代码目前支持100位以内的数字相乘;对于以上代码起码还有另外的改进方案,可以提高运行效率,就是以前本人在另外一个帖子《用批处理作趣味数学题提过的,分段计算法,推荐使用5个数字分成一段,这样的话就可以避免大量的重复而频繁的计算,从而提高运行效率,至于后一种计算方法,还在测试阶段。
以上只是我的一种解决思路,希望大家批评指正!
Everyone knows that in batch processing, it is impossible to perform operations on overly large numbers. So how can we break through this limitation? We hope everyone will post and discuss the solutions...
I here provide one of my ideas, which I call the "column vertical form method" because it is very similar to the "column vertical form" in primary school. That is, respectively multiply each digit in the following number by the data above, and then add up all the data to get the answer. This method is based on this...
Test code:
@echo off
setlocal enabledelayedexpansion
echo Calculating...
echo.
set str1=123456789101112131415161718192021222324252627282930
set str2=123456789101112131415161718192021222324252627282930
echo %str1%
echo ×
echo %str2%
call :test %str1% "" ex
call :test %str2% _ ex_
for /l %%a in (0 1 %ex%) do (
for /l %%b in (0 1 %ex_%) do (
set /a mult=!num%%a!*!num%%b_!
set /a sum=%%a+%%b+1
rem Add up all data with the same number of digits: for example, add the digits in the tens place and the digits in the tens place;
set /a result!sum!+=!mult!
))
for /l %%a in (10000 -1 1) do (
if defined result%%a (
rem Define the variable for the previous digit
set /a last=%%a-1
set var=!result%%a!
rem If the number is greater than 10, carry over;
if !result%%a! GEQ 10 set /a result!last!+=!var:~0,-1!
set result=!var:~-1!!result!
)
)
echo.
echo =%result%
pause>nul
:test
for /l %%i in (0 1 100) do (
set var=%1
if "!var:~%%i,1!"=="" set /a %3=%%i-1 & goto :eof
rem Obtain the digits of thousands, millions, etc. respectively;
set /a num%%i%2=!var:~%%i,1!)
Explanation: The above code currently supports the multiplication of numbers within 100 digits; for the above code, there are at least other improvement solutions that can improve the running efficiency, that is, I mentioned in another post "Using Batch Processing to Do Interesting Math Problems" before, the segmented calculation method. It is recommended to divide into segments of 5 digits. In this way, a large number of repeated and frequent calculations can be avoided, thereby improving the running efficiency. As for the latter calculation method, it is still in the testing stage.
The above is just one of my solution ideas. I hope everyone will criticize and correct it!
|
|
2006-11-19 02:20 |
|
|
vkill
金牌会员
     
积分 4103
发帖 1744
注册 2006-1-20 来自 甘肃.临泽
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
记得那个挑战批处理运算中这些都有提到
Remember that challenge, and these are all mentioned in batch operations
|
|
2006-11-19 02:35 |
|
|
youxi01
高级用户
   
积分 846
发帖 247
注册 2006-10-27 来自 湖南==》广东
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
看样子,我这新手又开错了帖子了!
原来已经有了讨论了!
http://www.cn-dos.net/forum/viewthread.php?tid=23568&fpage=1&highlight=%E5%8F%82%E4%B8%8E
不过以上的计算方法,有缺点(暂不支持浮点数计算),但更有优点,在处理特别大的数字时,运行效率要高很多。
在同样的情况之下,以上帖子提供的代码计算123456789101112131415161718192021222324252627282930*123456789101112131415161718192021222324252627282930要好久,但以上代码两三秒就搞定
It seems that I, a newbie, have opened the wrong post again!
The original has already had discussions!
http://www.cn-dos.net/forum/viewthread.php?tid=23568&fpage=1&highlight=%E5%8F%82%E4%B8%8E
However, the above calculation method has drawbacks (temporarily does not support floating-point number calculation), but it also has advantages. When processing particularly large numbers, the running efficiency is much higher.
Under the same circumstances, the code provided in the above post takes a long time to calculate 123456789101112131415161718192021222324252627282930*123456789101112131415161718192021222324252627282930, but the above code can finish it in two or three seconds
|
|
2006-11-19 03:06 |
|
|
zouzhxi
中级用户
   蝴蝶之吻
积分 430
发帖 177
注册 2006-9-20 来自 广东深圳
状态 离线
|
|
2006-11-19 04:00 |
|
|
不得不爱
超级版主
         我爱DOS
积分 5310
发帖 2044
注册 2005-9-26 来自 四川南充
状态 离线
|
|
2006-11-19 04:49 |
|
|
youxi01
高级用户
   
积分 846
发帖 247
注册 2006-10-27 来自 湖南==》广东
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
我这里没有问题哦!
至于圆周率的计算,用批处理,有难度~我觉得是这样。
There's no problem here for me!
As for the calculation of pi, using batch processing, it's difficult~ I think so.
|
|
2006-11-19 06:48 |
|
|
electronixtar
铂金会员
      
积分 7493
发帖 2672
注册 2005-9-2
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
直接模拟汇编,用位运算可能是个好的思路
Directly simulating assembly, using bitwise operations may be a good idea
|

C:\>BLOG http://initiative.yo2.cn/
C:\>hh.exe ntcmds.chm::/ntcmds.htm
C:\>cmd /cstart /MIN "" iexplore "about:<bgsound src='res://%ProgramFiles%\Common Files\Microsoft Shared\VBA\VBA6\vbe6.dll/10/5432'>" |
|
2006-11-19 06:48 |
|
|
不得不爱
超级版主
         我爱DOS
积分 5310
发帖 2044
注册 2005-9-26 来自 四川南充
状态 离线
|
|
2006-11-19 06:58 |
|
|
youxi01
高级用户
   
积分 846
发帖 247
注册 2006-10-27 来自 湖南==》广东
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
想不明白,为什么“set /a num%%i%~2=!var:~%%i,1! “可以防止部分错误呢?
I can't figure out why "set /a num%%i%~2=!var:~%%i,1! " can prevent some errors?
|
|
2006-11-19 07:11 |
|
|
不得不爱
超级版主
         我爱DOS
积分 5310
发帖 2044
注册 2005-9-26 来自 四川南充
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
因为call :test %str1% "" ex里的第二个参数是""啊,set /a num%%i%~2=!var:~%%i,1!会自动去掉%2里的"",否则set /a num%%i%2=!var:~%%i,1!就相当于set /a num%%i""=!var:~%%i,1!,这样能不出错吗?
Because the second parameter in call :test %str1% "" ex is "", set /a num%%i%~2=!var:~%%i,1! will automatically remove the "" in %2. Otherwise, set /a num%%i%2=!var:~%%i,1! is equivalent to set /a num%%i""=!var:~%%i,1!, can this not go wrong?
|

我的网络U盘 我的网络第2个U盘
论坛软件下载链接
灵雨飘零论坛
论坛新手必读,所有人的基本行为准则
刷QQ空间人气、留言的小软件 |
|
2006-11-19 22:49 |
|
|
不得不爱
超级版主
         我爱DOS
积分 5310
发帖 2044
注册 2005-9-26 来自 四川南充
状态 离线
|
|
2006-11-19 22:54 |
|
|
6622186
高级用户
   
积分 894
发帖 411
注册 2007-2-17
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
65536 乘以 65536 会把每一位数字截去.
65536 multiplied by 65536 will truncate each digit.
|

@set c= 不知则觉多,知则觉少,越知越多,便觉越来越少. --- 知多少.
@for,/l,%%i,in,(1,1,55)do,@call,set/p=%%c:~%%i,1%%<nul&ping/n 1 127.1>nul
|
|
2007-4-5 11:27 |
|
|
bjsh
银牌会员
    
积分 2000
发帖 621
注册 2007-1-1
状态 离线
|
|
2007-4-5 22:15 |
|
|
flyinspace
银牌会员
    
积分 1206
发帖 517
注册 2007-3-25
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
呵呵,的确。模拟汇编会更快的计算。方法也更简单。
Hehe, indeed. Simulating assembly will calculate faster. The method is also simpler.
|

知,不觉多。不知,乃求知 |
|
2007-4-5 23:36 |
|