-6
\$\begingroup\$

Guidelines

Task

Given two non-negative integers, find the sum of both numbers... to the power of 4.


Examples

2, 3 -> 97 (2^4 + 3^4 = 97)

14, 6 -> 39712 (14^4 + 6^4 = 39712)

0, 25 -> 390625 (0^4 + 25^4 = 390625)


Rules

  • You will only ever receive two non-negative integers as input (bonus points if you get it to work with negative integer input).
  • You can take the input however you would like (arrays, list, two separate arguments, comma-separated string)
  • You can either output your answer as a number or a string.
  • As this is code golf, the shortest answer in bytes will win.
\$\endgroup\$
  • 2
    \$\begingroup\$ You say two positive integers, but one of the test cases contains a 0. Also, there's not many languages that would have trouble with negative integers, given the result is the same as if they were positive. \$\endgroup\$ – Jo King Feb 19 '18 at 10:08
  • 13
    \$\begingroup\$ I've downvoted this challenge for the following reason: it is a fairly trivial, yet non-essential (such as Hello, World!) and I doubt that any interesting answers will be produced. \$\endgroup\$ – caird coinheringaahing Feb 19 '18 at 12:09
  • 1
    \$\begingroup\$ Downvoted for the same reason as @cairdcoinheringaahing. \$\endgroup\$ – Yytsi Feb 19 '18 at 15:54
  • 3
    \$\begingroup\$ Upvoted because sometimes trivial challenges are fun \$\endgroup\$ – FantaC Feb 19 '18 at 18:11
  • 1
    \$\begingroup\$ @cairdcoinheringaahing Just because it is trivial, does not mean it is a bad challenge? Take a look at the CP-1610 answer, I would definitely call that interesting. It has produced interesting solutions. \$\endgroup\$ – Amorris Feb 19 '18 at 19:22

45 Answers 45

5
\$\begingroup\$

CP-1610 assembly, 30 DECLEs = 38 bytes

Let's try this on a processor lacking a multiply instruction. This code is intended to be run on an Intellivision.

CP-1610 instructions are encoded with 10-bit values, known as 'DECLE' s. This subroutine is 30 DECLEs long, starting at $4808 and ending at $4825.

Takes input in registers R0 and R3. Saves the result in R2.

                              ROMW  10            ; use 10-bit ROM
                              ORG   $4800         ; map program at address $4800

4800  02B8 000E               MVII  #14,    R0    ; example call
4802  02BB 0006               MVII  #6,     R3
4804  0004 0148 0008          CALL  addX4Y4
4807  0017                    DECR  PC            ; loop forever

4808  0275            addX4Y4 PSHR  R5            ; push the return address
4809  0004 0148 001A          CALL  square        ; compute R2 = R0^2
480C  0004 0148 0019          CALL  square2       ; compute R2 = R2^2
480F  0272                    PSHR  R2            ; push this result on the stack
4810  0098                    MOVR  R3,     R0    ; compute R2 = R3^2
4811  0004 0148 001A          CALL  square
4814  0004 0148 0019          CALL  square2       ; compute R2 = R2^2
4817  02F2                    ADD@  R6,     R2    ; add this result to the intermediate one
4818  02B7                    PULR  PC            ; return

4819  0090            square2 MOVR  R2,     R0    ; copy R2 to R0
481A  0081            square  MOVR  R0,     R1    ; copy R0 to R1
481B  01D2                    CLRR  R2            ; initialize R2 = result
481C  0200 0002               B     halve         ; start by halving R1
481E  00C2            add     ADDR  R0,     R2    ; add R0 to R2
481F  0048            loop    SLL   R0            ; double R0
4820  0079            halve   SARC  R1            ; halve R1
4821  0221 0004               BC    add           ; was the LSB set?
4823  022C 0005               BNEQ  loop          ; is R1 now equal to zero?
4825  00AF                    JR    R5            ; return

Example run

Running the above code (with R0 = 14 and R3 = 6) gives:

> b 4807
Set breakpoint at $4807
> r
Hit breakpoint at $4807
 0900 0000 9B20 0006 01FE 4817 02F1 4807 S-----iq  DECR R7
           ^^^^

R2 is set to $9B20, which is 39712 in decimal.

\$\endgroup\$
4
\$\begingroup\$

Perl, 12 bytes

Includes +1 for p

Works for 1 or more numbers each given on a separate line on STDIN

(echo 2; echo 3) | perl -pe '$\+=$_**4}{'
\$\endgroup\$
4
\$\begingroup\$

Haskell, 11 bytes

sum.map(^4)

This is a function that takes the parameters as a list.

Try it online!

\$\endgroup\$
  • 2
    \$\begingroup\$ Same bytecount: a#b=a^4+b^4 \$\endgroup\$ – ბიმო Feb 19 '18 at 15:02
3
\$\begingroup\$

><>, 11 bytes

:*:*$:*:*+n

Try it online!

Takes values through the -v flag. Dupe and multipy, dupe and multiply, and repeat with the other value before adding the two together and printing.

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

Japt, 3 bytes

Takes input as an array of integers; can handle negatives and more than 2 integers at a time. Add N at the beginning to take input as individual integers.

xp4

Try it


Explanation

p4 raises each element to the power of 4 and x reduces by addition.

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

Pyt, 2 bytes

⁴Ʃ

Try it online!

Takes input as a list.

\$\endgroup\$
  • \$\begingroup\$ Knew there would be a language with a built-in for **4, congrats. \$\endgroup\$ – ETHproductions Feb 20 '18 at 16:50
2
\$\begingroup\$

APL (Dyalog Unicode), 5 bytesSBCS

Anonymous tacit prefix function. Takes a list as argument. The list may have any length and contain any numbers, even complex ones.

+.*∘4

Try it online!

+.* is a variant on matrix product, +.× as follows: a b+.×c d is (a×c)+(b×d) and a b+.×c is (a×c)+(b×c). So a b+.*c is (a*c)+(b*c). * is power.

∘4 curry four as right argument. This results in a monadic function (a*4)+(b*4).

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

Retina, 9 bytes

.+
****
_

Try it online!

Input should be linefeed-separated.

Explanation

.+
****

* is Retina's repetition operator. It has implicit operands $& and _, respectively, so the substitution pattern is short for $&*$&*$&*$&*_. It's also right-associative, if the regex matches a decimal number n, this generates a string of n4 underscores (i.e. a unary representation of the fourth power of n).

_

To sum the two results and convert the sum back to decimal, we simply count the number of underscores in the string.

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

C (gcc), 0 + 24 bytes

Compile this code

main(){
    printf("%d\n",x(2,3));
    printf("%d\n",x(14,6));
    printf("%d\n",x(0,25));
}

With this flag:

-D=x(a,b)a*a*a*a+b*b*b*b

Try it online! (GCC Tio)
Try it online! (Bash Compile example)

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

Python 3, 20 bytes

lambda x,y:x**4+y**4
\$\endgroup\$
  • \$\begingroup\$ You can remove the assignment to f as this isn't a recursive function. \$\endgroup\$ – Shaggy Feb 19 '18 at 11:56
  • \$\begingroup\$ @Shaggy Maybe I'm confused but how else would my answer take any input? Wouldn't I then be defining something that is essentially 'lost' right after being interpreted? It's not like I can feed input in via args, for example. Or it is normal in codegolf that those bytes 'f=' are not counted? \$\endgroup\$ – linemade Feb 19 '18 at 12:27
  • \$\begingroup\$ What I meant was: you don't need to include it in your byte count. Anonymous functions/lambdas are valid. \$\endgroup\$ – Shaggy Feb 19 '18 at 13:00
2
\$\begingroup\$

MATL, 3 bytes

K^s

Try it online!

This can handle more than two input values, as well as negative inputs.

Explanation:

Fasten your seat belts, this might blow your mind!

       % Implicit input
K      % Push literal 4
 ^     % Raise each element of the input vector to the 4th power
  s    % Sum

Also works:

4^s    % Push 4 and raise input to it, then sum
UUs    % Square input twice, then sum
\$\endgroup\$
1
\$\begingroup\$

05AB1E, 3 bytes

4mO

Try it online!

Explanation

4m    # raise each to the power of 4
  O   # sum
\$\endgroup\$
1
\$\begingroup\$

Julia, 11 bytes

a$b=a^4+b^4

Try it online!


Julia, 12 bytes

!a=sum(a.^4)

Try it online!

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

J, 7 6 bytes

-1 byte thanks to Adám

1#.^&4

Try it online!

Works for lists with arbitrary length

^&4 - each item of the list to the 4-th power

1#. - sum of all 4-th powers by base-1 conversion

Try it online!

Alternative

J, 7 bytes

+/ .^4:

This is a variant of the matrix product, analogue of Adám's APL solution

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ 6 bytes: 1#.^&4 \$\endgroup\$ – Adám Feb 19 '18 at 10:34
  • \$\begingroup\$ @Adám Thanks, I forgot to try to add up the numbers by base-1 conversion. \$\endgroup\$ – Galen Ivanov Feb 19 '18 at 11:09
1
\$\begingroup\$

C (gcc), 26 bytes

f(a,b){a=a*a*a*a+b*b*b*b;}

Try it online!

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

C, C++ => 29 bytes

-1 byte thanks to Jonathan Frech

#define Q(a,b)a*a*a*a+b*b*b*b

Test cases :

#include <stdio.h>
int main() {
    printf("Q(%d,%d) = %d\n", 2, 3, Q(2, 3));
    printf("Q(%d,%d) = %d\n", 14, 6, Q(14, 6));
    printf("Q(%d,%d) = %d\n", 0, 25, Q(0, 25));
}
\$\endgroup\$
  • \$\begingroup\$ Could you not drop the space in ) a? \$\endgroup\$ – Jonathan Frech Feb 19 '18 at 11:07
1
\$\begingroup\$

Triangularity, 31 bytes

...)...
..IEM..
.)4s^}.
u......

Try it online!

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

R, 16 bytes

pryr::f(x^4+y^4)

Try it online!

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

Ruby, 16 bytes

->a,b{a**4+b**4}

Try It Online!

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

Perl 6, 9 bytes

*⁴+*⁴

Try it online!

\$\endgroup\$
  • \$\begingroup\$ @EsolangingFruit The problem is that along with * WhateverCode lambdas there are also ** HyperWhatever lambdas; so it would have to be written as * **4+* **4, or else it would would be seen as ** *4+** *4 which doesn't work. ((**⁴)(2,3) results in (16,18)) \$\endgroup\$ – Brad Gilbert b2gills Feb 19 '18 at 17:59
  • \$\begingroup\$ @BradGilbertb2gills I guess that makes sense. Why am I surprised Perl supports Unicode superscript exponents? \$\endgroup\$ – Esolanging Fruit Feb 20 '18 at 1:01
1
\$\begingroup\$

Python 3, 20 bytes

lambda a,b:a**4+b**4

Try it online!

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

JavaScript (ES7), 15 bytes

Does exactly what it says on the tin.

a=>b=>a**4+b**4

Test cases

let f =

a=>b=>a**4+b**4

console.log(f(2)(3))
console.log(f(14)(6))
console.log(f(0)(25))

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

IBM/Lotus Notes Formula, 17 bytes

@Sum(@Power(a;4))

Field formula that takes its input from a multi-value numeric field on the same form. Works because for most Formula functions, if a list is given as input then the formula will apply the given function recursively to every item in the list.

There is no TIO for Notes but here's a screenshot of one of the test cases:

Test Case

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

Excel, 10 bytes

=A1^4+B1^4

Nothing to see here.

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

Jelly, 3 bytes

*4S

Try it online!

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

Add++, 8 bytes

L,4^$4^+

Try it online!

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

Perl 6, 15 bytes

{$^a**4+$^b**4}

An anonymous lambda. Try It Online!

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

Java 8, 21 bytes

a->b->a*a*a*a+b*b*b*b

Try it online.

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

APL+WIN, 5 bytes

Prompts for screen input of a two element vector. Seems to work for negative inputs

+/⎕*4
\$\endgroup\$
0
\$\begingroup\$

Python 2, 30 28 bytes

lambda i:sum(x**4for x in i)

Try it online!

Alternative approach for Python (albeit slightly longer than the more obvious one). Takes input as a list of integers.

-2 with thanks to @JonathanAllan

\$\endgroup\$
  • \$\begingroup\$ You can drop the [ and ] \$\endgroup\$ – Jonathan Allan Feb 19 '18 at 14:21
  • \$\begingroup\$ @JonathanAllan - I was sure I'd tried that and got a syntax error but it seems to work now. Thanks! \$\endgroup\$ – ElPedro Feb 19 '18 at 15:25

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.