| Bytes | Lang | Time | Link |
|---|---|---|---|
| 003 | Uiua | 240913T212517Z | nyxbird |
| 006 | Husk | 240913T082129Z | int 21h |
| 007 | MATL | 170619T030810Z | DrQuariu |
| 018 | Cubix | 170615T191705Z | Giuseppe |
| 009 | Excel | 171108T124421Z | Wernisch |
| 004 | J | 170721T213126Z | Richard |
| 020 | Add++ | 170615T170757Z | caird co |
| 098 | Plain English | 170628T041619Z | Jasper |
| 002 | 05AB1E | 170627T155434Z | space ju |
| 003 | NewStack | 170626T191723Z | Graviton |
| 006 | Dyalog APL | 170619T140628Z | Adalynn |
| 042 | C# | 170619T091644Z | Erlantz |
| 020 | BrainFlak | 170619T012742Z | Wheat Wi |
| 007 | Charcoal | 170619T012541Z | ASCII-on |
| 004 | Pyth | 170616T223412Z | jacoblaw |
| 010 | CJam | 170616T213002Z | Esolangi |
| nan | 170616T173205Z | Brad Gil | |
| 024 | C# | 170616T081838Z | TheLetha |
| 026 | S.I.L.O.S | 170615T214248Z | Rohan Jh |
| 007 | Noether | 170615T213158Z | Beta Dec |
| 009 | Haskell | 170615T212136Z | Antisthe |
| 017 | Java OpenJDK 9 / JShell | 170615T212039Z | David Co |
| 003 | TIBasic | 170615T210915Z | Timtech |
| 022 | TSQL | 170615T200601Z | BradC |
| 021 | Retina | 170615T185631Z | Neil |
| 010 | JavaScript ES6 | 170615T184337Z | Downgoat |
| 003 | 05AB1E | 170615T175234Z | totallyh |
| 003 | Actually | 170615T182231Z | totallyh |
| 002 | Ohm | 170615T181750Z | totallyh |
| 003 | Japt | 170615T172952Z | Oliver |
| 022 | BrainFlak | 170615T173844Z | DJMcMayh |
| 008 | Julia 0.5 | 170615T175358Z | Dennis |
| 009 | QBIC | 170615T171017Z | steenber |
| 056 | Go | 170615T173652Z | totallyh |
| 010 | Alice | 170615T172726Z | Martin E |
| 006 | Jelly | 170615T171057Z | Jonathan |
| 005 | dc | 170615T170316Z | Digital |
| 016 | Python 3 | 170615T172339Z | ovs |
| 044 | PHP | 170615T172254Z | Jör |
| 009 | Pari/GP | 170615T171719Z | alephalp |
| 008 | Mathematica | 170615T170401Z | Martin E |
| 007 | Jelly | 170615T170801Z | Christia |
| 002 | Jelly | 170615T170606Z | Martin E |
| 010 | Octave | 170615T170404Z | Stewie G |
| 017 | Mathematica | 170615T170318Z | ZaMoC |
Husk, 6 bytes
←-¹□→√
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
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;;/
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
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.
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.
Brain-Flak, 20 bytes
Shout out to DJMcMayhem's amazing (albiet slightly longer) answer here
{({}()[({}()())])}{}
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·⁵
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
If it had to work with positive numbers that aren't squares:
{+($_^..^(.sqrt.Int+1)²)}
C#, 24 bytes
n=>System.Math.Sqrt(n)*2
S.I.L.O.S, 26 bytes
readIO
i=2*i^.5
printInt i
Straightforward port of Martin Ender's Mathematica answer.
Noether, 7 bytes
I.5^2*P
Just the same as every other answer: outputs two times the square root.
Java (OpenJDK 9) / JShell, 17 bytes
n->2*Math.sqrt(n)
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...
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
Brain-Flak, 38, 22 bytes
{([[]](({})))}{}([]<>)
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))
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
Alice, 10 bytes
2/*<ER
o@i
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.
dc, 5
?2*vp
Previously I misread the question. This version works for any positive integer input, not just perfect squares:
dc, 12
?dv1+d*1-r-p
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²Ṫ_‘
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
½Ḥ
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
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.