0

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
Dominique
  • 16,450
  • 15
  • 56
  • 112
TheGamerLord
  • 79
  • 1
  • 2
  • 9
  • 2
    You can't do math within an IF statement. You can only do arithmetic operations with SET /A. And that is not the correct syntax for an IF ELSE statement. Read the help for the SET and IF commands. – Squashman Oct 27 '15 at 14:30

2 Answers2

2

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...
Community
  • 1
  • 1
rojo
  • 24,000
  • 5
  • 55
  • 101
  • and that was how my batch script was shamed XD – TheGamerLord Oct 27 '15 at 16:13
  • sorry when i asked him i didn't even notice your edit sorry, but thxs it was useful – TheGamerLord Oct 27 '15 at 17:39
  • @TheGamerLord If my answer was helpful, please consider marking it as accepted, or at least upvoting it. [See this page](http://meta.stackexchange.com/questions/5234/) for an explanation of why this is important. – rojo Oct 27 '15 at 17:44
  • im getting to it i just noticed a problem in both our programs it has prime numbers and some non-prime – TheGamerLord Oct 27 '15 at 17:46
  • Mine agrees with https://primes.utm.edu/lists/small/1000.txt up to 509. I haven't checked beyond that, but I don't doubt it's correct for larger numbers. Only problem with mine is that it's inefficient for big numbers. I tried checking 2147483647 but I got bored and Ctrl-C'd it after 10 minutes or so. – rojo Oct 27 '15 at 17:50
1

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:\>
Dominique
  • 16,450
  • 15
  • 56
  • 112
  • how was 7 unexpected? – TheGamerLord Oct 27 '15 at 16:19
  • 1
    It was certainly a surprise to me. I think Dominique's major point was that the first step in troubleshooting a script that's throwing errors is to leave `echo` enabled, so you can see which part of the script is causing grief. In this case, `if 7 == 2 * 2` isn't throwing the error, but it doesn't do what you think it does. The error is being thrown when the script encounters `if else 7`. It's treating `else` as a string literal, not a keyword. It's treating `7` as a comparison operator, and complaining that `7` is neither `==` nor `equ` nor `gtr` nor any other operator it understands. – rojo Oct 27 '15 at 16:20
  • so how can i fix this? and even when i turn echo off it closes way too fast for me to actually read what it is saying. – TheGamerLord Oct 27 '15 at 16:26
  • @TheGamerLord See my edited answer. Also, you should open a cmd prompt and execute your script by typing its name, rather than double-clicking on it. That way the console window will stay open after execution completes. – rojo Oct 27 '15 at 16:49
  • @TheGamerLord, did you bother to read my comments under your original question. – Squashman Oct 27 '15 at 16:50