23
\$\begingroup\$

So this is my first challenge on this site.

The challenge is to take in an input integer \$n\$, which will be positive, and print, in ascending order (\$1\$ to \$n\$, including n), the output of \$i^{(n-i)}\$ (where \$i\$ is the current integer).

Example

Given the input 5, the program will print:

1  
8  
9  
4  
1  

\$1^4\$ is 1 and \$1+4=5\$
\$2^3\$ is 8 and \$2+3=5\$
\$3^2\$ is 9 and \$3+2=5\$
\$4^1\$ is 4 and \$4+1=5\$
\$5^0\$ is 1 and \$5+0=5\$

Input and Output

Input will be in the form of a positive integer. Output will be a list of numbers, delimited by either commas or new lines.

This is , so shortest code wins.

\$\endgroup\$
9
  • 5
    \$\begingroup\$ the comma/newline detail should be omitted, it is normal practice around here to let output of lists be in any convenient format, including as a list/array object being returned by a function \$\endgroup\$
    – Sparr
    Commented Nov 28, 2018 at 4:20
  • 3
    \$\begingroup\$ Is the input always greater than 0 or do we have to deal with 0 and negatives? \$\endgroup\$
    – Veskah
    Commented Nov 28, 2018 at 4:21
  • \$\begingroup\$ Inputs will always be positive \$\endgroup\$
    – Gymhgy
    Commented Nov 28, 2018 at 5:01
  • 6
    \$\begingroup\$ Two equally short answers doesn't matter. If you feel like accepting an answer, choose the earliest posted one. However I strongly recommend waiting at least a few days, and would suggest never accepting an answer (to encourage more submissions). \$\endgroup\$
    – Οurous
    Commented Nov 28, 2018 at 5:21
  • 2
    \$\begingroup\$ Shouldn't the title be "Given an integer, print all the powers obtained with a base and an exponent that sum to the input"? \$\endgroup\$
    – Nicola Sap
    Commented Nov 28, 2018 at 14:12

40 Answers 40

6
\$\begingroup\$

APL (Dyalog Unicode), 8 5 bytes

⍳*⊢-⍳

Try it online!

Anonymous prefix tacit function. TIO tests for the range [1..10].

Thanks @lirtosiast for 3 bytes.

How:

⍳*⊢-⍳ ⍝ Tacit function
    ⍳ ⍝ Range. ⍳n generates the vector [1..n].
  ⊢- ⍝ Subtracted from the argument. The vector is now [n-1,n-2,...,0]
⍳*    ⍝ Exponentiate using the range [1..n] as base. The result is the vector
      ⍝ [1^(n-1), 2^(n-2), 3^(n-3),...]
\$\endgroup\$
2
  • 2
    \$\begingroup\$ ⍳*⊢-⍳ is 5 bytes, using ⎕IO←1. \$\endgroup\$
    – lirtosiast
    Commented Nov 28, 2018 at 18:43
  • \$\begingroup\$ @lirtosiast took me a while to figure out why does that work, but I got it. Thanks. \$\endgroup\$
    – J. Sallé
    Commented Nov 29, 2018 at 12:20
5
\$\begingroup\$

Haskell, 23 bytes

f i=[x^(i-x)|x<-[1..i]]

Try it online!

Alternative version, also 23 bytes:

f i=(^)<*>(i-)<$>[1..i]
\$\endgroup\$
5
\$\begingroup\$

Japt, 5 bytes

õ_p´U

Try it

õ         :Range [1,input]
 _        :Map
  p       :  Raise to the power of
   ´U     :  Input decremented
\$\endgroup\$
5
\$\begingroup\$

Perl 6, 19 bytes

{^$_+1 Z**[R,] ^$_}

Try it online!

Anonymous code block that takes a number and returns a list. Zip exponents the range 1 to input and the range input-1 to 0

\$\endgroup\$
5
\$\begingroup\$

Aheui (esotope), 193 164 bytes (56 chars)

방빠싹받분샥퍼붇바파쟈뿌차샦히망맣여
타빠바푸투반또분뽀뿌서썪삯타삯받반타
석차샦져쌲볼어타토싻삭빠쏛ㅇ또섞썪뻐

Try it online!

Try it on AVIS(Korean); just copy and paste code above, press start button, input a number, see how it moves. To see output, press the >_ icon on left side.


It's not golfed much, but I give it a shot.

\$\endgroup\$
2
  • \$\begingroup\$ Is it possible to chose a character set, so that each character is stored in 2 bytes? \$\endgroup\$
    – tsh
    Commented Nov 29, 2018 at 2:52
  • \$\begingroup\$ @tsh According to Aheui specification, an Aheui code consists of only UTF-8 characters. \$\endgroup\$
    – cobaltp
    Commented Nov 29, 2018 at 5:07
4
\$\begingroup\$

Pyth, 5 bytes

_m^-Q

Try it online!

Optimally encoded this would be 4.106 bytes.

_                reverse of the following list:
 m               map the following lambda d:
  ^                (N-d)**d
   -Qd             
      d
       Q         over [0,...,N-1]
\$\endgroup\$
0
4
\$\begingroup\$

J, 10 bytes

(>:^|.)@i.

Try it online!

If we really need to separate the numbers by a newline:

J, 13 bytes

,.@(>:^|.)@i.

Try it online!

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

Jelly, 5 bytes

R*ḶU$

Try it online!

R                [1,...,n]
 *               to the power of
  ḶU$            [0,...,n-1] reversed
\$\endgroup\$
3
\$\begingroup\$

PHP, 32 bytes

while($argn)echo++$i**--$argn,_;

Run as pipe with -nR or try it online.

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

Octave, 18 bytes

@(n)(t=1:n).^(n-t)

Try it online!

Thanks Luis Mendo, using internal variable saves 3 bytes.

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

Jelly, 4 bytes

*ạ¥€

Try it online!

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

Wolfram Language (Mathematica), 24 20 18 bytes

(x=Range@#)^(#-x)&

Try it online!

-4 thanks @lirtosiast.

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

MathGolf, 6 bytes

rx\╒m#

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ I have implemented reverse subtraction, multiplication and division, but it looks like a reverse power operator could come in handy? \$\endgroup\$
    – maxb
    Commented Nov 28, 2018 at 7:33
2
\$\begingroup\$

Python 2, 40 bytes

lambda n:[i**(n-i)for i in range(1,n+1)]   #Outputs a list

Try it online!

Python 2, 41 bytes

n,i=input(),0
exec"print(n-i)**i;i+=1;"*n   #Prints in reversed order

Try it online!

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

Ruby, 27 bytes

->n{(1..n).map{|r|r**n-=1}}

Try it online!

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

Retina, 35 bytes

.+
*
_
$$.($.'*$($.>`$*)_¶
%~`^
.+¶

Try it online! Explanation:

.+
*

Convert the input to unary.

_

Match each position. This then sets several replacement variables. $` becomes the left of the match; $>` modifies this to be the left and match; $.>` modifies this to take the length, i.e. the current index. $' meanwhile is the right of the match, so $.' is the length i.e. the current exponent.

$$.($.'*$($.>`$*)_¶

Create a string $.( plus $.' repetitions of $.>`* plus _. For an example, for an index of 2 in an original input of 5, $.' is 3 and $.>` is 2 so the resulting string is $.(2*2*2*_. This conveniently is a Retina replacement expression that caluclates 2³. Each string is output on its own line.

%~`^
.+¶

For each line generated by the previous stage, prefix a line .+ to it, turning it into a replacement stage, and evaluate that stage, thereby calculating the expression.

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

QBasic, 3533 bytes

Thank you @Neil for 2 bytes!

INPUT a
FOR b=1TO a
?b^(a-b)
NEXT

Slightly expanded version on REPL.IT because the interpreter in't entirely up-to-spec.

Output

QBasic (qb.js)
Copyright (c) 2010 Steve Hanov
   
   5
1
8
9
4
1
\$\endgroup\$
2
  • \$\begingroup\$ Save 2 bytes by outputting the list in the correct order! (b^(a-b) for b=1..a) \$\endgroup\$
    – Neil
    Commented Nov 28, 2018 at 12:14
  • \$\begingroup\$ @Neil Thanks, I've worked it in! \$\endgroup\$
    – steenbergh
    Commented Nov 28, 2018 at 13:26
2
\$\begingroup\$

F# (.NET Core), 42 bytes

let f x=Seq.map(fun y->pown y (x-y))[1..x]

Try it online!

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

JavaScript (Node.js), 33 32 bytes

n=>(g=i=>--n?++i**n+[,g(i)]:1)``

Try it online!

-3 bytes with credits to @Shaggy, and -1 byte by @l4m2!

JavaScript (Node.js), 36 bytes

f=(n,i=1)=>n--?[i++**n,...f(n,i)]:[]

Try it online!

JavaScript (Node.js), 37 bytes

n=>[...Array(n)].map(x=>++i**--n,i=0)

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ 33 bytes \$\endgroup\$
    – Shaggy
    Commented Nov 28, 2018 at 8:03
  • \$\begingroup\$ 32 \$\endgroup\$
    – l4m2
    Commented Nov 28, 2018 at 14:25
2
\$\begingroup\$

C# (Visual C# Interactive Compiler), 46 bytes

x=>new int[x].Select((_,i)=>Math.Pow(i+1,--x))

Try it online!

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

MATL, 5 bytes

:Gy-^

Try it online!

Explanation

Consider input 5 as an example.

:     % Implicit input. Range
      % STACK: [1 2 3 4 5]
G     % Push input again
      % STACK: [1 2 3 4 5], 5
y     % Duplicate from below
      % STACK: [1 2 3 4 5], 5, [1 2 3 4 5]
-     % Subtract, element-wise
      % STACK: [1 2 3 4 5], [4 3 2 1 0]
^     % Power, element-wise. Implicit display
      % STACK: [1 8 9 4 1]
\$\endgroup\$
2
\$\begingroup\$

Java, 59 Bytes

for(int i=1;a+1>i;i++)System.out.println(Math.pow(i,a-i));
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Welcome to PPCG. It looks like this requires "input" be assigned to the predefined variable a, which we don't allow. \$\endgroup\$
    – Shaggy
    Commented Nov 29, 2018 at 21:01
  • 2
    \$\begingroup\$ Hello, here's a fix for you: n->{for(int i=0;i++<n;)System.out.println(Math.pow(i,n-i));} 60 bytes (code and test cases in the link) \$\endgroup\$ Commented Nov 30, 2018 at 9:20
1
\$\begingroup\$

Clean, 37 bytes

import StdEnv
$n=[i^(n-i)\\i<-[1..n]]

Try it online!

Defines $ :: Int -> [Int] taking an integer and returning the list of results.

$ n                // function $ of n
 = [i ^ (n-i)      // i to the power of n minus i
    \\ i <- [1..n] // for each i in 1 to n
   ]
\$\endgroup\$
1
\$\begingroup\$

R, 34 bytes

x=1:scan();cat(x^rev(x-1),sep=',')

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Is the default "sep" not a space? Would that not work? \$\endgroup\$ Commented Nov 29, 2018 at 15:51
  • 1
    \$\begingroup\$ @stuartstevenson "Output will be a list of numbers, delimited by either commas or new lines." \$\endgroup\$
    – Giuseppe
    Commented Nov 29, 2018 at 15:53
1
\$\begingroup\$

05AB1E, 5 bytes

LD<Rm

Port of @lirtosiast's Jelly answer.

Try it online.

Explanation:

L      # List in the range [1, (implicit) input integer]
       #  i.e. 5 → [1,2,3,4,5]
 D<    # Duplicate this list, and subtract 1 to make the range [0, input)
       #  i.e. [1,2,3,4,5] → [0,1,2,3,4]
   R   # Reverse it to make the range (input, 0]
       #  i.e. [0,1,2,3,4] → [4,3,2,1,0]
    m  # Take the power of the numbers in the lists (at the same indices)
       # (and output implicitly)
       #  i.e. [1,2,3,4,5] and [4,3,2,1,0] → [1,8,9,4,1]
\$\endgroup\$
1
\$\begingroup\$

Lua, 43 41 bytes

-2 bytes thanks to @Shaggy

s=io.read()for i=1,s do print(i^(s-i))end

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I don't think you need the +0; seems to work without it. \$\endgroup\$
    – Shaggy
    Commented Nov 28, 2018 at 10:06
1
\$\begingroup\$

R, 22 bytes

n=scan();(1:n)^(n:1-1)

Fairly self-explanatory; note that the : operator is higher precendence than the - operator so that n:1-1 is shorter than (n-1):0

If we are allowed to start at 0, then we can lose two bytes by using (0:n)^(n:0) avoiding the need for a -1.

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

Charcoal, 9 bytes

I⮌ENX⁻θιι

Try it online! Link is to verbose version of code. Explanation:

   N        Input as a number
  E         Map over implicit range
       ι    Current value
     ⁻      Subtracted from
      θ     First input
    X       Raised to power
        ι   Current value
 ⮌          Reverse list
I           Cast to string
             Implicitly print on separate lines
\$\endgroup\$
1
\$\begingroup\$

C# (Visual C# Interactive Compiler), 55 bytes

v=>Enumerable.Range(0,v--).Select(i=>Math.Pow(i+1,v--))

Try it online!

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

Perl 5 -n, 21 bytes

say++$\**--$_ while$_

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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