Page 1 of 1

Find multiples of 3 and 5 below 1000

Posted: 06 Sep 2011 22:36
by Rileyh
Hi again,
I need help with getting my batch file to do maths in general.
I have gotten it to do simple arithmatic such as +,- etc, but it can't do complex maths.
Could someone please help me out

Thanks,
Rileyh

Re: Find multiples of 3 and 5 below 1000

Posted: 07 Sep 2011 08:33
by Exouxas
Like this?

Code: Select all

@echo off

set /a var1=1
set /a var2=1

:loop
set /a var1=var*3
set /a var2=var*5

if %var1% gtr 1000 goto waz
if %var2% gtr 1000 goto waz

echo %var1%
echo %var2%

goto loop
:waz
echo done
pause



by the way, this is not tested yet.

hope it helps!

-Exouxas

Re: Find multiples of 3 and 5 below 1000

Posted: 07 Sep 2011 18:42
by Rileyh
Sorry Exouxas,
I don't think it worked (it spat back hundreds of zeros!).....
Thanks so much for the effort though.

Riley,

P.S- Where did you get the code for that or did you think of it.
If you thought of the code, where did you learn- I would love to know!

Re: Find multiples of 3 and 5 below 1000

Posted: 07 Sep 2011 21:45
by nitt

Code: Select all

@echo off
echo.>test.log
for /l %%a in (0,3,1000) do (
echo %%a>>test.log
)
pause


That will send all the multiples of 3 all the way up to 1000 to a file named "test.log". Just change the "3" to a "5" to make it get the multiples of 5.

Re: Find multiples of 3 and 5 below 1000

Posted: 08 Sep 2011 07:45
by trebor68
Here is a test batch. Inside you will see how the command "set / a" is working.

Code: Select all

@echo off
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
echo          divided by 3      #      divided by 5
for /l %%a in (1,1,20) do (
  set numh=0
  set "result="
  set num=%%a
  if %%a lss 100 set num= %%a
  if %%a lss 10 set num=  %%a

  set /a num1=num/3
  set /a num2=num-num1*3
  set /a num3=num/5
  set /a num4=num-num/5*5

  if !num2!==0 set /a numh+=1
  if !num4!==0 set /a numh+=2
  if !numh!==1 set result=multiple of 3.
  if !numh!==2 set result=multiple of 5.
  if !numh!==3 set result=multiple of 3 and 5.

  rem if !numh!==3 echo !num!
  echo !num!   !num1!; remainder of !num2!    #   !num3!; remainder of !num4!   #   !result!
)