Question: Please help me make my algorithm more efficient to reduce the runtime error ( max is 1 0 minutes ) . Right now, all my
Please help me make my algorithm more efficient to reduce the runtime error max is minutes Right now, all my test cases pass except for large positive values. The below code has to be a program not a function!!
Calculates a x y
a is a local variable
x & y are static variables
Initialise local variable a to
push constant
pop local
Load x and y into temp variables
push static
pop temp temp x
push static
pop temp temp y
Calculate if result will be negative
push temp
push constant
lt
push temp
push constant
lt
eq
not
pop temp temp if result should be negative, else
Take absolute value of x
push temp
push constant
lt
ifgoto MAKEXPOSITIVE
goto XISPOSITIVE
label MAKEXPOSITIVE
push temp
neg
pop temp
label XISPOSITIVE
Take absolute value of y
push temp
push constant
lt
ifgoto MAKEYPOSITIVE
goto YISPOSITIVE
label MAKEYPOSITIVE
push temp
neg
pop temp
label YISPOSITIVE
Multiplication loop
label LOOP
push temp
push constant
eq
ifgoto ENDLOOP
Add x to a
push local
push temp
add
pop local
Decrement y
push temp
push constant
sub
pop temp
Continue loop
goto LOOP
label ENDLOOP
Apply negative sign to the result if necessary
push temp
push constant
eq
ifgoto END
Negate result
push local
neg
pop local
End of program
label END
goto END
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
