2016-08-02 30 views
5

作为新的东西,我试图使用批处理脚本(https://projecteuler.net/problem=5)来完成Project Euler Problem 5。然而;我遇到了一些问题。如果任何人都可以阅读我的代码,那会很棒。在批处理脚本中执行模运算

@ECHO off 

SET init=1 
SET iter=1 
SET /a func=%init% %% %iter% 
cls 

:Num 
IF func==0 (
    IF iter==20 (
     ECHO Val = %init% 
     pause 
     exit 
    ) ELSE (
     SET /a iter+=1 
     GOTO Num 
    ) 
) ELSE (
    SET iter=1 
    SET /a init+=1 
    GOTO Num 
) 

什么它的意思做的是检查是否init mod iter返回一个0,如果确实如此,加1到iter值,直到它达到21.不过,如果它不等于0,则迭代计数将被设置回0,并且将再次开始计算。


什么的意思发生的一个例子:

1 mod 1 = 0, Therefor add 1 to iter 
1 mod 2 != 0, Therefor init is set to 0 and 1 is added to init 
2 mod 1 = 0, Therefor add 1 to iter 
2 mod 2 = 0, Therefor add 1 to iter 
2 mod 3 != 0, Therefor init is set to 0 and 1 is added to init 

等等等等。


的是什么发生的一个例子:

1 mod 1 != 0, Therefor add 1 to init 
2 mod 1 != 0, Therefor add 1 to init 
3 mod 1 != 0, Therefor add 1 to init 

等等等等。



任何帮助表示赞赏,谢谢。

+0

但是,我已经对代码进行了一些更改;现在它完全跳过模数运算,并且说1 mod 1到20 = 0。为什么它会这样做? '@ECHO关闭 SET初始化= 1名 SET ITER = 1个 SET/FUNC一个= “%INIT%%%%ITER%” CLS :货号 IF “%FUNC%” ==“0 “( \t IF ”%ITER%“ == ”21“( \t \t回波。 \t \t ECHO缬氨酸=%初始化% \t \t回波。 \t \t回声按任何键退出。 \t \t p澳洲英语> NUL \t \t出口 \t)ELSE( \t \t回波%初始化%MOD%ITER%= 0; CONT \t \t SET /一个ITER + = 1 \t \t GOTO货号 \t) )ELSE( 回波%初始化%MOD%ITER%!= 0; BREAK \t SET ITER = 1 \t SET /一个INIT + = 1 \t GOTO货号 )' 很抱歉的丑陋格式化。 – Sennsei

+3

你从不重新计算'%func%'。 – SomethingDark

+0

我该如何加入? – Sennsei

回答

0

如何:(!?)

@Echo off 
setlocal enabledelayedexpansion 
SET init=1 
SET iter=1 
cls 
set loopCounter=1 
set loopBatch=1 

:numLoop 
SET /a func="!init! %% !iter!" 
IF !iter! == 21 (goto :done) 
IF !func! == 0 (call :incIter) ELSE (call :incInit) 
SET /a loopCounter+=1 
SET /a loopBatch="%loopCounter% %% 1000" 
if !loopBatch! == 0 (echo %loopCounter% iterations done) 
goto :numLoop 

:incInit 
    rem echo %init% mod %iter% == %func%; Increasing init 
    SET iter=1 
    SET /a init+=1 
    goto :eof 

:incIter 
    rem echo %init% mod %iter% == %func%; Increasing iter 
    SET /a iter+=1 
    goto :eof 

:done 
    echo. 
    ECHO Val = %init% 
0

只是为了发布 “实用” 的解决方案,以防万一有人能找到 “有用”,根据它

@echo off 
    setlocal enableextensions enabledelayedexpansion 

    rem Our search limit 
    set "limit=20" 
    rem Note: batch arithmetic is limited to 2^31 values, so 26 is the highest 
    rem  value that we can directly use 

    rem Initialize searched number 
    set "euler5=1" 

    rem Initialize list of numbers for a Erastotenes cribe 
    for /l %%a in (2 1 %limit%) do set "f.%%a=%%a" 

    rem Search for prime numbers and simplify (divide) greater multiples 
    rem Keep multiplying as we iterate over the list 
    for /l %%a in (2 1 %limit%) do (
     if !f.%%a! gtr 1 (
      set /a "euler5*=!f.%%a!" 
      for %%c in (!f.%%a!) do for /l %%b in (%%a %%a %limit%) do (
       set /a "f.%%b/=%%c" 
      ) 
     ) 
    ) 

    rem Echo solution 
    echo %euler5% 
0

(!?)到这个链接:link有一个模数运算符。

所以,你可以试试这个,而不是

@echo off 

::we should start with 21 because we know that all numbers from 1-20 
::cannot be divided by 20 all 20 times. 
::This will also fix the problem of an unwanted a zero remainder at the 
::early numbers 

set count=21 
set divide=1 
::We need to set enabledelayedexpansion so we can use ! as a varible 
::expander. 
setlocal enabledelayedexpansion 

:loop 
:: begin the modulus operator. 
set /a remainder=!count!%%!divide! 
if %remainder%==0 (
if %divide%==20 
::Yea! 
echo number found:%count% 
::Don't forget to pause 
::or else you cant see the number. 
pause 
) else (
set /a divide=%divide%+1 
::equivelent to set /a divide+=1 
goto :loop 
) 
) else (
set /a count=%count%+1 
goto :loop 
) 

虽然不能解决你的脚本,它是definitly更快的方法。

相关问题