Skip to main content
added 70 characters in body
Source Link

#T-SQL, 164 156 155 154154 159 bytes (-1 byte. Thanks Jonathan!)   

(-1 more because why do I have trailing spaces on lines? SMH)

(+5 realized my golfing broke things)

create function b(@ int) returns int as begin declare @b varchar=''varchar(max)='',@i int=@ while @>0SELECT @b=cast(@%2 as varchar)+@b,@/=2 return cast(@b as int)/@i end 

I don't know why I keep coming back to these questions where I'm supposed to convert to Binary... T-SQL doesn't know how to do that right.

In any case, here's a SQLFiddle.

Un-golfed:

create function binarySquare(@id int) returns int as BEGIN 

Most of this stuff is required to write a function in T-SQL, as far as I'm aware.

 declare @bin nvarchar(max) = '' 

Create a blank string that we're going to store as our binary number.

 declare @id2 int = @id 

Save the input value for use at the end. It seems like there should be a way to use the original input even if we change the value, but I can't find one.

 while @id>0 BEGIN SET @bin = cast(@id%2 as varchar(1)) + @bin 

So we take our original input, MOD it with 2 to find the remainder, and that's going to be our next smallest digit. For example, 5%2 = 1

 SET @id = @id/2 

Then we take our number, and divide it in half. Because it's an int type, it rounds it down to the nearest whole number, so 5/2 = 2. END We then loop through this until the value is 0. So we end up with 5%2 = 1 5/2 = 2 2%2 = 0 2/2 = 1 1%2 = 1 1/2 = 0 which gives us our binary string value of 101.

 declare @binNum int = (SELECT cast(@bin as int)) 

We take our binary string and convert it back to an int again.

 return @binNum/@id2 

We return our binary string int divided by our original value, per the origin of the question.

END 

#T-SQL, 164 156 155 154 bytes (-1 byte. Thanks Jonathan!)  (-1 more because why do I have trailing spaces on lines? SMH)

create function b(@ int) returns int as begin declare @b varchar='',@i int=@ while @>0SELECT @b=cast(@%2 as varchar)+@b,@/=2 return cast(@b as int)/@i end 

I don't know why I keep coming back to these questions where I'm supposed to convert to Binary... T-SQL doesn't know how to do that right.

In any case, here's a SQLFiddle.

Un-golfed:

create function binarySquare(@id int) returns int as BEGIN 

Most of this stuff is required to write a function in T-SQL, as far as I'm aware.

 declare @bin nvarchar(max) = '' 

Create a blank string that we're going to store as our binary number.

 declare @id2 int = @id 

Save the input value for use at the end. It seems like there should be a way to use the original input even if we change the value, but I can't find one.

 while @id>0 BEGIN SET @bin = cast(@id%2 as varchar(1)) + @bin 

So we take our original input, MOD it with 2 to find the remainder, and that's going to be our next smallest digit. For example, 5%2 = 1

 SET @id = @id/2 

Then we take our number, and divide it in half. Because it's an int type, it rounds it down to the nearest whole number, so 5/2 = 2. END We then loop through this until the value is 0. So we end up with 5%2 = 1 5/2 = 2 2%2 = 0 2/2 = 1 1%2 = 1 1/2 = 0 which gives us our binary string value of 101.

 declare @binNum int = (SELECT cast(@bin as int)) 

We take our binary string and convert it back to an int again.

 return @binNum/@id2 

We return our binary string int divided by our original value, per the origin of the question.

END 

#T-SQL, 164 156 155 154 159 bytes (-1 byte. Thanks Jonathan!) 

(-1 more because why do I have trailing spaces on lines? SMH)

(+5 realized my golfing broke things)

create function b(@ int) returns int as begin declare @b varchar(max)='',@i int=@ while @>0SELECT @b=cast(@%2 as varchar)+@b,@/=2 return cast(@b as int)/@i end 

I don't know why I keep coming back to these questions where I'm supposed to convert to Binary... T-SQL doesn't know how to do that right.

In any case, here's a SQLFiddle.

Un-golfed:

create function binarySquare(@id int) returns int as BEGIN 

Most of this stuff is required to write a function in T-SQL, as far as I'm aware.

 declare @bin nvarchar(max) = '' 

Create a blank string that we're going to store as our binary number.

 declare @id2 int = @id 

Save the input value for use at the end. It seems like there should be a way to use the original input even if we change the value, but I can't find one.

 while @id>0 BEGIN SET @bin = cast(@id%2 as varchar(1)) + @bin 

So we take our original input, MOD it with 2 to find the remainder, and that's going to be our next smallest digit. For example, 5%2 = 1

 SET @id = @id/2 

Then we take our number, and divide it in half. Because it's an int type, it rounds it down to the nearest whole number, so 5/2 = 2. END We then loop through this until the value is 0. So we end up with 5%2 = 1 5/2 = 2 2%2 = 0 2/2 = 1 1%2 = 1 1/2 = 0 which gives us our binary string value of 101.

 declare @binNum int = (SELECT cast(@bin as int)) 

We take our binary string and convert it back to an int again.

 return @binNum/@id2 

We return our binary string int divided by our original value, per the origin of the question.

END 
added 50 characters in body
Source Link

#T-SQL, 164 156156 155 154 bytes (-1 byte. Thanks Jonathan!) (-1 more because why do I have trailing spaces on lines? SMH)

create function b(@ int) returns int  as begin declare @b varchar='',@i int=@ while @>0 SELECT@>0SELECT @b=cast(@%2 as varchar)+@b,@/=2 return cast(@b as int)/@i end 

I don't know why I keep coming back to these questions where I'm supposed to convert to Binary... T-SQL doesn't know how to do that right.

In any case, here's a SQLFiddle.

Un-golfed:

create function binarySquare(@id int) returns int as BEGIN 

Most of this stuff is required to write a function in T-SQL, as far as I'm aware.

 declare @bin nvarchar(max) = '' 

Create a blank string that we're going to store as our binary number.

 declare @id2 int = @id 

Save the input value for use at the end. It seems like there should be a way to use the original input even if we change the value, but I can't find one.

 while @id>0 BEGIN SET @bin = cast(@id%2 as varchar(1)) + @bin 

So we take our original input, MOD it with 2 to find the remainder, and that's going to be our next smallest digit. For example, 5%2 = 1

 SET @id = @id/2 

Then we take our number, and divide it in half. Because it's an int type, it rounds it down to the nearest whole number, so 5/2 = 2. END We then loop through this until the value is 0. So we end up with 5%2 = 1 5/2 = 2 2%2 = 0 2/2 = 1 1%2 = 1 1/2 = 0 which gives us our binary string value of 101.

 declare @binNum int = (SELECT cast(@bin as int)) 

We take our binary string and convert it back to an int again.

 return @binNum/@id2 

We return our binary string int divided by our original value, per the origin of the question.

END 

#T-SQL, 164 156 bytes

create function b(@ int) returns int  as begin declare @b varchar='',@i int=@ while @>0 SELECT @b=cast(@%2 as varchar)+@b,@/=2 return cast(@b as int)/@i end 

I don't know why I keep coming back to these questions where I'm supposed to convert to Binary... T-SQL doesn't know how to do that right.

In any case, here's a SQLFiddle.

Un-golfed:

create function binarySquare(@id int) returns int as BEGIN 

Most of this stuff is required to write a function in T-SQL, as far as I'm aware.

 declare @bin nvarchar(max) = '' 

Create a blank string that we're going to store as our binary number.

 declare @id2 int = @id 

Save the input value for use at the end. It seems like there should be a way to use the original input even if we change the value, but I can't find one.

 while @id>0 BEGIN SET @bin = cast(@id%2 as varchar(1)) + @bin 

So we take our original input, MOD it with 2 to find the remainder, and that's going to be our next smallest digit. For example, 5%2 = 1

 SET @id = @id/2 

Then we take our number, and divide it in half. Because it's an int type, it rounds it down to the nearest whole number, so 5/2 = 2. END We then loop through this until the value is 0. So we end up with 5%2 = 1 5/2 = 2 2%2 = 0 2/2 = 1 1%2 = 1 1/2 = 0 which gives us our binary string value of 101.

 declare @binNum int = (SELECT cast(@bin as int)) 

We take our binary string and convert it back to an int again.

 return @binNum/@id2 

We return our binary string int divided by our original value, per the origin of the question.

END 

#T-SQL, 164 156 155 154 bytes (-1 byte. Thanks Jonathan!) (-1 more because why do I have trailing spaces on lines? SMH)

create function b(@ int) returns int as begin declare @b varchar='',@i int=@ while @>0SELECT @b=cast(@%2 as varchar)+@b,@/=2 return cast(@b as int)/@i end 

I don't know why I keep coming back to these questions where I'm supposed to convert to Binary... T-SQL doesn't know how to do that right.

In any case, here's a SQLFiddle.

Un-golfed:

create function binarySquare(@id int) returns int as BEGIN 

Most of this stuff is required to write a function in T-SQL, as far as I'm aware.

 declare @bin nvarchar(max) = '' 

Create a blank string that we're going to store as our binary number.

 declare @id2 int = @id 

Save the input value for use at the end. It seems like there should be a way to use the original input even if we change the value, but I can't find one.

 while @id>0 BEGIN SET @bin = cast(@id%2 as varchar(1)) + @bin 

So we take our original input, MOD it with 2 to find the remainder, and that's going to be our next smallest digit. For example, 5%2 = 1

 SET @id = @id/2 

Then we take our number, and divide it in half. Because it's an int type, it rounds it down to the nearest whole number, so 5/2 = 2. END We then loop through this until the value is 0. So we end up with 5%2 = 1 5/2 = 2 2%2 = 0 2/2 = 1 1%2 = 1 1/2 = 0 which gives us our binary string value of 101.

 declare @binNum int = (SELECT cast(@bin as int)) 

We take our binary string and convert it back to an int again.

 return @binNum/@id2 

We return our binary string int divided by our original value, per the origin of the question.

END 
Source Link

#T-SQL, 164 156 bytes

create function b(@ int) returns int as begin declare @b varchar='',@i int=@ while @>0 SELECT @b=cast(@%2 as varchar)+@b,@/=2 return cast(@b as int)/@i end 

I don't know why I keep coming back to these questions where I'm supposed to convert to Binary... T-SQL doesn't know how to do that right.

In any case, here's a SQLFiddle.

Un-golfed:

create function binarySquare(@id int) returns int as BEGIN 

Most of this stuff is required to write a function in T-SQL, as far as I'm aware.

 declare @bin nvarchar(max) = '' 

Create a blank string that we're going to store as our binary number.

 declare @id2 int = @id 

Save the input value for use at the end. It seems like there should be a way to use the original input even if we change the value, but I can't find one.

 while @id>0 BEGIN SET @bin = cast(@id%2 as varchar(1)) + @bin 

So we take our original input, MOD it with 2 to find the remainder, and that's going to be our next smallest digit. For example, 5%2 = 1

 SET @id = @id/2 

Then we take our number, and divide it in half. Because it's an int type, it rounds it down to the nearest whole number, so 5/2 = 2. END We then loop through this until the value is 0. So we end up with 5%2 = 1 5/2 = 2 2%2 = 0 2/2 = 1 1%2 = 1 1/2 = 0 which gives us our binary string value of 101.

 declare @binNum int = (SELECT cast(@bin as int)) 

We take our binary string and convert it back to an int again.

 return @binNum/@id2 

We return our binary string int divided by our original value, per the origin of the question.

END