20
\$\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\$
  • 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 Nov 28 '18 at 4:20
  • 3
    \$\begingroup\$ Is the input always greater than 0 or do we have to deal with 0 and negatives? \$\endgroup\$ – Veskah Nov 28 '18 at 4:21
  • \$\begingroup\$ Inputs will always be positive \$\endgroup\$ – Embodiment of Ignorance Nov 28 '18 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 Nov 28 '18 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 Nov 28 '18 at 14:12

38 Answers 38

1
\$\begingroup\$

C++ (clang), 80 bytes, 71 bytes, 63 bytes, 59 bytes, 56 bytes

int n,c=1;cin>>n;for(;c<=n;++c){cout<<pow(c,n-c)<<endl;}

Try it online!

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

Pari/GP, 22 bytes

n->[i^(n-i)|i<-[1..n]]

Try it online!

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

PowerShell, 39 bytes

param($n)1..$n|%{"1"+"*$_"*($n-$_)|iex}

Try it online!

Because exponents are expensive in Powershell, this works by using invoke-expression to parse and calculate the string "1*n*n...*n" Works because the first and last entry are always 1.

\$\endgroup\$
  • \$\begingroup\$ Nice. The length of param($n)1..$n|%{[Math]::Pow($_,$n-$_)} is 39 bytes too :) \$\endgroup\$ – mazzy Dec 7 '18 at 9:10
  • \$\begingroup\$ @mazzy I actually did check that after posting this and chuckled at finding out. \$\endgroup\$ – Veskah Dec 7 '18 at 20:59
1
\$\begingroup\$

Gambit Scheme (gsi), 52 bytes

(lambda(x)(map(lambda(y)(expt y(-x y)))(iota x 1)))

For some reason this code does not appear to work on TIO. It works fine on my machine.

Explanation:

(lambda(x)(map(lambda(y)(expt y(- x y)))(iota x 1)))    Full program
(lambda(x)                                        )    Anonymous function with arg x
          (map(lambda(y)              )(iota x 1))     Map over the range 1 to input
                        (expt y                        Raises the mapped value to...
                               (- x y)                 The input value minus the mapped value (this powers the list by the reverse)
\$\endgroup\$
0
\$\begingroup\$

C (gcc) (-lm), 52 bytes

f(n,i){for(i=n;i--;printf("%.0f\n",pow(n-i,i)));}

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Can't you just use %.f withouth the 0? Also, in a trailing comma is allowed, youcan replace \n with , \$\endgroup\$ – bznein Nov 29 '18 at 13:11
0
\$\begingroup\$

APL(NARS), 11 chars, 22 bytes

{⍪⍵*⌽⍵-1}∘⍳

test:

  f←{⍪⍵*⌽⍵-1}∘⍳
  f 5
1
8
9
4
1
\$\endgroup\$
0
\$\begingroup\$

SAS, 71 bytes

data a;input n;o=cat(1);do x=1 to n-1;o=catx(',',(n-x)**x,o);end;cards;

Input goes after the cards; statement, separated by newlines, like so:

data a;input n;o=cat(1);do x=1 to n-1;o=catx(',',(n-x)**x,o);end;cards;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Outputs a dataset containing the input n, and comma-separated output string o (and also helper variable x)

+----+------------------------------------------------------------------------------------------+----+
| n  |                                            o                                             | x  |
+----+------------------------------------------------------------------------------------------+----+
¦ 1  ¦ 1                                                                                        ¦ 1  ¦
¦ 2  ¦ 1,1                                                                                      ¦ 2  ¦
¦ 3  ¦ 1,2,1                                                                                    ¦ 3  ¦
¦ 4  ¦ 1,4,3,1                                                                                  ¦ 4  ¦
¦ 5  ¦ 1,8,9,4,1                                                                                ¦ 5  ¦
¦ 6  ¦ 1,16,27,16,5,1                                                                           ¦ 6  ¦
¦ 7  ¦ 1,32,81,64,25,6,1                                                                        ¦ 7  ¦
¦ 8  ¦ 1,64,243,256,125,36,7,1                                                                  ¦ 8  ¦
¦ 9  ¦ 1,128,729,1024,625,216,49,8,1                                                            ¦ 9  ¦
¦ 10 ¦ 1,256,2187,4096,3125,1296,343,64,9,1                                                     ¦ 10 ¦
¦ 11 ¦ 1,512,6561,16384,15625,7776,2401,512,81,10,1                                             ¦ 11 ¦
¦ 12 ¦ 1,1024,19683,65536,78125,46656,16807,4096,729,100,11,1                                   ¦ 12 ¦
¦ 13 ¦ 1,2048,59049,262144,390625,279936,117649,32768,6561,1000,121,12,1                        ¦ 13 ¦
¦ 14 ¦ 1,4096,177147,1048576,1953125,1679616,823543,262144,59049,10000,1331,144,13,1            ¦ 14 ¦
¦ 15 ¦ 1,8192,531441,4194304,9765625,10077696,5764801,2097152,531441,100000,14641,1728,169,14,1 ¦ 15 ¦
+----------------------------------------------------------------------------------------------------+
\$\endgroup\$
0
\$\begingroup\$

C (clang), 47 bytes

i;f(a){for(i=a;i;printf("%.f,",pow(i,a-i--)));}

Try it online!

Prints in reverse order, delimited with commas.

\$\endgroup\$

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.