Billy McBurnie Billy McBurnie - 14 days ago 6
SQL Question

SQL Solve a Function with a While Loop

Using SQL server, write a while loop to calculate and print the functional value of f(x) = 3x&3 -2X^2 + 15 for all odd non negative integers <= 25. ( 1, 3, 5, …23,25).

This is what I have written out so far

My Query

And this is the result I get

My Results

So theoretically I should be getting 45,640 when

@x = 25


What am I going wrong here?

Answer

You have to use power function like the following

Declare @SUMSQUARE Integer, @x Integer
SET @X = 1

WHILE (@X <= 25)
BEGIN

SELECT @SUMSQUARE =  (3*power(@X,3))-(2*power(@X,2))+15
PRINT @SUMSQUARE
SET @X = @X + 2

END
Comments