|
qzwqzw
银牌会员
     天的白色影子
积分 2343
发帖 636
注册 2004-3-6
状态 离线
|
  『楼 主』:
[讨论]寻找大素数-32位正整数的素性判定
使用 LLM 解释/回答一下
此帖的由来
http://www.cn-dos.net/forum/viewthread.php?tid=27044
-----------------------------------------------------------------------------------------------
以下代码完全照搬了以下链接中的判定算法——米勒拉宾检验+二次检验
http://blog.csdn.net/bsrw/archive/2006/11/28/1419145.aspx
-----------------------------------------------------------------------------------------------
遗憾的是其中的二次检测会导致大素数判定时数据溢出
测试结果从46400开始大量出现因为数据溢出而导致的漏检
1000以内测试没有漏检
1000~46400 没有测试
@echo off
setlocal EnableDelayedExpansion
set time0=%time%
for /l %%i in (46001,2,48000) do (
set /a testnum=%%i
call :JudgePrime !testnum!
if !errorlevel! equ 1 set /p=!testnum! <nul & set /a iprime+=1
)
echo.
echo.
echo begin: %time0%
echo finish: %time%
echo found: %iprime%
pause
goto :eof
:JudgePrime
if == exit /b 2
set /a tmp1=%1
if not %tmp1%==%1 exit /b 2
if %1 lss 2 exit /b 0
if %1 equ 2 exit /b 1
set i=0
for %%i in (2,3,5,7,11) do (
set prime_!i!=%%i
set /a prime6p_!i!=%%i*%%i*%%i
set /a prime6p_!i!*=prime6p_!i!
set /a i+=1
)
set i=0
:loop1_JP
call set prime_i=%%prime_%i%%%
call set prime6p_i=%%prime6p_%i%%%
if %1 geq %prime6p_3% (
if !prime_i! equ 3 (
set /a i+=1
goto :loop1_JP
)
) else (
if !prime_i! neq 2 if %1 lss !prime6p_i! exit /b 1
)
call :LikePrime %1 %prime_i%
if not errorlevel 1 exit /b 0
set /a i+=1
if %i% lss 5 goto :loop1_JP
exit /b 1
goto :eof
:LikePrime
set /a x=result=1, tmp1=%1-1, bits=0
:loop1_LP
set /a bits+=1
set /a "tmp1>>=1"
if %tmp1% gtr 0 goto :loop1_LP
set /a tmp1=%1-1
:loop2_LP
set /a bits-=1
set /a result=(x*x) %% %1 %=此句代码判断导致大素数时数据溢出=%
if %result% equ 1 if %x% neq 1 if %x% neq %tmp1% exit /b 0
set /a "tmp2=%tmp1% & (1 << %bits%)"
if %tmp2% neq 0 set /a result=(result*%2) %% %1
set x=%result%
if %bits% gtr 0 goto :loop2_LP
if %result% equ 1 exit /b 1
goto :eof
Last edited by qzwqzw on 2007-1-28 at 11:34 AM ]
Origin of this post
http://www.cn-dos.net/forum/viewthread.php?tid=27044
-----------------------------------------------------------------------------------------------
The following code completely copies the judgment algorithm in the following link - Miller-Rabin test + quadratic test
http://blog.csdn.net/bsrw/archive/2006/11/28/1419145.aspx
-----------------------------------------------------------------------------------------------
Unfortunately, the quadratic test will cause data overflow in large prime number judgment
The test results start from 46400 and a large number of missed detections due to data overflow occur
No missed detection in the test within 1000
No test from 1000 to 46400
@echo off
setlocal EnableDelayedExpansion
set time0=%time%
for /l %%i in (46001,2,48000) do (
set /a testnum=%%i
call :JudgePrime !testnum!
if !errorlevel! equ 1 set /p=!testnum! <nul & set /a iprime+=1
)
echo.
echo.
echo begin: %time0%
echo finish: %time%
echo found: %iprime%
pause
goto :eof
:JudgePrime
if == exit /b 2
set /a tmp1=%1
if not %tmp1%==%1 exit /b 2
if %1 lss 2 exit /b 0
if %1 equ 2 exit /b 1
set i=0
for %%i in (2,3,5,7,11) do (
set prime_!i!=%%i
set /a prime6p_!i!=%%i*%%i*%%i
set /a prime6p_!i!*=prime6p_!i!
set /a i+=1
)
set i=0
:loop1_JP
call set prime_i=%%prime_%i%%%
call set prime6p_i=%%prime6p_%i%%%
if %1 geq %prime6p_3% (
if !prime_i! equ 3 (
set /a i+=1
goto :loop1_JP
)
) else (
if !prime_i! neq 2 if %1 lss !prime6p_i! exit /b 1
)
call :LikePrime %1 %prime_i%
if not errorlevel 1 exit /b 0
set /a i+=1
if %i% lss 5 goto :loop1_JP
exit /b 1
goto :eof
:LikePrime
set /a x=result=1, tmp1=%1-1, bits=0
:loop1_LP
set /a bits+=1
set /a "tmp1>>=1"
if %tmp1% gtr 0 goto :loop1_LP
set /a tmp1=%1-1
:loop2_LP
set /a bits-=1
set /a result=(x*x) %% %1 %=This code judgment causes data overflow in large primes=%
if %result% equ 1 if %x% neq 1 if %x% neq %tmp1% exit /b 0
set /a "tmp2=%tmp1% & (1 << %bits%)"
if %tmp2% neq 0 set /a result=(result*%2) %% %1
set x=%result%
if %bits% gtr 0 goto :loop2_LP
if %result% equ 1 exit /b 1
goto :eof
Last edited by qzwqzw on 2007-1-28 at 11:34 AM ]
此帖被 +85 点积分 点击查看详情 评分人:【 无奈何 】 | 分数: +20 | 时间:2007-1-29 01:05 | 评分人:【 pengfei 】 | 分数: +15 | 时间:2007-1-29 02:31 | 评分人:【 Wengier 】 | 分数: +20 | 时间:2007-1-29 02:40 | 评分人:【 redtek 】 | 分数: +15 | 时间:2007-1-30 05:22 | 评分人:【 electronixtar 】 | 分数: +11 | 时间:2007-1-30 09:15 | 评分人:【 tao0610 】 | 分数: +4 | 时间:2007-1-30 12:06 |
|
|
|
2007-1-29 00:11 |
|
|
redtek
金牌会员
     
积分 2902
发帖 1147
注册 2006-9-21
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
顶~~欣赏~~~
Top~~Appreciate~~~
|

Redtek,一个永远在网上流浪的人……
_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._ |
|
2007-1-29 02:14 |
|
|
pengfei
银牌会员
    
积分 1218
发帖 485
注册 2006-7-21 来自 湖南.娄底
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
qzwqzw兄的代码精彩~~~ 支持!
由于批处理中set /a进行算术运算时可以存储的最大值为长整型(32 位), 正数为2147483647, 负数为-2147483648. 再多加或减1时都会溢出.
Brother qzwqzw's code is wonderful~~~ Support!
When performing arithmetic operations with set /a in batch processing, the maximum value that can be stored is a 32-bit signed integer, with the positive number being 2147483647 and the negative number being -2147483648. Adding or subtracting 1 more will cause an overflow.
|

业精于勤而荒于嬉,形成于思而毁于随。 |
|
2007-1-29 02:31 |
|
|
pengfei
银牌会员
    
积分 1218
发帖 485
注册 2006-7-21 来自 湖南.娄底
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
批处理进行大数判定时效率还是很慢, 可以单独判定一个数是否素数, 这样能更好地讨论一些算法的实现.
When performing large number judgment in batch processing, the efficiency is still very slow. We can individually judge whether a number is a prime number, which can better discuss the implementation of some algorithms.
|

业精于勤而荒于嬉,形成于思而毁于随。 |
|
2007-1-29 02:57 |
|
|
qzwqzw
银牌会员
     天的白色影子
积分 2343
发帖 636
注册 2004-3-6
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
实际上顶楼的代码实现的就是单个大素数的判定
代码中的循环只是用来判断其算法的有效性
而代码的效率可以通过从语法上的改善来提高
--------------------------------------------------
关于整型溢出的上下界是受cmd本身的限制
不仅仅是set/a而已
要想解决数据溢出的问题
一是寻找不使用二次检验的确定性判定算法
二是构建cmd处理64位整数的能力
这可能是我们讨论的主要方向
---------------------------------------------------
也许用64位Windows的CMD也可以解决
不知谁有测试环境可以测试一下
Actually, the code in the top post is actually implementing the determination of a single large prime number.
The loop in the code is just used to judge the effectiveness of its algorithm.
And the efficiency of the code can be improved through grammatical improvements.
--------------------------------------------------
The upper and lower bounds of integer overflow are limited by the cmd itself.
It's not just set/a.
To solve the problem of data overflow.
One is to find a deterministic determination algorithm that does not use quadratic testing.
The second is to build the ability of cmd to handle 64-bit integers.
This may be the main direction of our discussion.
---------------------------------------------------
Maybe the CMD of 64-bit Windows can also solve it.
I don't know who has a test environment to test it.
|
|
2007-1-29 03:39 |
|
|
pengfei
银牌会员
    
积分 1218
发帖 485
注册 2006-7-21 来自 湖南.娄底
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
建议兄做成一个让用户自己输入一个数, 再判断该数是否为素数.
此代码效率很差, 只是演示.
@echo off
setlocal enabledelayedexpansion
set /p num=请输入一个数:
echo 判断中, 请稍等...
set t=%time%
set /a sqrt=num/2,form=1
for /l %%i in (2 1 %sqrt%) do (
set /a x=!num! %% %%i
if "!x!"=="0" set form=0&goto show
)
:show
echo.
if "%form%"=="1" (echo %num%是素数.) else (echo %num%不是素数.)
echo 起始时间=%t%
echo 结束时间=%time%
pause
由于批处理中不能计算一个数的平方根, 这里用除2代替.
Last edited by pengfei on 2007-1-29 at 04:45 AM ]
Suggest the brother to make a program where the user enters a number and then judges whether the number is a prime number.
This code has very poor efficiency, just for demonstration.
@echo off
setlocal enabledelayedexpansion
set /p num=Please enter a number:
echo Judging, please wait...
set t=%time%
set /a sqrt=num/2,form=1
for /l %%i in (2 1 %sqrt%) do (
set /a x=!num! %% %%i
if "!x!"=="0" set form=0&goto show
)
:show
echo.
if "%form%"=="1" (echo %num% is a prime number.) else (echo %num% is not a prime number.)
echo Start time=%t%
echo End time=%time%
pause
Because the square root of a number cannot be calculated in batch processing, here it is replaced by dividing by 2.
Last edited by pengfei on 2007-1-29 at 04:45 AM ]
|

业精于勤而荒于嬉,形成于思而毁于随。 |
|
2007-1-29 04:37 |
|
|
qzwqzw
银牌会员
     天的白色影子
积分 2343
发帖 636
注册 2004-3-6
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
感觉用户输入没有什么大用
较小的数都已测试过
较大的数是素数的概率比较低
测试的效率很低
-------------------------------------
不过也好
就加上一个输入判断
如果输入i则使用内置测试集判定素数
否则进行单个素数的判定
@echo off
setlocal EnableDelayedExpansion
:loop_test
set testnum=
set /p testnum=请输入一个整数(按i使用内置测试集,直接按回车退出):
if "%testnum%"=="" goto :eof
if /i not "%testnum%"=="i" (
call :JudgePrime %testnum%
if errorlevel 2 (echo 无效输入:%testnum%
) else if errorlevel 1 (echo %testnum% 是素数
) else (echo %testnum% 是合数)
goto :loop_test
)
set time0=%time%
for /l %%i in (46001,2,48000) do (
set /a testnum=%%i
call :JudgePrime !testnum!
if !errorlevel! equ 1 set /p=!testnum! <nul & set /a iprime+=1
)
echo.
echo.
echo found: %iprime%
echo begin: %time0%
echo finish: %time%
pause
goto :eof
:JudgePrime
if == exit /b 2
set /a tmp1=%1
if not %tmp1%==%1 exit /b 2
if %1 lss 2 exit /b 0
if %1 equ 2 exit /b 1
set i=0
for %%i in (2,3,5,7,11) do (
set prime_!i!=%%i
set /a prime6p_!i!=%%i*%%i*%%i
set /a prime6p_!i!*=prime6p_!i!
set /a i+=1
)
set i=0
:loop1_JP
call set prime_i=%%prime_%i%%%
call set prime6p_i=%%prime6p_%i%%%
if %1 geq %prime6p_3% (
if !prime_i! equ 3 (
set /a i+=1
goto :loop1_JP
)
) else (
if !prime_i! neq 2 if %1 lss !prime6p_i! exit /b 1
)
call :LikePrime %1 %prime_i%
if not errorlevel 1 exit /b 0
set /a i+=1
if %i% lss 5 goto :loop1_JP
exit /b 1
goto :eof
:LikePrime
set /a x=result=1, tmp1=%1-1, bits=0
:loop1_LP
set /a bits+=1
set /a "tmp1>>=1"
if %tmp1% gtr 0 goto :loop1_LP
set /a tmp1=%1-1
:loop2_LP
set /a bits-=1
set /a result=(x*x) %% %1 %=此句代码判断导致大素数时数据溢出=%
if %result% equ 1 if %x% neq 1 if %x% neq %tmp1% exit /b 0
set /a "tmp2=%tmp1% & (1 << %bits%)"
if %tmp2% neq 0 set /a result=(result*%2) %% %1
set x=%result%
if %bits% gtr 0 goto :loop2_LP
if %result% equ 1 exit /b 1
goto :eof
It feels that the user input is not very useful.
Smaller numbers have all been tested.
The probability that larger numbers are prime is relatively low.
The efficiency of testing is very low.
-------------------------------------
But it's also okay.
Just add an input judgment.
If input i, use the built-in test set to determine prime numbers.
Otherwise, perform the judgment of a single prime number.
@echo off
setlocal EnableDelayedExpansion
:loop_test
set testnum=
set /p testnum=Please enter an integer (press i to use the built-in test set, press Enter directly to exit):
if "%testnum%"=="" goto :eof
if /i not "%testnum%"=="i" (
call :JudgePrime %testnum%
if errorlevel 2 (echo Invalid input: %testnum%
) else if errorlevel 1 (echo %testnum% is a prime number
) else (echo %testnum% is a composite number)
goto :loop_test
)
set time0=%time%
for /l %%i in (46001,2,48000) do (
set /a testnum=%%i
call :JudgePrime !testnum!
if !errorlevel! equ 1 set /p=!testnum! <nul & set /a iprime+=1
)
echo.
echo.
echo found: %iprime%
echo begin: %time0%
echo finish: %time%
pause
goto :eof
:JudgePrime
if == exit /b 2
set /a tmp1=%1
if not %tmp1%==%1 exit /b 2
if %1 lss 2 exit /b 0
if %1 equ 2 exit /b 1
set i=0
for %%i in (2,3,5,7,11) do (
set prime_!i!=%%i
set /a prime6p_!i!=%%i*%%i*%%i
set /a prime6p_!i!*=prime6p_!i!
set /a i+=1
)
set i=0
:loop1_JP
call set prime_i=%%prime_%i%%%
call set prime6p_i=%%prime6p_%i%%%
if %1 geq %prime6p_3% (
if !prime_i! equ 3 (
set /a i+=1
goto :loop1_JP
)
) else (
if !prime_i! neq 2 if %1 lss !prime6p_i! exit /b 1
)
call :LikePrime %1 %prime_i%
if not errorlevel 1 exit /b 0
set /a i+=1
if %i% lss 5 goto :loop1_JP
exit /b 1
goto :eof
:LikePrime
set /a x=result=1, tmp1=%1-1, bits=0
:loop1_LP
set /a bits+=1
set /a "tmp1>>=1"
if %tmp1% gtr 0 goto :loop1_LP
set /a tmp1=%1-1
:loop2_LP
set /a bits-=1
set /a result=(x*x) %% %1 %=This code judgment causes data overflow when dealing with large primes%
if %result% equ 1 if %x% neq 1 if %x% neq %tmp1% exit /b 0
set /a "tmp2=%tmp1% & (1 << %bits%)"
if %tmp2% neq 0 set /a result=(result*%2) %% %1
set x=%result%
if %bits% gtr 0 goto :loop2_LP
if %result% equ 1 exit /b 1
goto :eof
|
|
2007-1-29 05:35 |
|
|
qjbm
初级用户
 
积分 125
发帖 44
注册 2007-1-24
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
我的建议是先采集判断方法,然后再代码化.
对一个素数的判定,我的方法是:
--------------
条件1.
任一个>2的自然数,%%10 NEQ 1\3\7\9 为合数.
条件2.
不符合条件1的,能被小于其平方根的任意质数整除为合数.
不符合条件1.2的为质数.
-------------------
此方法的最大缺点是要计算"小于被测试数平方根的所有质数"
不过此方法可以保存一份自小至大的质数表,在多次次检测时可以显著提高效率
尤其当"后次检测在前任一次检测范围内",效率更高.
关于批处理中无法计算平方根的问题,可以变通一下.在枚举过程中,利用IF语句进行控制.
例如:我下面代码中 :MAIN 段中 IF...ELSE...
@ECHO %DBG% OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET /P X=请输入大于2的自然数:
CALL :PRI 2
:MAIN
FOR /L %%i IN (3,2,%X%) DO CALL :ANA %%i
EXIT /B
:ANA
FOR %%j IN (!STR!) DO (
SET /A K=%%j*%%j
IF !K! GTR %1 (
CALL :PRI %1 & GOTO :EOF
) ELSE (
SET /A T=%1%%%%j & IF !T! EQU 0 GOTO :EOF
)
)
GOTO :EOF
:PRI
SET STR=!STR! %1 & ECHO %1
GOTO :EOF
这段代码,2.7/512 配置 ,在X=10万 时效率尚可,时间为:
开始: 16:10:48.95
结束: 16:14:01.56
Last edited by qjbm on 2007-1-29 at 04:17 PM ]
My suggestion is to first collect the judgment method and then code it.
For the judgment of a prime number, my method is:
--------------
Condition 1.
Any natural number greater than 2, if %%10 NEQ 1\3\7\9, it is a composite number.
Condition 2.
For those not meeting condition 1, if it can be divided evenly by any prime number less than its square root, it is a composite number.
Those not meeting conditions 1 and 2 are prime numbers.
-------------------
The biggest shortcoming of this method is that it is necessary to calculate "all prime numbers less than the square root of the number to be tested".
However, this method can keep a list of prime numbers from small to large, which can significantly improve efficiency in multiple detections.
Especially when "the subsequent detection is within the range of the previous detection", the efficiency is higher.
Regarding the problem that the square root cannot be calculated in batch processing, it can be circumvented. In the enumeration process, use the IF statement for control.
For example: In the :MAIN segment in my following code, IF...ELSE...
@ECHO %DBG% OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET /P X=Please enter a natural number greater than 2:
CALL :PRI 2
:MAIN
FOR /L %%i IN (3,2,%X%) DO CALL :ANA %%i
EXIT /B
:ANA
FOR %%j IN (!STR!) DO (
SET /A K=%%j*%%j
IF !K! GTR %1 (
CALL :PRI %1 & GOTO :EOF
) ELSE (
SET /A T=%1%%%%j & IF !T! EQU 0 GOTO :EOF
)
)
GOTO :EOF
:PRI
SET STR=!STR! %1 & ECHO %1
GOTO :EOF
This code, with a 2.7/512 configuration, has acceptable efficiency when X=100,000. The time is:
Start: 16:10:48.95
End: 16:14:01.56
Last edited by qjbm on 2007-1-29 at 04:17 PM ]
|
|
2007-1-30 04:55 |
|
|
qzwqzw
银牌会员
     天的白色影子
积分 2343
发帖 636
注册 2004-3-6
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
编辑中有删除本帖的功能,可以试试
判定算法在顶楼给的链接里有讨论
你所给出的算法中对10的取模倒是很有创意
但给数时通常步进值取2
所以判断时可以不考虑0/2/4/6/8的尾数
而5是素数表中第三个素数
很快就会被试除法用来做判定
因此节约的性能可能很有限
There is a function to delete this post in the editor, you can try. The determination algorithm has discussions in the link given at the top floor. The modulo operation of 10 in the algorithm you provided is quite creative. But when giving numbers, usually the step value is taken as 2. So when judging, you can not consider the tail numbers of 0/2/4/6/8. And 5 is the third prime number in the prime number table, which will soon be used by the trial division method for judgment. Therefore, the performance saved may be very limited.
|
|
2007-1-30 05:51 |
|
|
qzwqzw
银牌会员
     天的白色影子
积分 2343
发帖 636
注册 2004-3-6
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
另外请注意
这里主要探讨单个32位正整数的素性判定
素数表的方法通常用于判定一个素数群
而且理论上说
如果要判定一个最大的32位正整数是否正整数
你的算法(实际上以前也讨论过这些算法)要构建的素数表就太大了
大约需要四千多个素数才能通过试除法判定
而且你的生成素数表的算法并没有优化
即没有到sqrt(n)就停止生成素数
素数表会大很多
系统环境空间明显是不够用的
Last edited by qzwqzw on 2007-1-29 at 05:09 PM ]
Also please note
Here mainly discuss the primality test of a single 32-bit positive integer
The method of prime table is usually used to determine a group of primes
And theoretically speaking
If you want to determine whether the largest 32-bit positive integer is a positive integer
Your algorithm (in fact, these algorithms have been discussed before) needs to build a prime table that is too large
It is approximately necessary to have more than four thousand primes to pass the trial division test
And your algorithm for generating the prime table is not optimized
That is, it does not stop generating primes until sqrt(n)
The prime table will be much larger
The system environment space is obviously not enough
Last edited by qzwqzw on 2007-1-29 at 05:09 PM ]
|
|
2007-1-30 06:07 |
|
|
qjbm
初级用户
 
积分 125
发帖 44
注册 2007-1-24
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
在8楼的代码中,由于批处理无法进行sqrt(n)计算
所以使用了:
SET /A K=%%j*%%j
IF !K! GTR %1 GOTO :EOF
进行控制,相当于到sqrt(n)就停止.
不过此种方法还是试除法.没有质的突破.
关于:
米勒-拉宾法加二次检测后所有的最小可判定底数都不大于n^(1/6)仍然只是一个
猜想,无法证明.我也曾利用试除法,对50万以内素数进行各种处理以寻找规律,
但都是在一定范围内有效....不能完全公式化..唔...可以公式化就留名青史了...:)
可以以此贴收集众家之长,说不定自然数素性判定法公式就在本贴产生.
Last edited by qjbm on 2007-1-29 at 06:11 PM ]
In the code on the 8th floor, since batch processing cannot perform sqrt(n) calculations, the following was used:
SET /A K=%%j*%%j
IF !K! GTR %1 GOTO :EOF
to control, which is equivalent to stopping when reaching sqrt(n). However, this method is still trial division, and there is no breakthrough in nature. Regarding the fact that all the smallest determinable bases after the Miller-Rabin method plus quadratic detection are all not greater than n^(1/6) is still just a conjecture and cannot be proved. I have also used trial division to process prime numbers within 500,000 in various ways to find patterns, but they are only effective within a certain range.... It cannot be completely formulaic.. Well... If it could be formulaic, it would be famous... :) This post can collect the strengths of everyone, and maybe the formula for the primality determination method of natural numbers will be generated in this post.
Last edited by qjbm on 2007-1-29 at 06:11 PM ]
|
|
2007-1-30 06:54 |
|
|
qzwqzw
银牌会员
     天的白色影子
积分 2343
发帖 636
注册 2004-3-6
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
关于8楼代码也许我理解的有误
IF !K! GTR %1 GOTO :EOF
感觉它只是控制试除的上界
而非生成素数的上界
比如判断一个整数7是否素数
素数表只需要生成到3就可以了
而你的素数表会生成到5
虽然它只会试除到3
所以在显示完是否素数后
应该再加一个判断
看是否需要将这个素数追加到到素数表
这个优化在顶楼提到了链接里已经有间接的实现了
------------------------------------------------
我是没奢望能讨论出新的判定公式了
只希望能将现有的算法代码化并能在有效的时间内得出结果
毕竟这里能讨论算法的人很少
能讨论数论的人更是罕见
There might be a misunderstanding about the code on floor 8.
IF !K! GTR %1 GOTO :EOF
It seems it just controls the upper bound of trial division rather than the upper bound of generating prime numbers. For example, to determine whether the integer 7 is a prime number, the prime number table only needs to be generated up to 3. But your prime number table will generate up to 5. Although it will only perform trial division up to 3. So after displaying whether it is a prime number, there should be an additional judgment to see if this prime number needs to be appended to the prime number table. This optimization was mentioned in the top floor, and there is an indirect implementation in the linked content.
------------------------------------------------
I didn't expect to discuss a new judgment formula. I just hope to code the existing algorithm and get the result within an effective time. After all, there are few people who can discuss algorithms here, and even fewer who can discuss number theory.
|
|
2007-1-30 07:26 |
|
|
qjbm
初级用户
 
积分 125
发帖 44
注册 2007-1-24
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
你的理解没有太大误差,
在被判断的自然数为质数时,被枚举素数表上限确实多一个.但不对其进行试除.
但是在被判断的自然数为合数时,是不到上限即可断定,
SET /A T=%1%%%%j & IF !T! EQU 0 GOTO :EOF
枚举到整除就断定.
我建议不要在对8楼的代码进行讨论,因为只是一个试除法.没有新建树,
放代码的原意是举例一下sqrt(n)在批处理中的变通.
---------------
昨天晚上在CCTV1看到一个节目,
说一个65岁初中农民用一生的时间来证明歌德巴赫猜想.未果,将愿望寄托与女儿身上..
精神可嘉.......
Last edited by qjbm on 2007-1-29 at 07:32 PM ]
Your understanding is not too inaccurate.
When the natural number to be judged is a prime number, the upper limit of the enumerated prime number table is indeed one more. But it is not divided by trial.
But when the natural number to be judged is a composite number, it can be determined before reaching the upper limit.
SET /A T=%1%%%%j & IF !T! EQU 0 GOTO :EOF
Enumerate until it is divisible to determine.
I suggest not discussing the code on floor 8 anymore, because it's just a trial division. There is no new tree built.
The original intention of putting the code is to give an example of the workaround of sqrt(n) in batch processing.
---------------
I saw a program on CCTV1 last night,
It said that a 65-year-old junior high school farmer spent his whole life proving Goldbach's conjecture. He failed and entrusted his wish to his daughter..
Admirable spirit.......
Last edited by qjbm on 2007-1-29 at 07:32 PM ]
|
|
2007-1-30 08:27 |
|
|
electronixtar
铂金会员
      
积分 7493
发帖 2672
注册 2005-9-2
状态 离线
|
|
2007-1-30 09:18 |
|
|
qzwqzw
银牌会员
     天的白色影子
积分 2343
发帖 636
注册 2004-3-6
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
electronixtar给的链接已阅
感觉批处理化的难度很大
方根倒数通常小于1
这在批处理中处理起来将会很繁琐
除非建立比较完善的浮点数运算机制
-----------------------------------------------------
顶楼代码的溢出上界已得到证明
不用再测试了
set /a result=(x*x) %% %1
这一句决定了在x^2的溢出上界为2^31
所以x的溢出上界为(2^31)^1/2,约为46340
if %tmp2% neq 0 set /a result=(result*%2) %% %1
set x=%result%
这两句决定了x不会大于等于判定数%1,最大值为%1-1
所以%1的溢出上界应该46341
所以可以确定46341之前的所有数确定不会溢出不会误判
46431之后溢出概率级数增大
漏判率也随之级数增大
-----------------------------------------------------
现在我比较倾向于使用新的算法来替代二次检验
搜索中……
The link given by electronixtar has been read.
It feels that the difficulty of batch processing is quite great.
The reciprocal of the square root is usually less than 1.
This will be very tedious to handle in batch processing.
Unless a relatively perfect floating-point operation mechanism is established.
-----------------------------------------------------
The upper bound of overflow of the code in the top floor has been proved.
No need to test anymore.
The sentence "set /a result=(x*x) %% %1" determines that the upper bound of overflow of x^2 is 2^31.
So the upper bound of overflow of x is (2^31)^1/2, approximately 46340.
The two sentences "if %tmp2% neq 0 set /a result=(result*%2) %% %1" and "set x=%result%" determine that x will not be greater than or equal to the judgment number %1, and the maximum value is %1-1.
So the upper bound of overflow of %1 should be 46341.
So it can be determined that all numbers before 46341 will not overflow and will not be misjudged.
The probability level of overflow increases after 46431.
The false judgment rate also increases at the same level.
-----------------------------------------------------
Now I am more inclined to use a new algorithm to replace the quadratic test.
Searching...
|
|
2007-1-30 11:46 |
|
|