g | x | w | all
Bytes Lang Time Link
003Uiua240913T212517Znyxbird
006Husk240913T082129Zint 21h
007MATL170619T030810ZDrQuariu
018Cubix170615T191705ZGiuseppe
009Excel171108T124421ZWernisch
004J170721T213126ZRichard
020Add++170615T170757Zcaird co
098Plain English170628T041619ZJasper
00205AB1E170627T155434Zspace ju
003NewStack170626T191723ZGraviton
006Dyalog APL170619T140628ZAdalynn
042C#170619T091644ZErlantz
020BrainFlak170619T012742ZWheat Wi
007Charcoal170619T012541ZASCII-on
004Pyth170616T223412Zjacoblaw
010CJam170616T213002ZEsolangi
nan170616T173205ZBrad Gil
024C#170616T081838ZTheLetha
026S.I.L.O.S170615T214248ZRohan Jh
007Noether170615T213158ZBeta Dec
009Haskell170615T212136ZAntisthe
017Java OpenJDK 9 / JShell170615T212039ZDavid Co
003TIBasic170615T210915ZTimtech
022TSQL170615T200601ZBradC
021Retina170615T185631ZNeil
010JavaScript ES6170615T184337ZDowngoat
00305AB1E170615T175234Ztotallyh
003Actually170615T182231Ztotallyh
002Ohm170615T181750Ztotallyh
003Japt170615T172952ZOliver
022BrainFlak170615T173844ZDJMcMayh
008Julia 0.5170615T175358ZDennis
009QBIC170615T171017Zsteenber
056Go170615T173652Ztotallyh
010Alice170615T172726ZMartin E
006Jelly170615T171057ZJonathan
005dc170615T170316ZDigital
016Python 3170615T172339Zovs
044PHP170615T172254ZJör
009Pari/GP170615T171719Zalephalp
008Mathematica170615T170401ZMartin E
007Jelly170615T170801ZChristia
002Jelly170615T170606ZMartin E
010Octave170615T170404ZStewie G
017Mathematica170615T170318ZZaMoC

Uiua, 3 bytes

×2√

Try it!

The of the input ×2.

Husk, 6 bytes

←-¹□→√

Try it online!

This solution is so clear, you wouldn't even need to know Husk to read it (from right to left):

The number is input implicitly, take square root , increase by one , square the result , from this subtract - the initial value ¹ and decrement by 1 .

MATL (8 7 bytes)

I'm sure this can be reduced significantly (edit: thanks Luis), but a naive solution is:

X^QUG-q

Try it online!

Explanation:

X^   % Take the square root of the input (an integer)
QU  % Square the next integer to find the next square
G-   % Subtract the input to find the difference
q    % Decrement solution by 1 to count only "in between" values.

Cubix, 22 18 bytes

+@UOI0),c?;^..u;;/

Try it online!

Watch it online!

A port of Martin Ender's solution. Since Cubix only supports integer arithmetic, computing the square root is literally just trying every number until we hit the square root, so this approach only works since the input is a perfect square.

Expands to the following cube:

    + @
    U O
I 0 ) , c ? ; ^
. . u ; ; / . .
    . .
    . .

I       # read in input [n^2]
0       # push 0 [n^2, 0]
Main loop:
)       # increment [n^2, 1]
,       # divide, rounding to zero [n^2, 1, n^2]
c       # bitwise xor -- zero if equal, positive if not
?       # turn right if positive, go straight if zero [n^2,1,n^2,___]
 nonzero branch:
  /      # reflect IP direction
  ;;     # pop top two off stack [n^2, 1]
  u      # right-hand U-turn
  (re-enter main loop)
 zero branch: stack is [n^2, n, n, 0]
  ;      # pop top of stack [n^2, n, n]
  ^      # go north
  +      # add top two elements [n^2, n, n, 2n]
  U      # left-hand u-turn
  O      # output as int
  @      # terminate program

Excel, 9 bytes

=2*A1^0.5

To handle all cases (not only squares): 21 bytes

=INT(A1^0.5+1)^2-A1-1

J, 4 bytes

+:%:

    e.g. +:%: 9  ====>  6

Add++, 22 20 bytes

+?
_
S
+1
^2
-1
-G
O

Try it online!

Do you want to know how it works? Well, fear not! I'm here to educate you!

+?   Add the input to x (the accumulator)
_    Store the input in the input list
S    Square root
+1   Add 1
^2   Square
-1   Subtract 1
-G   Subtract the input
O    Output as number

Plain English 129 98 bytes

To put a n number's d into a r number:
Put the square root of the n in the r.
Add the r to the r.

Ungolfed:

To put a number's delta into a result number:
Put the square root of the number in the result.
Add the result to the result.

The Plain English IDE is available at github.com/Folds/english. The IDE runs on Windows. It compiles to 32-bit x86 code.

05AB1E, 2 bytes

Try it online!

Another port of Martin Ender's submission ...

NewStack, 3 bytes

2√ᵢ

The breakdown:

 √ᵢ    Append the square root of the input.
2      Multiply it by two.

Why this works:

To find how many numbers N are in between the square number x and the next square number m, we first need to find a formula for m.

Since x is a square number, √x will be an integer. And √x + 1 will equal √m.

Therefore m = (√x + 1)^2 = x + 2√x + 1

The amount of numbers in between x and m will just equal their difference minus 1.

N = m - x - 1 = (x + 2√x + 1) - x - 1

So we've concluded

N = 2√x

Dyalog APL, 6 bytes

2×*∘.5

I can't believe this hasn't been posted yet. A variant with the same byte count:

+⍨*∘.5

Both of them work by doubling the square root.

C#, 42 bytes

int b=(int)Math.Sqrt(n);b=(b+1)*(b+1)-n-1;

Try it online!

Brain-Flak, 20 bytes

Shout out to DJMcMayhem's amazing (albiet slightly longer) answer here

{({}()[({}()())])}{}

Try it online!

Explanation

This code works by counting down from the square number by odd increments. Since every square is the sum of consecutive odd numbers this will reach 0 in n1/2 steps. The trick here is we actually keep track of our steps in an even number and use a static () to offset it to the appropriate odd number. Since the answer is 2n1/2, this even number will be our answer. So when we reach 0 we remove the zero and our answer is sitting there on the stack.

Charcoal, 7 bytes

IײXN·⁵

Try it online!

Explanation

I         Cast (int -> str)
  ײ       Multiplied by 2
    XN·⁵ N to the power of .5 (Square root of N)

Pyth, 4 bytes

y@Q2

Explanation:

     # Implicit copy input to Q
y    # Multiply by 2
 @Q2 # Square-root of Q
     # Implicit print

CJam, 11 10 bytes

ri_mQ)_*-~

Explanation:

ri e# Read integer: | 16
_  e# Duplicate:    | 16 16
mQ e# Square root:  | 16 4
)  e# Increment:    | 16 5
_* e# Square:       | 16 25
-  e# Subtract:     | -9
~  e# Bitwise NOT:  | 8

Alternative Solution, 6 bytes

rimQ2*

Explanation:

ri e# Read integer: | 16
mQ e# Square root:  | 4
2* e# Times 2:      | 8

Perl 6, 12 bytes

*.sqrt.Int*2

Try it


If it had to work with positive numbers that aren't squares:

{+($_^..^(.sqrt.Int+1)²)}

Try it

C#, 24 bytes

n=>System.Math.Sqrt(n)*2

S.I.L.O.S, 26 bytes

readIO
i=2*i^.5
printInt i

Try it online!

Straightforward port of Martin Ender's Mathematica answer.

Noether, 7 bytes

I.5^2*P

Try it here!

Just the same as every other answer: outputs two times the square root.

Haskell, 9 bytes

(*2).sqrt

Try it online

Input and output will be treated as Float values.

Java (OpenJDK 9) / JShell, 17 bytes

n->2*Math.sqrt(n)

Try it online!

Note: This would require import java.util.function.*; to get IntFunction<T> in Java 8 or Java 9, but the java.util.function package is imported by default in JShell.

TI-Basic, 3 bytes

2√(Ans

Simplest approach...

T-SQL, 22 bytes

SELECT 2*SQRT(a)FROM t

Input is via a pre-existing table, per our standards.

Retina, 21 bytes

.+
$*
(^1?|11\1)+
$1

Try it online! Explanation: Works by taking the square root of the number based on @MartinEnder's triangular number solver. After matching the square number, $1 is the difference between the square number and the previous square number, in unary. We want the next difference, but exclusive, which is just 1 more. To achieve this, we count the number of null strings in $1.

JavaScript ES6, 10 bytes

n=>n**.5*2

Try it online! Math.sqrt is pretty long which is why we use **.5

05AB1E, 4 3 bytes

Crossed out 4 is still 4 :c

t2*

Try it online!

Actually, 3 bytes

√2*

Try it online!

Ohm, 2 bytes

√d

Try it online!

Japt, 5 3 bytes

¬*2

Try it online!

Square root of the input, then multiply by 2.

Brain-Flak, 38, 22 bytes

{([[]](({})))}{}([]<>)

Try it online!

I'm very proud of this answer. IMO, one of my best brain-flak golfs.

How does it work?

As many other users have pointed out, the answer is simply sqrt(n) * 2. However, calculating the square root in brain-flak is very very nontrivial. Since we know the input will always be a square, we can optimize. So we write a loop that subtracts

1, 3, 5, 7, 9...

from the input, and track how many times it runs. Once it hits 0, the answer is simply the last number we subtracted minus one.

Originally, I had pushed a counter on to the other stack. However, we can use the main stack itself as a counter, by increasing the stack height.

#While TOS (top of stack, e.g. input) != 0:
{

    #Push:
    (

      #The negative of the height of the stack (since we're subtracting)
      [[]]

      #Plus the TOS pushed twice. This is like incrementing a counter by two
      (({}))
    )

#Endwhile
}

#Pop one value off the main stack (or in other words, decrement our stack-counter)
{}

#And push the height of the stack onto the alternate stack
([]<>)

In python-y pseudocode, this is basically the following algorithm:

l = [input]
while l[-1] != 0:   #While the back of the list is nonzero
    old_len = len(l)
    l.append(l[-1])
    l.append(l[-1] - old_len)

l.pop()

print(len(l))

Julia 0.5, 8 bytes

!n=2n^.5

Try it online!

QBIC, 19 9 bytes

?sqr(:)*2

Saved a bunch by copying @MartinEnder's approach.

No TIO link for QBIC, unfortunately.

Explanation

?          PRINT
 sqr( )    The square root of
     :     the input
        *2 doubled

Go, 56 bytes

import."math"
func f(n float64)float64{return 2*Sqrt(n)}

Try it online!

Alice, 10 bytes

2/*<ER
o@i

Try it online!

Explanation

Again, computes 2 sqrt(n). The layout saves two bytes over the standard solution:

/o
\i@/2RE2*

Breakdown of the code, excluding redirection of the IP:

2    Push 2 for later.
i    Read all input.
i    Try reading more input, pushes "".
2    Push 2.
R    Negate to get -2.
E    Implicitly discard the empty string and convert the input to an integer.
     Then take the square root of the input. E is usually exponentiation, but
     negative exponents are fairly useless in a language that only understands
     integers, so negative exponents are interpreted as roots instead.
*    Multiply the square root by 2.
o    Output the result.
@    Terminate the program.

Jelly,  7  6 bytes

I missed the "input will be square" caveat, but this will work for all non-negative integers... Martin Ender already gave the 2 byte solution.

½‘Ḟ²’_

A monadic link returning the count.

Try it online!

dc, 5

?2*vp

Try it online.


Previously I misread the question. This version works for any positive integer input, not just perfect squares:

dc, 12

?dv1+d*1-r-p

Try it online.

Python 3, 16 bytes

lambda n:2*n**.5

Try it online!

PHP, 44 bytes

<?=count(range($argn,(sqrt($argn)+1)**2))-2;

Try it online!

Pari/GP, 9 bytes

n->2*n^.5

Try it online!

Mathematica, 8 bytes

2Sqrt@#&

Try it online! (Using Mathics.)

The difference between n2 and (n+1)2 is always 2n+1 but we just want the values between them excluding both ends, which is 2n.

This can potentially be shortened to 2#^.5& depending on precision requirements.

Jelly, 7 bytes

½‘R²Ṫ_‘

Try it online!

Explanation:

½‘R²Ṫ_    Input:              40
½         Square root         6.32455532...
 ‘        Increment           7.32455532...
  R       Range               [1, 2, 3, 4, 5, 6, 7]
   ²      Square              [1, 4, 9, 16, 25, 36, 49]
    Ṫ     Tail                49
     _‘   Subtract input+1    8

Jelly, 2 bytes

½Ḥ

Try it online!

Port of my Mathematica answer (take square root, then double). This is limited to inputs which can be represented exactly as a floating-point number. If that's an issue, the three-byte solution ƽḤ works for arbitrary squares (which Dennis posted first but then deleted).

Octave, 25 10 bytes

@(n)2*n^.5

Try it online!

Saved 15 bytes by using Martin's much better approach. The range consists of 2*sqrt(n) elements. The function does exactly that: Multiplies 2 with the root of the input.

Mathematica, 17 bytes

(Sqrt@#+1)^2-#-1&

Try it online!