kaprekar number check T-SQL problem

Anim233 21 Reputation points
2022-05-08T15:06:59.9+00:00

Hello, i made a function on T-SQL that checks if the number is a kaprekar one
CREATE FUNCTION [dbo].[kaprekar](@n int)
RETURNS bit
WITH EXECUTE AS CALLER
AS
BEGIN
DECLARE @result bit;
DECLARE @sq_n INT;
DECLARE @count_digits INT;
DECLARE @r_digits INT;
DECLARE @eq_parts INT;
DECLARE @sum INT;
``
SET @sq_n = @n * @n;
SET @count_digits=0;
WHILE @sq_n <> 0
BEGIN
SET @count_digits= @count_digits + @count_digits;
SET @sq_n=@sq_n/10;
END;
SET @sq_n = @n*@n;

WHILE @r_digits<@count_digits
BEGIN
SET @eq_parts=POWER(10,@r_digits);
SET @sum = @sq_n/@eq_parts + @sq_n% @eq_parts;
END;

IF @n = 1
BEGIN
SET @result=1;
END;
ELSE IF @eq_parts = @n
BEGIN
SET @result=0;
`END;` `ELSE IF @sum=@n` `BEGIN` `SET @result=1;`
END;
ELSE
BEGIN
SET @result=0;
END;
` RETURN @result;` `END;`
But it don't work. when i insert any number except 1, it prints 0, which is not correct. i tried to analyze my code, and made a conclusion that the problem is with in the main code block, not with the massive IF ELSE block. can someone help?

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,597 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jingyang Li 5,891 Reputation points
    2022-05-08T17:45:12.073+00:00
    create or alter FUNCTION [dbo].[kaprekar](@n int)
    RETURNS bit
    AS
    BEGIN
    DECLARE @result bit;
    Begin
    If exists ( 
    select 1
    from 
    (select @n number,   square_value,   
    Cast(left(square_value,sq_len-n ) as int) number_left
    ,Cast(stuff(square_value,1,(sq_len-n ),'') as int) number_right 
    from (
    Select @n number,  len(square(@n)) sq_len  
    ,square(@n) square_value   )t
    Cross apply (Select Number FROM master.dbo.spt_values
    where type='P' and Number >=1 and Number <=1000 and Number< len(square(@n)) ) d(n)
    ) t
    where number_left+number_right=number
    
    )
    Select @result=1
    else 
    Select @result=0
    End
    
    RETURN @result; 
    
    END;
    
    /*
    select  [dbo].[kaprekar](9)
    --9 45 55 99 297 703 999
    
    */
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Tom Cooper 8,466 Reputation points
    2022-05-08T17:36:09.767+00:00

    One way to do it
    CREATE FUNCTION [dbo].[kaprekar](@n int)
    RETURNS bit
    WITH EXECUTE AS CALLER
    AS
    BEGIN
    Declare @nSqrd As bigint,
    @nSqrdString As varchar(20),
    @LennSqrd As bigint,
    @Pointer As bigint,
    @Result bit;
    Set @Result = 0;
    Set @nSqrd = Power(Cast(@n As bigint), 2);
    Set @nSqrdString = @nSqrd;
    Set @Pointer = 1;
    If @n = 1
    Begin
    Set @Result = 1;;
    End;
    While @Pointer < Len(@nSqrdString) And @Result = 0
    Begin
    If Cast(Left(@nSqrdString, @Pointer) As bigint) + Cast(SubString(@nSqrdString, @Pointer + 1, Len(@nSqrdString)) As bigint) = @n
    And Cast(SubString(@nSqrdString, @Pointer + 1, Len(@nSqrdString)) As bigint) > 0
    Begin
    Set @Result = 1;
    End;
    Set @Pointer = @Pointer + 1;
    END;
    Return @Result;
    End;
    go

    Tom

    0 comments No comments