18
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ Interesting to note that this will never tie, as the sequence alternates odd and even numbers. \$\endgroup\$ – Okx Jul 10 '17 at 9:39
  • 5
    \$\begingroup\$ you could change the name to "Find the nearest zenzizenzic". en.wiktionary.org/wiki/zenzizenzic \$\endgroup\$ – Destructible Lemon Jul 10 '17 at 9:41
  • 1
    \$\begingroup\$ @Mayube It necessarily does, because the sequence is just n^4 and n alternates in sign. \$\endgroup\$ – Martin Ender Jul 10 '17 at 9:43
  • 2
    \$\begingroup\$ 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, ... \$\endgroup\$ – sergiol Jul 10 '17 at 10:35
  • 2
    \$\begingroup\$ Isn't that called "quartic"? (Merriam-Webster, Wiktionary) \$\endgroup\$ – Olivier Grégoire Jul 10 '17 at 15:49

24 Answers 24

15
\$\begingroup\$

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

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

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.

\$\endgroup\$
  • 2
    \$\begingroup\$ It is even golfier! \$\endgroup\$ – flawr Jul 10 '17 at 11:30
7
\$\begingroup\$

Haskell, 51 49 bytes

Function monad ftw!

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

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

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!

\$\endgroup\$
  • 2
    \$\begingroup\$ 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? \$\endgroup\$ – Martin Rosenau Jul 10 '17 at 10:47
  • 3
    \$\begingroup\$ @MartinRosenau Neim uses a custom codepage \$\endgroup\$ – Okx Jul 10 '17 at 10:47
5
\$\begingroup\$

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

\$\endgroup\$
  • 1
    \$\begingroup\$ 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 \$\endgroup\$ – Taylor Scott Jul 12 '17 at 10:03
  • 1
    \$\begingroup\$ @TaylorScott, thanks for pointing that out. Have updated. \$\endgroup\$ – Wernisch Jul 12 '17 at 10:15
4
\$\begingroup\$

Mathematica, 21 bytes

Nearest[Range@#^4,#]&
\$\endgroup\$
4
\$\begingroup\$

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

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

\$\endgroup\$
3
\$\begingroup\$

Octave, 37 bytes

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

Anonymous function that uses nearest-neighbour interpolation.

Try it online!

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

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

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

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

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

PHP, 33 bytes

<?=(($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!

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

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

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

Haskell, 35 bytes

f n=(floor$(n**0.5-3/4)**0.5+0.5)^4

Port of Anders' Python3 answer.

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

R, 47 44 37 35 bytes

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

Try it online!

\$\endgroup\$
  • \$\begingroup\$ 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 :) \$\endgroup\$ – Giuseppe Jul 11 '17 at 15:21
  • 1
    \$\begingroup\$ @Giuseppe Oh, there is no need to define x at all. Thank you! \$\endgroup\$ – Maxim Mikhaylov Jul 11 '17 at 15:31
  • \$\begingroup\$ 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. \$\endgroup\$ – Giuseppe Jul 11 '17 at 15:34
  • \$\begingroup\$ @Giuseppe Thanks again! Useful to know for future answers in R. \$\endgroup\$ – Maxim Mikhaylov Jul 11 '17 at 15:44
1
\$\begingroup\$

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!

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

Japt, 20 bytes

This feels far too long!


õp4
V®nU a
VgWaWrmU

Test it

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

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

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!

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

Common Lisp, 50 bytes

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

Try it online!

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

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)}");

            Console.ReadLine();
        }
    }
}
\$\endgroup\$
0
\$\begingroup\$

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!

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

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.