# Find the nearest biquadratic number

A biquadratic number is a number that is the fourth power of another integer, for example: 3^4 = 3*3*3*3 = 81

Given an integer as input, output the closest biquadratic number.

Here are the first 15 double-squares:

1, 16, 81, 256, 625, 1296, 2401, 4096, 6561, 10000, 14641, 20736, 28561, 38416, 50625


This is so fewest bytes in each language wins

This is OEIS A000583

• Interesting to note that this will never tie, as the sequence alternates odd and even numbers. – Okx Jul 10 '17 at 9:39
• you could change the name to "Find the nearest zenzizenzic". en.wiktionary.org/wiki/zenzizenzic – Destructible Lemon Jul 10 '17 at 9:41
• @Mayube It necessarily does, because the sequence is just n^4 and n alternates in sign. – Martin Ender Jul 10 '17 at 9:43
• That nomenclature of biquadratic is confusing: before seeing the question contents, I thought it were the 2 x n² numbers: 2, 8, 18, 32, 50, 72, 98, ... – sergiol Jul 10 '17 at 10:35
• Isn't that called "quartic"? (Merriam-Webster, Wiktionary) – Olivier Grégoire Jul 10 '17 at 15:49

# Python 3, 35 bytes

lambda n:int((n**.5-.75)**.5+.5)**4


Try it online!

### How it works

The value n at which the output switches from (k − 1)4 to k4 satisfies √(√n − 3/4) + 1/2 = k, or n = ((k − 1/2)2 + 3/4)2 = (k2k + 1)2 = ((k − 1)4 + k4 + 1)/2, which is exactly the first integer that’s closer to k4.

(Works for all n ≤ 4504699340341245 = (81924 + 81934 − 7)/2 > 252, after which floating-point roundoff starts to break it, even though it works mathematically for all n.)

• You can save a byte with round if you switch to Python 2 which rounds all .5's up. – xnor Jul 10 '17 at 21:22

# Octave, 35 bytes

This challenge needed a convolution-based approach.

@(n)sum(n>conv((1:n).^4,[1 1]/2))^4


Try it online!

### Explanation

The expression (1:n).^4 produces the row vector [1 16 81 256 ... n^4].

This vector is then convolved with [1 1]/2, which is equivalent to computing the sliding average of blocks of size 2. This implicitly assumes that the vector is left- and right-padded with 0. So the first value in the result is 0.5 (average of an implicit 0 and 1), the second is 8.5 (average of 1 and 16), etc.

As an example, for n = 9 the result of conv((1:n).^4,[1 1]/2) is

0.5 8.5 48.5 168.5 440.5 960.5 1848.5 3248.5 5328.5 3280.5


The comparison n>... then yields

1 1 0 0 0 0 0 0 0 0 0


and applying sum(...) gives 2. This means that n exceeds exactly 2 of the mid-points betwen biquadratic numbers (including the additional mid-point 0.5). Finally, ^4 raises this to 4 to yield the result, 16.

• It is even golfier! – flawr Jul 10 '17 at 11:30

f n=snd.minimum$(abs.(n-)<$>)>>=zip$(^4)<$>[1..n]


Try it online!

Explanation:

                                (^4)<$>[1..n] -- creates a list of fourth powers (abs.(n-)<$>)>>=zip               -- creates a list of |n-(4th powers)| and
-- zips it with the 4th powers list
minimum                                   -- finds the minimum
-- (only first tuple entry matters)
snd                                           -- exctracts the second entry (the 4th power)


# MATL, 6 bytes

t:4^Yk


Try it online!

### Explanation

Consider input 9 as an example.

t    % Implicitly input n. Duplicate
% STACK: 9, 9
:    % Range [1 2 ... n]
% STACK: 9, [1 2 3 4 5 6 7 8 9]
4^   % Raise to 4, element-wise
% STACK: 9, [1 16 81 256 625 1296 2401 4096 6561]
Yk   % Closest element. Implicitly display
% STACK: 16


# Neim, 5 bytes

𝐈4𝕎S𝕔


Explanation:

𝐈       Inclusive range [1 .. input]
𝕎    Raise to the  v  power
4                   4th
𝕔  Select the value closest to
S   the input


Try it online!

• This programming language seems to use Unicode characters ("𝕎" and "𝕔"). Such characters typically require more than one byte. Are you sure the 5 characters can be stored using only 5 bytes? – Martin Rosenau Jul 10 '17 at 10:47
• @MartinRosenau Neim uses a custom codepage – Okx Jul 10 '17 at 10:47

## Excel, 25 bytes

=INT((A1^.5-3/4)^.5+.5)^4


Excel updates this to =INT((A1^0.5-3/4)^0.5+0.5)^4

• Just a note on convention for excel: it is the de facto standard that Excel and Excel VBA function that take input from the Excel.ActiveSheet object take them from cell A1 – Taylor Scott Jul 12 '17 at 10:03
• @TaylorScott, thanks for pointing that out. Have updated. – Wernisch Jul 12 '17 at 10:15

## Mathematica, 21 bytes

Nearest[Range@#^4,#]&


# Brachylog, 9 bytes

;I≜+.~^₄∧


Try it online!

### Explanation

;I≜          I = 0 / I = 1 / I = -1 / I = 2 / etc. on backtracking
+.        Output = Input + I
.~^₄     Output = Something to the power 4
∧


## JavaScript (ES7), 42 bytes

x=>(n=x**.25|0,x-(k=n**4)<++n**4-x?k:n**4)


### Recursive version, 44 bytes

f=(x,k,b)=>(a=k**4)>x?a-x>x-b?b:a:f(x,-~k,a)


### Demo

let f =

x=>(n=x**.25|0,x-(k=n**4)<++n**4-x?k:n**4)

console.log(f(16))
console.log(f(48))
console.log(f(49))

# Octave, 37 bytes

@(n)interp1(t=(1:n).^4,t,n,'nearest')


Anonymous function that uses nearest-neighbour interpolation.

Try it online!

• -1 no conv :( – flawr Jul 10 '17 at 10:28
• @flawr Does this make you feel better? – Luis Mendo Jul 10 '17 at 10:39
• It does very much so! – flawr Jul 10 '17 at 11:28

# 05AB1E, 6 bytes

LnnI.x


Try it online!

### Explanation

LnnI.x
L      # Push [1 .. input]
nn    # Raise every element to the 4th power
I   # Push input
.x # Closest element in the array to input


# APL, 22 bytes

{o/⍨p=⌊/p←|⍵-⍨o←4*⍨⍳⍵}


Try it online!

How?

o←4*⍨⍳⍵ - o = range(⍵)4 [vectorize]

p←|⍵-⍨o - p = abs(o - ⍵) [vectorize]

o/⍨ - take the o element at the index where ...

p=⌊/p - the p minimum element is

# Jelly, 6 bytes

R*4ạÐṂ


A monadic link returning a list of one item, or a full program that prints the result (using an inefficient method).

Try it online!

### How?

R*4ạÐṂ - Link: number, n
R      - range(n) -> [1,2,3,...,n]
*4    - raise to the fourth power -> [1,16,81,...,n**4]
ÐṂ - filter keep those (only ever one) minimal:
ạ   -   absolute difference (with n)
- if a full program: implicit print (one item lists print their content).


<?=(($argn**.5-.75)**.5+.5^0)**4;  Try it online! # PHP, 56 bytes <?=2*$argn-($x=($f=$argn**.25^0)**4)>($y=++$f**4)?$y:$x;  Try it online! C++, 96 bytes int Q(int N){int i=1;while (pow(i,4)<N){i++;}if (pow(i,4)-N>N-pow(i-1,4)){i--;}return pow(i,4);}  Full version: int Q(int N) { int i = 1; while (pow(i, 4) < N) { i++; } if (pow(i, 4)-N > N-pow(i - 1, 4)) i--; return pow(i,4); }  LINK to try # Haskell, 35 bytes f n=(floor$(n**0.5-3/4)**0.5+0.5)^4


# R, 474437 35 bytes

n=scan();which.min(((1:n)^4-n)^2)^4


Try it online!

• you can return an anonymous function (removing f=) and instead of x[which.min((x-n)^2)] use which.min((x-n)^2)^4, and then put f= into the header of the TIO link for testing like here :) – Giuseppe Jul 11 '17 at 15:21
• @Giuseppe Oh, there is no need to define x at all. Thank you! – Maxim Mikhaylov Jul 11 '17 at 15:31
• ah, then the only other improvement is taking the input from stdin, n=scan();which.min(((1:n)^4-n)^2)^4 and the input goes into the footer section on TIO. – Giuseppe Jul 11 '17 at 15:34
• @Giuseppe Thanks again! Useful to know for future answers in R. – Maxim Mikhaylov Jul 11 '17 at 15:44

# Pyth, 9 bytes

.maQb^R4S


Try it online!

### Pyth, 17 bytes

A full program that uses the same arithemtic approach as in @AndersKaseorg's answer:

K.5^s+^-^QK.75KK4


Try it online!

# Japt, 20 bytes

This feels far too long!


õp4
V®nU a
VgWaWrmU

Test it

# QBIC, 38 bytes

{p=q^4~p>:|~p-a>a-o|_Xo\_Xp]\o=p┘q=q+1


## Explanation

{           DO infinitely
p=q^4       Set p to q quad (q starts out as 1)
~p>:|       IF p exceeds the input THEN
~p-a>a-o    check the distance to p and to o (the last quad) and
|_Xo        PRINT o, or
\_Xp        PRINT p accordingly
]           END IF
\o=p        ELSE  ( p <= input) store p in o to keep track of this quad
┘q=q+1      and raise q for the next iteration


# Java (OpenJDK 8), 64 bytes

n->{int i=0,q,Q=0;while((q=i*i*i*i++)<n)Q=q;return q-n<n-Q?q:Q;}


Try it online!

# Common Lisp, 50 bytes

(lambda(x)(expt(floor(+(sqrt(-(sqrt x).75)).5))4))


Try it online!

# C#, 95 bytes

namespace System.Linq{n=>new int[940].Select((_,i)=>i*i*i*i).OrderBy(i=>Math.Abs(i-n)).First()}


We use 940 as a set value as any larger value will overflow the int.

Full/Formatted Version:

namespace System.Linq
{
class P
{
static void Main()
{
Func<int, int> f = n => new int[940].Select((_, i) => i * i * i * i).OrderBy(i => Math.Abs(i - n)).First();

for (int i = 1; i <= Int32.MaxValue; ++i)
Console.WriteLine(\$"{i} = {f(i)}");

}
}
}


# Ruby, 23 34 bytes

I have no idea why 0.75 is such an important number for this, but hey, whatever works.

->n{((n**0.5-0.75)**0.5).round**4}


Try it online!

• This will not give the closest biquadratic. E.g. it will return 256 for 151. – P.Péter Jul 11 '17 at 10:04