# 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? Jun 15 '17 at 17:08
• I think this would have been more interesting if the input didn't have to be a square.
– xnor
Jun 15 '17 at 17:24
• @xnor Hindsight, I definitely agree. Jun 15 '17 at 18:01

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

# C#, 24 bytes

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


# 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

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


# Pyth, 4 bytes

y@Q2


Explanation:

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


# Charcoal, 7 bytes

Ｉ×²ＸＮ·⁵


Try it online!

## Explanation

Ｉ         Cast (int -> str)
×²       Multiplied by 2
ＸＮ·⁵ N to the power of .5 (Square root of N)

• @DestructibleLemon No? The output is always a whole number (something.0) Jun 19 '17 at 6:53
• counter example: 66 and 2. wait a minute I didn't read the challenge very well nvm Jun 19 '17 at 6:59
• @DestructibleLemon "Given a positive square number as input" Jun 19 '17 at 6:59

C#, 42 bytes

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


Try it online!

• Can't it just the last assignment just be b=(b+1)*(b+1)-n-1? Jun 19 '17 at 13:42
• You could do this: int b=1+(int)Math.sqrt(n);b=b*b-n-1; Doesn't this need to be a Lambda or something? Jun 19 '17 at 13:58

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

# 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

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

# J, 4 bytes

+:%:

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


# Excel, 9 bytes

=2*A1^0.5


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

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


# 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