11
\$\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\$
3
  • 1
    \$\begingroup\$ What range of input values do we have to support? \$\endgroup\$ 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

1
2
0
\$\begingroup\$

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.

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

C#, 24 bytes

n=>System.Math.Sqrt(n)*2
\$\endgroup\$
0
\$\begingroup\$

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

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

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

Pyth, 4 bytes

y@Q2

Explanation:

     # Implicit copy input to Q
y    # Multiply by 2
 @Q2 # Square-root of Q
     # Implicit print
\$\endgroup\$
0
\$\begingroup\$

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)
\$\endgroup\$
3
  • \$\begingroup\$ @DestructibleLemon No? The output is always a whole number (something.0) \$\endgroup\$
    – ASCII-only
    Jun 19 '17 at 6:53
  • \$\begingroup\$ counter example: 66 and 2. wait a minute I didn't read the challenge very well nvm \$\endgroup\$ Jun 19 '17 at 6:59
  • \$\begingroup\$ @DestructibleLemon "Given a positive square number as input" \$\endgroup\$
    – ASCII-only
    Jun 19 '17 at 6:59
0
\$\begingroup\$

C#, 42 bytes

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

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Can't it just the last assignment just be b=(b+1)*(b+1)-n-1? \$\endgroup\$
    – Adalynn
    Jun 19 '17 at 13:42
  • \$\begingroup\$ 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? \$\endgroup\$
    – Adalynn
    Jun 19 '17 at 13:58
0
\$\begingroup\$

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.

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

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

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

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.

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

J, 4 bytes

+:%:

    e.g. +:%: 9  ====>  6
\$\endgroup\$
0
\$\begingroup\$

Excel, 9 bytes

=2*A1^0.5

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

=INT(A1^0.5+1)^2-A1-1
\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
1
2

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.