2016-08-02 66 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更快的方法。

相關問題