Performing module arithmetic in a Script package

As something new, I am trying to complete the Project Euler 5 task with a script package ( https://projecteuler.net/problem=5 ). But; I ran into a few problems. If anyone could skip my code, that would be great.

@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 ) 

What you need to do is check if the init mod iter returns 0, and if so, add 1 to the iter until it reaches 21. However; if it is not 0, the iteration counter will be set to 0 and will begin to be calculated again.


An example of what will happen MEANT:
 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 

And so on and so forth.


An example of what is happening:
 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 

And so on and so forth.



Any help is appreciated, thanks.
+5
source share
3 answers

How about this:

 @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
source

Just to post a โ€œpracticalโ€ (!?) Solution in case someone can find it โ€œusefulโ€ (!!?)

 @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
source

this link: link exists module operator.

So you can try this instead

 @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 ) 

Although this does not fix your script, it is certainly a faster way.

0
source

All Articles