# Number of values between input and next highest square

Given a positive square number as input. Output the number of values between the input and next highest square.

### Example

Input: 1

Output: 2

Reason: The numbers 2 and 3 are between 1 and 4, the next highest square

Input: 4

Output: 4

Reason: The numbers 5, 6, 7, 8 are between 4 and 9

• What range of input values do we have to support? Commented Jun 15, 2017 at 17:08
• I think this would have been more interesting if the input didn't have to be a square.
– xnor
Commented Jun 15, 2017 at 17:24
• @xnor Hindsight, I definitely agree. Commented Jun 15, 2017 at 18:01

# 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))

• My brain has literally been flak'd by this, nice job. Commented Jun 16, 2017 at 20:52

## 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.

• How about 2√#& ? Commented Jul 22, 2017 at 16:37

## 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).

• Oh I missed the whole "input will be a square" oops. Commented Jun 15, 2017 at 17:22
• @JonathanAllan Me too. Weird spec IMO. Commented Jun 15, 2017 at 17:24
• Are there any squares that can't be represented exactly in floating point? Commented Jun 15, 2017 at 18:05
• @Christian Sure, floating-point numbers are fixed in size, so there is only a finite number of values they can represent. Commented Jun 15, 2017 at 18:06
• @MartinEnder In that case, given Jelly's support for arbitrary-precision integers and the spec's lack of an upper bound, I vote it should support all valid inputs. Commented Jun 15, 2017 at 18:11

# Julia 0.5, 8 bytes

!n=2n^.5


Try it online!

# 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


# 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.

½‘Ḟ²’_


Try it online!

# Japt, 5 3 bytes

¬*2


Try it online!

Square root of the input, then multiply by 2.

# 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.

# Mathematica, 17 bytes

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


Try it online!

# 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.

# 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

• Btw, the input will always be a square itself. Commented Jun 15, 2017 at 17:12
• @JonathanAllan Fixed Commented Jun 15, 2017 at 17:14
• @MartinEnder I totally misread the challenge, then... in the interest of not copying your answer (since it's obvious now why that works) I'll leave this one up. Commented Jun 15, 2017 at 17:15

# Python 3, 16 bytes

lambda n:2*n**.5


Try it online!

# Ohm, 2 bytes

√d


Try it online!

## JavaScript ES6, 10 bytes

n=>n**.5*2


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

• Then that's ES7 rather than ES6...
– Neil
Commented Jun 15, 2017 at 20:00

# 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.

# TI-Basic, 3 bytes

2√(Ans


Simplest approach...

# 05AB1E, 2 bytes

t·


Try it online!

Another port of Martin Ender's submission ...

• Welcome to PPCG! Commented Jun 27, 2017 at 16:00

+?
_
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
^2   Square
-1   Subtract 1
-G   Subtract the input
O    Output as number

• I had the same logic for my original QBIC answer, but there's a shorter way. Commented Jun 16, 2017 at 7:54

# 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.

• You can replace 2^ by U (and this worked in version 20.1.1, which was the most recent at the time of the challenge, so the answer it would be eligible even by our old standard) Commented May 27, 2019 at 9:40
• Thanks Luis! I'm surprised that my naive approach only wasted 1 character relative to the MATL master. :) Commented Jul 17, 2019 at 11:25

# Pari/GP, 9 bytes

n->2*n^.5


Try it online!

# PHP, 44 bytes

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


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    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.


# Go, 56 bytes

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


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


# Actually, 3 bytes

√2*


Try it online!

# 05AB1E, 4 3 bytes

Crossed out 4 is still 4 :c

t2*


Try it online!

# T-SQL, 22 bytes

SELECT 2*SQRT(a)FROM t


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

# 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.

(*2).sqrt


Try it online

Input and output will be treated as Float values.

# Noether, 7 bytes

I.5^2*P


Try it here!

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