'How can I deal with this error in my Batch script?
I'm not sure what is happening as batch script closes almost immediately when I try to run it. It's pretty simple it should test whether a number(num
) is a prime Number. If not it continues until num1
is bigger than num
.
echo off
title Prime Numbers
cls
set /a prime=7
set /a num1=2
set /a num2=2
:do1
if %prime% == %num1% * %num2% goto do_if_1
if else %prime% LSS %num1% * %num2% set /a num2=%num2%+1
if else %prime% GTR %num1% * %num2% goto do_if_2
if else %prime% LSS %num1% * 1 goto do_if_3
goto do1
:do_if_1
set /a prime=%prime%+1
set /a num1=2
set /a num2=2
goto do1
:do_if_2
set /a num1=%num1%+1
set /a num2=2
goto do2
:do_if_3
echo %prime%
goto do_if_1
Solution 1:[1]
Edit: 6n±1 method
In case it's useful to anyone, here's a much more efficient method for testing whether a number is prime or composite. It's a Batch language adaptation of the 6n±1 algorithm mentioned by several people on this post.
@echo off & setlocal enabledelayedexpansion
if "%~1"=="" goto usage
for /f "delims=0123456789" %%I in ("%~1") do goto usage
if %~1 LEQ 1 (
echo %~1 is neither prime nor composite.
exit /b 1
) else if %~1 LEQ 3 (
echo %~1 is prime.
exit /b 0
)
set /a "mod2or3 = ^!(%~1 %% 2) | ^!(%~1 %% 3), half = %~1 / 2"
if %mod2or3% EQU 1 (
echo %~1 is composite.
exit /b 1
)
for /L %%I in (5,6,%half%) do (
set /a "n6pm1 = ^!(%~1 %% %%I) | ^!(%~1 %% (%%I + 2))"
if !n6pm1! EQU 1 (
echo %~1 is composite.
exit /b 1
)
)
echo %~1 is prime.
goto :EOF
:usage
echo Usage: %~nx0 integer
goto :EOF
Original answer:
Thought I might accept the challenge.
@echo off
setlocal enabledelayedexpansion
if "%~1"=="" goto usage
for /f "delims=0123456789" %%I in ("%~1") do goto usage
set /a limit = %~1 - 1
for /L %%I in (2,1,%limit%) do (
set /a modulo = %~1 %% %%I
if !modulo! equ 0 (
echo %~1 is composite ^(divisible by %%I^).
goto :EOF
)
)
echo %~1 is prime.
goto :EOF
:usage
echo Usage: %~nx0 integer
You had commented under Dominique's answer, "So how do I fix this?" Take the first part of your script:
set /a prime=7
set /a num1=2
set /a num2=2
:do1
if %prime% == %num1% * %num2% goto do_if_1
Since you've been informed by both Squashman and myself that if %prime% == %num1% * %num2%
is not going to work as you intend, you should now know that you need to insert an additional set /a
above your if
statement to perform %num1% * %num2%
.
set /a prime=7
set /a num1=2
set /a num2=2
:do1
set /a product = num1 * num2
if %prime% == %product% goto do_if_1
Let's continue...
...
if %prime% == %product% goto do_if_1
if else %prime% LSS %num1% * %num2% set /a num2=%num2%+1
There are two immediate problems here. #1, I think you probably meant else if
, not if else
. #2, in batch scripting, else
needs to be included as a part of the if
command you're else
-ing. This means else
either needs to be appended to the end of the previous line, or you should use some parentheses. Rewrite it like this:
if %prime% == %product% (
goto do_it_1
) else if %prime% LSS %product% (
set /a num2 += 1
) else etc...
Solution 2:[2]
The first thing I advise you, is to get rid of the echo off
, like this you can follow what your script is doing:
D:\>set /a prime=7
D:\>set /a num1=2
D:\>set /a num2=2
D:\>if 7 == 2 * 2 goto do_if_1
7 was unexpected at this time. => this is causing your problem!
D:\>if else 7 LSS 2 * 2 set /a num2=2+1
D:\>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Community |
Solution 2 | Dominique |