Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
3 Altered Code to save bytes, new code correct for all values.
source | link

T-SQL, 221 189189 177 bytes

EDIT: The original versions of this code would produce incorrect output for some numbers, this has been corrected.

  With every query here, just add the number to calculate before the first comma.

Everyone know that T-SQL is the best golfing language. Here is a version that will calculate even the last test case. On the machine I tested it on, it ran in under a second, I'd be interested to see how it runs for everyone else.

DECLARE @I@ BIGINT=1000BIGINT=,@T VARCHAR(MAX)='';WITH M AS(SELECT CAST(1 AS2AS BIGINT)I UNION ALL SELECT I+I+1FROMI*2FROM M WHERE I<@I/2I<@)SELECT @T += CASTSTR(@I@/(I AS VARCHAR-1),@I%=I 1),@%=(I-1)FROM M ORDER BY I DESC SELECT @T

And here it is again, but readable:

DECLARE 
    @I BIGINT =@ BIGINT=,
    @T VARCHAR(MAX)='';

WITH M AS
(
    SELECT 
        CAST(12 AS BIGINT) I

    UNION ALL

    SELECT
      I * I+I+12
    FROM M
    WHERE I<@I/2I < @
)

SELECT 
    @T += CAST@T+=STR(@I@/(I AS VARCHAR-1),1),
    @I%=I@%=(I-1)
FROM M 
ORDER BY I DESC

SELECT @T

If I only use ints, this can be a bit shorter, coming out at 174157 bytes:

DECLARE @I INT@ =INT=,@T VARCHAR(MAX)='';WITH M AS(SELECT 1I2I UNION ALL SELECT I+I+1FROMI*2FROM M WHERE I+I+1<=@II<@)SELECT @T+=CAST@T+=STR(@I@/(I AS VARCHAR-1),@I %= 1),@%=(I -1)FROM M ORDER BY I DESC SELECT @T

And once more again, more readable:

DECLARE 
    @I INT@ =INT=,
    @T VARCHAR(MAX)='';

WITH M AS
(
    SELECT 
 1I       2I

    UNION ALL

    SELECT I+I+1
        I * 2
    FROM M
    WHERE I+I+1I <=< @I@
)

SELECT 
    @T+=CAST@T+=STR(@I@/(I AS VARCHAR-1),1),
    @I %= @%=(I-1)
FROM M 
ORDER BY I DESC

SELECT @T

T-SQL, 221 189 bytes

  With every query here, just add the number to calculate before the first comma.

Everyone know that T-SQL is the best golfing language. Here is a version that will calculate even the last test case. On the machine I tested it on, it ran in under a second, I'd be interested to see how it runs for everyone else.

DECLARE @I BIGINT=1000,@T VARCHAR(MAX)='';WITH M AS(SELECT CAST(1 AS BIGINT)I UNION ALL SELECT I+I+1FROM M WHERE I<@I/2)SELECT @T += CAST(@I/I AS VARCHAR),@I%=I FROM M ORDER BY I DESC SELECT @T

And here it is again, but readable:

DECLARE 
    @I BIGINT = ,
    @T VARCHAR(MAX)='';

WITH M AS
(
    SELECT 
        CAST(1 AS BIGINT)I

    UNION ALL

    SELECT
        I+I+1
    FROM M
    WHERE I<@I/2
)

SELECT 
    @T += CAST(@I/I AS VARCHAR),
    @I%=I
FROM M
ORDER BY I DESC

SELECT @T

If I only use ints, this can be a bit shorter, coming out at 174 bytes:

DECLARE @I INT =,@T VARCHAR(MAX)='';WITH M AS(SELECT 1I UNION ALL SELECT I+I+1FROM M WHERE I+I+1<=@I)SELECT @T+=CAST(@I/I AS VARCHAR),@I %= I FROM M ORDER BY I DESC SELECT @T

And once more again, more readable:

DECLARE 
    @I INT =,
    @T VARCHAR(MAX)='';

WITH M AS
(
    SELECT 1I

    UNION ALL

    SELECT I+I+1
    FROM M
    WHERE I+I+1 <= @I
)

SELECT
    @T+=CAST(@I/I AS VARCHAR),
    @I %= I
FROM M
ORDER BY I DESC

SELECT @T

T-SQL, 221 189 177 bytes

EDIT: The original versions of this code would produce incorrect output for some numbers, this has been corrected.

With every query here, just add the number to calculate before the first comma.

Everyone know that T-SQL is the best golfing language. Here is a version that will calculate even the last test case. On the machine I tested it on, it ran in under a second, I'd be interested to see how it runs for everyone else.

DECLARE @ BIGINT=,@T VARCHAR(MAX)='';WITH M AS(SELECT CAST(2AS BIGINT)I UNION ALL SELECT I*2FROM M WHERE I<@)SELECT @T += STR(@/(I-1),1),@%=(I-1)FROM M ORDER BY I DESC SELECT @T

And here it is again, but readable:

DECLARE 
    @ BIGINT=,
    @T VARCHAR(MAX)='';

WITH M AS
(
    SELECT
        CAST(2 AS BIGINT) I

    UNION ALL

    SELECT I * 2
    FROM M
    WHERE I < @
)

SELECT 
    @T+=STR(@/(I-1),1),
    @%=(I-1)
FROM M 
ORDER BY I DESC

SELECT @T

If I only use ints, this can be a bit shorter, coming out at 157 bytes:

DECLARE @ INT=,@T VARCHAR(MAX)='';WITH M AS(SELECT 2I UNION ALL SELECT I*2FROM M WHERE I<@)SELECT @T+=STR(@/(I-1),1),@%=(I-1)FROM M ORDER BY I DESC SELECT @T

And once more again, more readable:

DECLARE 
    @ INT=,
    @T VARCHAR(MAX)='';

WITH M AS
(
    SELECT 
        2I

    UNION ALL

    SELECT 
        I * 2
    FROM M
    WHERE I < @
)

SELECT 
    @T+=STR(@/(I-1),1),
    @%=(I-1)
FROM M 
ORDER BY I DESC

SELECT @T
2 Removed redudant bigint casts and altered where clause to save 32 bytes
source | link

T-SQL, 221221 189 bytes

With every query here, just add the number to calculate before the first comma.

Everyone know that T-SQL is the best golfing language. Here is a version that will calculate even the last test case. On the machine I tested it on, it ran in under a second, I'd be interested to see how it runs for everyone else.

DECLARE@IDECLARE BIGINT@I =BIGINT=1000, @T VARCHAR(MAX)='';WITH M AS(SELECT CAST(1 AS BIGINT)I UNION ALL SELECT CAST(I+I+1AS BIGINT)FROMI+I+1FROM M WHERE CAST(I+I+1AS BIGINT)<=@II<@I/2)SELECT@T+=CASTSELECT @T += CAST(@I/I AS VARCHAR),@I%=I FROM M ORDER BY I DESC SELECT @T

And here it is again, but readable:

DECLARE 
    @I BIGINT = ,
    @T VARCHAR(MAX)='';

WITH M AS
(
    SELECT 
        CAST(1 AS BIGINT)I

    UNION ALL

    SELECT
        CAST(I+I+1AS BIGINT)I+I+1
    FROM M
    WHERE CAST(I+I+1AS BIGINT)<=@II<@I/2
)

SELECT 
    @T += CAST(@I/I AS VARCHAR),
    @I%=I
FROM M
ORDER BY I DESC

SELECT @T

If I only use ints, this can be a bit shorter, coming out at 174 bytes:

DECLARE @I INT =,@T VARCHAR(MAX)='';WITH M AS(SELECT 1I UNION ALL SELECT I+I+1FROM M WHERE I+I+1<=@I)SELECT @T+=CAST(@I/I AS VARCHAR),@I %= I FROM M ORDER BY I DESC SELECT @T

And once more again, more readable:

DECLARE 
    @I INT =,
    @T VARCHAR(MAX)='';

WITH M AS
(
    SELECT 1I

    UNION ALL

    SELECT I+I+1
    FROM M
    WHERE I+I+1 <= @I
)

SELECT
    @T+=CAST(@I/I AS VARCHAR),
    @I %= I
FROM M
ORDER BY I DESC

SELECT @T

T-SQL, 221 bytes

With every query here, just add the number to calculate before the first comma.

Everyone know that T-SQL is the best golfing language. Here is a version that will calculate even the last test case. On the machine I tested it on, it ran in under a second, I'd be interested to see how it runs for everyone else.

DECLARE@I BIGINT =, @T VARCHAR(MAX)='';WITH M AS(SELECT CAST(1 AS BIGINT)I UNION ALL SELECT CAST(I+I+1AS BIGINT)FROM M WHERE CAST(I+I+1AS BIGINT)<=@I)SELECT@T+=CAST(@I/I AS VARCHAR),@I%=I FROM M ORDER BY I DESC SELECT @T

And here it is again, but readable:

DECLARE 
    @I BIGINT = ,
    @T VARCHAR(MAX)='';

WITH M AS
(
    SELECT 
        CAST(1 AS BIGINT)I

    UNION ALL

    SELECT
        CAST(I+I+1AS BIGINT)
    FROM M
    WHERE CAST(I+I+1AS BIGINT)<=@I
)

SELECT 
    @T += CAST(@I/I AS VARCHAR),
    @I%=I
FROM M
ORDER BY I DESC

SELECT @T

If I only use ints, this can be a bit shorter, coming out at 174 bytes:

DECLARE @I INT =,@T VARCHAR(MAX)='';WITH M AS(SELECT 1I UNION ALL SELECT I+I+1FROM M WHERE I+I+1<=@I)SELECT @T+=CAST(@I/I AS VARCHAR),@I %= I FROM M ORDER BY I DESC SELECT @T

And once more again, more readable:

DECLARE 
    @I INT =,
    @T VARCHAR(MAX)='';

WITH M AS
(
    SELECT 1I

    UNION ALL

    SELECT I+I+1
    FROM M
    WHERE I+I+1 <= @I
)

SELECT
    @T+=CAST(@I/I AS VARCHAR),
    @I %= I
FROM M
ORDER BY I DESC

SELECT @T

T-SQL, 221 189 bytes

With every query here, just add the number to calculate before the first comma.

Everyone know that T-SQL is the best golfing language. Here is a version that will calculate even the last test case. On the machine I tested it on, it ran in under a second, I'd be interested to see how it runs for everyone else.

DECLARE @I BIGINT=1000,@T VARCHAR(MAX)='';WITH M AS(SELECT CAST(1 AS BIGINT)I UNION ALL SELECT I+I+1FROM M WHERE I<@I/2)SELECT @T += CAST(@I/I AS VARCHAR),@I%=I FROM M ORDER BY I DESC SELECT @T

And here it is again, but readable:

DECLARE 
    @I BIGINT = ,
    @T VARCHAR(MAX)='';

WITH M AS
(
    SELECT 
        CAST(1 AS BIGINT)I

    UNION ALL

    SELECT
        I+I+1
    FROM M
    WHERE I<@I/2
)

SELECT 
    @T += CAST(@I/I AS VARCHAR),
    @I%=I
FROM M
ORDER BY I DESC

SELECT @T

If I only use ints, this can be a bit shorter, coming out at 174 bytes:

DECLARE @I INT =,@T VARCHAR(MAX)='';WITH M AS(SELECT 1I UNION ALL SELECT I+I+1FROM M WHERE I+I+1<=@I)SELECT @T+=CAST(@I/I AS VARCHAR),@I %= I FROM M ORDER BY I DESC SELECT @T

And once more again, more readable:

DECLARE 
    @I INT =,
    @T VARCHAR(MAX)='';

WITH M AS
(
    SELECT 1I

    UNION ALL

    SELECT I+I+1
    FROM M
    WHERE I+I+1 <= @I
)

SELECT
    @T+=CAST(@I/I AS VARCHAR),
    @I %= I
FROM M
ORDER BY I DESC

SELECT @T
1
source | link

T-SQL, 221 bytes

With every query here, just add the number to calculate before the first comma.

Everyone know that T-SQL is the best golfing language. Here is a version that will calculate even the last test case. On the machine I tested it on, it ran in under a second, I'd be interested to see how it runs for everyone else.

DECLARE@I BIGINT =, @T VARCHAR(MAX)='';WITH M AS(SELECT CAST(1 AS BIGINT)I UNION ALL SELECT CAST(I+I+1AS BIGINT)FROM M WHERE CAST(I+I+1AS BIGINT)<=@I)SELECT@T+=CAST(@I/I AS VARCHAR),@I%=I FROM M ORDER BY I DESC SELECT @T

And here it is again, but readable:

DECLARE 
    @I BIGINT = ,
    @T VARCHAR(MAX)='';

WITH M AS
(
    SELECT 
        CAST(1 AS BIGINT)I

    UNION ALL

    SELECT
        CAST(I+I+1AS BIGINT)
    FROM M
    WHERE CAST(I+I+1AS BIGINT)<=@I
)

SELECT 
    @T += CAST(@I/I AS VARCHAR),
    @I%=I
FROM M
ORDER BY I DESC

SELECT @T

If I only use ints, this can be a bit shorter, coming out at 174 bytes:

DECLARE @I INT =,@T VARCHAR(MAX)='';WITH M AS(SELECT 1I UNION ALL SELECT I+I+1FROM M WHERE I+I+1<=@I)SELECT @T+=CAST(@I/I AS VARCHAR),@I %= I FROM M ORDER BY I DESC SELECT @T

And once more again, more readable:

DECLARE 
    @I INT =,
    @T VARCHAR(MAX)='';

WITH M AS
(
    SELECT 1I

    UNION ALL

    SELECT I+I+1
    FROM M
    WHERE I+I+1 <= @I
)

SELECT
    @T+=CAST(@I/I AS VARCHAR),
    @I %= I
FROM M
ORDER BY I DESC

SELECT @T