Batch error "+ unexpected"

I am trying to encode the collatz hypothesis tag in batch (this is mathematical stuff). The point of the file is to check if the number is even, and if it divides by two. If the number is odd, it should be multiplied by three and add one. This should be repeated over and over again. Each time I try to run this file, it allows me to enter a number, then it says that "+ was unexpected at this time." What is my mistake?

@echo off
color f0
title Collatz Conjecture Tester
echo/
echo Enter the number you want to test.
echo/
echo/
set /p number=
:start
set /a test=%number% %% 2
if %test% EQU 0 ( 
    set /a number=%number% * 1/2
) else (
    set /a number=(%number% * 3) + 1
)
echo/
echo Result: %number%
timeout /t 1 >nul
goto start
+4
source share
2 answers

SomethingDark already found the root cause of the problem and showed the correct solution in their answer .

, &, |, ^, <<, >> .. set /A :

if %test% EQU 0 ( 
    set /A "number=%number% * 1/2"
) else (
    set /A "number=(%number% * 3) + 1"
)

, , .

, % %% .
case delayed expansion , ! ^!, "", ^^! .

+2

if, for , , , , .

, if :

else (
    set /a number=(%number% * 3
)
+ 1

, else a + 1 - . , ^.

if %test% EQU 0 ( 
    set /a number=%number% * 1/2
) else (
    set /a number=(%number% * 3^) + 1
)
+3

Source: https://habr.com/ru/post/1659929/


All Articles