9
\$\begingroup\$

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

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

43 Answers 43

8
\$\begingroup\$

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

\$\endgroup\$
  • 1
    \$\begingroup\$ Oh I missed the whole "input will be a square" oops. \$\endgroup\$ – Jonathan Allan Jun 15 '17 at 17:22
  • 1
    \$\begingroup\$ @JonathanAllan Me too. Weird spec IMO. \$\endgroup\$ – Digital Trauma Jun 15 '17 at 17:24
  • \$\begingroup\$ Are there any squares that can't be represented exactly in floating point? \$\endgroup\$ – scatter Jun 15 '17 at 18:05
  • \$\begingroup\$ @Christian Sure, floating-point numbers are fixed in size, so there is only a finite number of values they can represent. \$\endgroup\$ – Martin Ender Jun 15 '17 at 18:06
  • \$\begingroup\$ @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. \$\endgroup\$ – scatter Jun 15 '17 at 18:11
12
\$\begingroup\$

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))
\$\endgroup\$
  • 2
    \$\begingroup\$ My brain has literally been flak'd by this, nice job. \$\endgroup\$ – Magic Octopus Urn Jun 16 '17 at 20:52
9
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ How about 2√#& ? \$\endgroup\$ – chyanog Jul 22 '17 at 16:37
3
\$\begingroup\$

Julia 0.5, 8 bytes

!n=2n^.5

Try it online!

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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!

\$\endgroup\$
2
\$\begingroup\$

Japt, 5 3 bytes

¬*2

Try it online!

Square root of the input, then multiply by 2.

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

Mathematica, 17 bytes

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ Btw, the input will always be a square itself. \$\endgroup\$ – Martin Ender Jun 15 '17 at 17:12
  • 1
    \$\begingroup\$ @JonathanAllan Fixed \$\endgroup\$ – scatter Jun 15 '17 at 17:14
  • \$\begingroup\$ @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. \$\endgroup\$ – scatter Jun 15 '17 at 17:15
1
\$\begingroup\$

Python 3, 16 bytes

lambda n:2*n**.5

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Ohm, 2 bytes

√d

Try it online!

\$\endgroup\$
1
\$\begingroup\$

JavaScript ES6, 10 bytes

n=>n**.5*2

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

\$\endgroup\$
  • 2
    \$\begingroup\$ Then that's ES7 rather than ES6... \$\endgroup\$ – Neil Jun 15 '17 at 20:00
1
\$\begingroup\$

TI-Basic, 3 bytes

2√(Ans

Simplest approach...

\$\endgroup\$
1
\$\begingroup\$

05AB1E, 2 bytes

Try it online!

Another port of Martin Ender's submission ...

\$\endgroup\$
  • \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ – Erik the Outgolfer Jun 27 '17 at 16:00
1
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ I had the same logic for my original QBIC answer, but there's a shorter way. \$\endgroup\$ – steenbergh Jun 16 '17 at 7:54
1
\$\begingroup\$

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.
\$\endgroup\$
  • 1
    \$\begingroup\$ 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) \$\endgroup\$ – Luis Mendo May 27 at 9:40
  • 1
    \$\begingroup\$ Thanks Luis! I'm surprised that my naive approach only wasted 1 character relative to the MATL master. :) \$\endgroup\$ – DrQuarius Jul 17 at 11:25
0
\$\begingroup\$

Pari/GP, 9 bytes

n->2*n^.5

Try it online!

\$\endgroup\$
0
\$\begingroup\$

PHP, 44 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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.
\$\endgroup\$
0
\$\begingroup\$

Go, 56 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

Actually, 3 bytes

√2*

Try it online!

\$\endgroup\$
0
\$\begingroup\$

05AB1E, 4 3 bytes

Crossed out 4 is still 4 :c

t2*

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

T-SQL, 22 bytes

SELECT 2*SQRT(a)FROM t

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

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

Haskell, 9 bytes

(*2).sqrt

Try it online

Input and output will be treated as Float values.

\$\endgroup\$
0
\$\begingroup\$

Noether, 7 bytes

I.5^2*P

Try it here!

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

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.