Unknown equations of mathematical operations

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
GAGATOSHUT
Posts: 4
Joined: 10 Dec 2015 06:55

Unknown equations of mathematical operations

#1 Post by GAGATOSHUT » 17 Dec 2015 08:51

Mathematical equations : x*7+3=80

Although very simple
But this is a simple algorithm of thought for a long time
May I have an in optimization?

Code: Select all

@echo off & setlocal EnableDelayedExpansion

   (for /l %%a in (1 1 100) do (
      for /l %%b in (7 1 7) do (
         for /l %%c in (3 1 3) do (
               set/a a=%%a*%%b+%%c
               set "b=!a!"
               set/p c=%%a*%%b+%%c=!b:~0,10!<nul
            )
            echo.
         )
         echo.
      ))>1.TXT
      findstr "\<7+3=80\>" "1.TXT"
      pause>nul

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Unknown equations of mathematical operations

#2 Post by Aacini » 17 Dec 2015 11:13

I don't like "questions" where the OP explain absolutely nothing about the problem, just throw some code and ask "What do you think?" (or something similar). To me, these questions imply: "think whatever you want about this code, but post some answer", so the answer may include anything I wish. Well, this is my answer:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set "f(x)=x*7-77"

cls
echo f(x) = %f(x)%
echo ------------------
set /A "x=1, y0=%f(x)%"
for /L %%x in (1,1,100) do (
   set /A "x=%%x, y1=%f(x)%, sign1=y0*y1, sign2=sign1>>31, y0=y1"
   REM echo f(%%x^) = !y1!
   if !sign1! equ 0 (
      echo Exact root found: %%x
      set y0=1
   ) else if !sign2! equ -1 (
      echo Root exist between %%x and previous X
   )
)

EDIT: Another version:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

cls
echo Enter polynomial coefficients (i.e.  1 3 -5 -10  for x^^3 + 3*x^^2 - 5*x - 10)
set /P "coef="
set /P "range=Enter range for X: "
for /F "tokens=1,2" %%a in ("%range%") do set /A minX=%%a, maxX=%%b
echo/

(
set "coef="
for %%a in (%coef%) do set "coef=%%a !coef!"
)

set /A "y0=0, x=1"
for %%N in (%coef%) do set /A "y0+=%%N*x, x*=minX"
for /L %%x in (%minX%,1,%maxX%) do (
   set /A "y1=0, x=1"
   for %%N in (%coef%) do set /A "y1+=%%N*x, x*=%%x"
   set /A "sign1=y0*y1, sign2=sign1>>31, y0=y1"
   echo f(%%x^) = !y1!
   if !sign1! equ 0 (
      echo Exact root found: %%x
      set y0=1
   ) else if !sign2! equ -1 (
      set /A prevX=%%x-1
      echo Root exist between !prevX! and %%x
   )
)

Output example:

Code: Select all

Enter polynomial coefficients (i.e.  1 3 -5 -10  for x^3 + 3*x^2 - 5*x - 10)
1 3 -5 -10
Enter range for X: -10 10

f(-10) = -660
f(-9) = -451
f(-8) = -290
f(-7) = -171
f(-6) = -88
f(-5) = -35
f(-4) = -6
f(-3) = 5
Root exist between -4 and -3
f(-2) = 4
f(-1) = -3
Root exist between -2 and -1
f(0) = -10
f(1) = -11
f(2) = 0
Exact root found: 2
f(3) = 29
f(4) = 82
f(5) = 165
f(6) = 284
f(7) = 445
f(8) = 654
f(9) = 917
f(10) = 1240

Antonio

GAGATOSHUT
Posts: 4
Joined: 10 Dec 2015 06:55

Re: Unknown equations of mathematical operations

#3 Post by GAGATOSHUT » 17 Dec 2015 23:47

Thank you Antonio!
You gave me some method, or some issues worth me thinking

Code: Select all

sign2=sign1>>31

Can you explain this sentence what mean
thank you

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Unknown equations of mathematical operations

#4 Post by Squashman » 18 Dec 2015 07:56

GAGATOSHUT wrote:Thank you Antonio!
You gave me some method, or some issues worth me thinking

Code: Select all

sign2=sign1>>31

Can you explain this sentence what mean
thank you

Well you could read the HELP for the SET command.

Code: Select all

The /A switch specifies that the string to the right of the equal sign
is a numerical expression that is evaluated.  The expression evaluator
is pretty simple and supports the following operations, in decreasing
order of precedence:

    ()                  - grouping
    ! ~ -               - unary operators
    * / %               - arithmetic operators
    + -                 - arithmetic operators
    << >>               - logical shift
    &                   - bitwise and
    ^                   - bitwise exclusive or
    |                   - bitwise or
    = *= /= %= += -=    - assignment
      &= ^= |= <<= >>=
    ,                   - expression separator

Post Reply