Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Given two integers greater than one, A and B, output four mathematical expressions in this order:

  1. The plain expression A^B (A to the power B). e.g. if A = 2 and B = 3, 2^3.

  2. The expansion of A^B in terms of repeated multiplications of A. e.g. 2*2*2.

  3. The expansion of A^B in terms of repeated additions of A. e.g. 2+2+2+2.

  4. The expansion of A^B in terms of repeated additions of 1. e.g. 1+1+1+1+1+1+1+1.

The four expressions may be output in any reasonable way as long as they are in order and clearly distinct. For example, you might put them in a list, or print them on separate lines

2^3
2*2*2
2+2+2+2
1+1+1+1+1+1+1+1

or perhaps on one line separated by equals signs:

2^3=2*2*2=2+2+2+2=1+1+1+1+1+1+1+1

Spaces may be inserted next to math operators so

2^3 = 2 * 2 * 2 = 2 + 2 + 2 + 2 = 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1

would be equally valid output when A = 2 and B = 3.

You may use symbols alternative to ^, *, and +, but only if the new symbols are more idiomatic for your language (e.g. ** instead of ^ in Python).

You may assume that A and B are sufficiently small so that A^B will not overflow your language's default integer type (given that that type has a reasonable maximum, 255 at least).

The shortest code in bytes wins.

Test Cases

One output per line. The input can be inferred as the first expression is always A^B.

2^2 = 2*2 = 2+2 = 1+1+1+1
2^3 = 2*2*2 = 2+2+2+2 = 1+1+1+1+1+1+1+1
2^4 = 2*2*2*2 = 2+2+2+2+2+2+2+2 = 1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1
2^5 = 2*2*2*2*2 = 2+2+2+2+2+2+2+2+2+2+2+2+2+2+2+2 = 1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1
3^2 = 3*3 = 3+3+3 = 1+1+1+1+1+1+1+1+1
3^3 = 3*3*3 = 3+3+3+3+3+3+3+3+3 = 1+1+1+1+1+1+1+1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+4+4+4+4+4+4+4+4 = 1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1
10^2 = 10*10 = 10+10+10+10+10+10+10+10+10+10 = 1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1
13^2 = 13*13 = 13+13+13+13+13+13+13+13+13+13+13+13+13 = 1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1
share|improve this question
    
@JonathanAllan Yeah, you got all the typos I saw. Nice job! You get nothing. – R. Kap yesterday
2  
You get thanks from me @JonathanAllan. R. Kap had me worrying about all my 3's – Helka Homba yesterday
    
Well, (luckily) there were no misplaced ='s to the left of any 3's. – R. Kap yesterday
4  
Please don't do "expand knuth's up arrows notation" next time – Matthew Roh yesterday
1  
If we read input from STDIN as a single string, is 2^3 a valid input format? Or does it have to be space/comma/linefeed-separated or something? – Martin Ender 22 hours ago

16 Answers 16

Python 3.6, 88 74 bytes

-2 bytes thanks to Dada (use ~)
-5 bytes thanks to Erwan (use f-strings from Python 3.6)

lambda a,b:f"{a}^{b}={a}{f'*{a}'*~-b}={a}{f'+{a}'*~-a**~-b}=1"+"+1"*~-a**b

online ide anyone?

How?

This is an unnamed function taking the two integer inputs a and b each greater than 0 (even though the specification is only for those greater than 1).

In Python 3.6 a new feature is available, namely formatted string literals or "f-strings". These allow run-time evaluated string construction. A leading f (or F) creates such a construct, e.g. f"blah" or f'blah'. Inside an f-string anything between a pair of braces, {...}, is an expression to be evaluated.

As such f"{a}^{b}={a}{f'*{a}'*~-b}={a}{f'+{a}'*~-a**~-b}=1" evaluates each of a, b, a, f'*{a}'*~-b, a, and f'+{a}'*~-a**~-b} as expressions, keeping the ^, =, =, and =1 as strings,all of which gets concatenated together.

The a and b expressions evaluate to the representations of a and b respectively.

The f'*{a}' and f'+{a}' in turn are also f-strings inside these expressions, which evaluate to a with a leading '*' and a leading '+' respectively

To create the required number of as and operations for the * and + portions note that there will be b as multiplied together and a**(b-1) as added together. Each case then requires one less operator sign than the number of as. So we can repeat the f-strings f'*{a}and f'+{a}' (using *) as many times as there are operators and prepend each with a single a. (b-1) is ~-b and (a**(b-1))-1 is ~-a**~-b.

The same is done for the 1s using (a**b)-1 being ~-**b, but we don't need the overhead of f-strings since 1 is constant, so a standard string repeated is concatenated with +.


Prior Python versions, 81:

lambda a,b:'%s^%s=%s=%s=1'%(a,b,('*%s'%a*b)[1:],('+%s'%a*a**~-b)[1:])+'+1'*~-a**b

Try it online!

share|improve this answer
    
Can't you replace (b-1) with ~-b ? – Dada yesterday
    
Yes you are right, I missed that. – Jonathan Allan yesterday
1  
you can win 5 bytes, if you use f string from python 3.6: lambda a,b:f"{a}^{b}={(f'*{a}'*b)[1:]}={(f'+{a}'*a**~-b)[1:]}=1"+'+‌​1'*~-a**b – Erwan 22 hours ago
    
@Erwan yes, I was planning on using f strings when I returned (now). I can save a couple more too. – Jonathan Allan 15 hours ago
    
Always good to see the tadpole operators. – Jack Brounstein 10 hours ago

MATL, 46 bytes

XH'^'i'='XJ2G:"H'*']xJ&Gq^:"H'+']xJ&G^:"1'+']x

Try it online!

Stretching the limits of "reasonable way" here, but the expressions are separated.

Explanation

First expression:

XH'^'i'='XJ

XH         % implicitly take input A, save it to clipboard H
'^'        % push literal '^'
i          % take input B
'='        % push literal '='
XJ         % copy '=' to clipboard J, we'll use this twice more so it's worth it

Second expression:

2G:"H'*']xJ

2G         % paste the second input (B) again
:"         % do the following B times
  H        % paste A from clipboard H
  '*'      % push literal '*'
]          % end loop
x          % delete the final element (at the moment we have a trailing *)
J          % paste '=' from clipboard J

Third expression:

&Gq^:"H'+']xJ

&G         % paste all of the input, ie push A and B
q          % decrement B
^          % power, giving A^(B-1)
:"         % do the following A^(B-1) times 
  H        % paste A from clipboard H
  '+'      % push literal '+'
]          % end loop
x          % delete the final element (at the moment we have a trailing +)
J          % paste '=' from clipboard J

Fourth expression:

&G^:"1'+']x

&G         % paste all of the input, ie push A and B
^          % power, giving A^B
:"         % do the following A^B times 
  1        % push 1
  '+'      % push '+'
]          % end loop
x          % delete the final element (at the moment we have a trailing +)
           % (implicit) convert all to string and display
share|improve this answer

JavaScript (ES7), 78 bytes

Takes input in currying syntax (a)(b). Outputs a string.

a=>b=>a+'^'+b+(g=o=>'='+a+('+*'[+!o]+a).repeat(b-1))()+g(b=a**~-b)+g(b*=a,a=1)

Test cases

let f =

a=>b=>a+'^'+b+(g=o=>'='+a+('+*'[+!o]+a).repeat(b-1))()+g(b=a**~-b)+g(b*=a,a=1)

console.log(f(2)(2));
console.log(f(2)(3));
console.log(f(2)(4));
console.log(f(2)(5));
console.log(f(3)(2));
console.log(f(3)(3));
console.log(f(3)(4));
console.log(f(3)(5));
console.log(f(4)(2));
console.log(f(4)(3));
console.log(f(10)(2));
console.log(f(13)(2));

share|improve this answer

05AB1E, 30 bytes

mUð¹'^²J'*¹«²×'+¹«X¹÷ׄ+1X×)€¦

Explanation:

mU                               # Store a^b in variable X
  ð                              # Push a space character to the stack (will be deleted at the end, but this is needed to keep the character count low)
   ¹'^²J                         # Push the string "a^b" to the stack
        '*¹«                     # Push the string "*a" to the stack
            ²×                   # Repeat b times
              '+¹«               # Push the string "+a" to the stack
                  «X¹÷×          # Repeat a^b / a times
                       „+1       # Push the string "+1" to the stack
                          X×     # Repeat a^b times
                            )    # Wrap stack to array
                             €¦  # Remove the first character from each element in the array

Try it online!

share|improve this answer

Ruby, 52 bytes

->a,b{[[a,b]*?^,[a]*b*?*,[a]*a**~-b*?+,[1]*a**b*?+]}
share|improve this answer

Cubix, 238 234 217 bytes

Saved 4 bytes thanks to ETHProductions

........................v.<...../#\...vt0....<!w.IO'^ou>'=o;\$o;\s+'q:q\.;sq\Uq:OI;.....q$s.su-.(Uq^.u1.s'*s.../....sqq.'1r.!tu:sqq.*..../;\....O?^.*sr/\#;;Ur\;/...........#(U-/;U;q....\r;\sU.........Ut!\.;.;*;r1-!@;^

Try it online!

Try it here

Explanation

First of all, the expanded cube (currently 7x7x7) looks like this:

              . . . . . . .
              . . . . . . .
              . . . . . . .
              . . . v . < .
              . . . . / # \
              . . . v t 0 .
              . . . < ! w .
I O ' ^ o u > ' = o ; \ $ o ; \ s + ' q : q \ . ; s q \
U q : O I ; . . . . . q $ s . s u - . ( U q ^ . u 1 . s
' * s . . . / . . . . s q q . ' 1 r . ! t u : s q q . *
. . . . / ; \ . . . . O ? ^ . * s r / \ # ; ; U r \ ; /
. . . . . . . . . . . # ( U - / ; U ; q . . . . \ r ; \
s U . . . . . . . . . U t ! \ . ; . ; * ; r 1 - ! @ ; ^
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
              . . . . . . .
              . . . . . . .
              . . . . . . .
              . . . . . . .
              . . . . . . .
              . . . . . . .
              . . . . . . .

The code consists of 8 steps, with two loops. I'll go over the code part by part.

Step 1 (A^B)

I O ' ^ o u >
U q : O I ; .

This block of code is on the left-most side, so the IP (instruction pointer) starts at the top-left. It executes the following commands (excluding IP control codes).

IO'^o;IO:q # Explanation
I          # Push the first input (A) to the stack
 O         # Output the top element of the stack
  '^       # Push the character "^" to the stack
    o      # And output that
     ;     # Then remove it from the stack
      I    # Push the second input (B) to the stack
       O   #   Output that and
        :  #   Duplicate and
         q #   Send one copy to the bottom of the stack

Now, the stack looks like this: B, A, B

Step 2 (prepare for print loop)

The print loop takes 3 arguments (the top 3 elements on the stack): A, B and C. A is an integer that is printed, B is the separator (character code) and C is the amount of times to repeat. Luckily, the loop also takes care of the requirement that the resulting string should end in A, not B.

We want to repeat A* exactly B times, with separator *. Note that the stack starts as B, A, B.

'*s # Explanation
'*  # Push the character code of *
  s # Swap the top two elements

Step 3/6/8 (the print loop)

The basic principle of the loop is that there's a reflector out of the loop, but there's a ! before that, so the IP will only leave the loop if the top element of the stack is falsy (0).

    . . . . / # \
<=s . . . . t 0 . <= s
    . . . . ! w .
 => ' = o ; \ $ o =>
    . . . . q $ s
    . . . . s q q
    . . . . O ? ^
    . . . . # ( .
    . . . . U t .

The arrows show how the IP enters and exits the loop. I removed some surrounding codes that are not used by the loop. As you can see, the IP first comes across four instructions before entering the loop.

'=o; #
'=   # Push the character code of =
  o  # Output it as a character
   ; # Remove it from the stack.

Since the character code is removed again, we reach the loop with the exact same stack as we entered this part. Now, the IP finds a mirror, reflecting the IP down. A few instructions later, there's a U, which turns the IP around, and at the top there are again 2 mirrors, turning the IP around once more, and just before it would reflect out of the loop, there's a !, which makes the IP skip the next instruction if the top item on the stack is truthy (not zero).

The first thing the loop does is move the top element on the stack (C, the amount of iterations) to the bottom (q). Then it swaps the top two elements (s, stack now looks like C, ..., B, A). Then it outputs the top of the stack as a number (O, top of the stack is A). Then it moves the bottom element to the top (#t, stack now looks like ..., B, A, C). It then decreases C by one and compares that value to zero ((?).

Since C is always positive or zero, we only have to consider two cases. If C is zero (i.e. this is the last iteration of the loop), the IP continues moving upward. If C is positive, it continues pointing right, but is then redirected upwards (^). In both cases, the top of the stack (C) is sent to the bottom again (Stack now looks like C-1, ..., B, A).

In the case of C=0, the IP hops over some instructions and then executes a w, which is a sidestep to the right. At that point the two paths merge again. The stack ends up like (C-1, ..., B, A), which is not exactly the same as in the other case, but the IP will exit the loop anyways, so it's not that important (as long as C-1 is on the bottom of the stack).

In the other case, the IP swaps the top elements (s, Stack: C-1, ..., A, B) and then outputs the top element.

The two paths have merged, and we move the bottom element of the stack to the top (C-1) and compare that to 0 (#t!). If this is 0, the next instruction is executed, reflecting the IP left. If this is not zero, the IP skips the next instruction, continuing the loop. Note that in this case, the stack is ..., A, B, C-1 again, so everything lines up.

Unfortunately, there's a o on the way out, which would output 0 as a character code, which is not what we want. As such, we have to place a $, which makes the IP skip the next instruction. However, if the IP reaches this $ from the bottom (which it will in the case of C=0), it will skip the sidestep instruction (which would break the loop). Therefore, we have to place another $ before the $ to skip the "skip next instruction" when coming from the south.

Step 4 (differentiating the IPs)

Since we use the above loop multiple times, but they all end up out of the loop in the same spot, we have to differentiate between multiple runs. First, we distinguishing between the * and + part, and if it's the + version, we have to distinguish between the number that is repeated. If it's 1, we just did the last iteration and we can stop the program.

Here's what it looks like on the cube:

=> ; \ . . . . . . . . . .
 . . s . . . . . . . . . .
 . . ' . . . . . . . . . .
 . . * . . . . . . . . . . 
 U - / . . . . . . . . . .
 ! \ . ; . ; * ; r 1 - ! @ 2>
   1 
   v

The fat arrow => shows where the IP enters this part, and the 1> arrow shows where it exits the first time, the 2> arrow shows where it exits the second time, and the third time, the program stops.

First comparison

;s'*-!
;      # Delete top element (`0`)
 s     # Swap the top two elements (Stack: A, B)
  '*   # Push the character code of *
    -  # Subtract the top two elements and push
       #  that to the stack (Stack: A, B, *, B-*)
     ! # Compare against 0. 
       # If B == *:
       #  Execute next instruction
       # Else:
       #  Skip next instruction

The next instruction sends the IP down, causing it to exit at arrow 1> (Go to step 5).

Second comparison

We are now going to compare A against 1 in roughly the same way. The stack initially looks like this: A, B, *, B-*

;;*;r1-!
;;       # Delete top two elements
  *      # Push multiplication of top two elements
   ;     #  and remove it again (these two instructions 
         #                       also part of another
         #                       loop, and this way we
         #                       can safely pass through)
    r    # Rotate top three elements (Stack: B, ..., A)
     1-  # Subtract the top element by 1 (Stack B, ..., A, 1, A-1)
       ! # Compare against 0. 
         # If A == 1:
         #  Execute next instruction
         # Else:
         #  Skip next instruction

The next instruction @ ends the program. If that was skipped, the IP continues going right.

Step 5 (preparing for "A+" (repeat A^(B-1)))

Sadly, Cubix doesn't have a power operator, so we need another loop. However, we need to clean up the stack first, which now contains B, A, B, *, B-*.

Cleaning up

;;;s1-rr; # Explanation
;;;       # Remove top 3 elements (Stack: B, A)
   s      # Swap top 2 elements (Stack: A, B)
    1-    # Push 1 and subtract top 2 elements (Stack: A, B, 1, B-1)
      rr  # Rotate twice (Stack: A, 1, B-1, B)
        ; # Remove top element (Stack: A, 1, B-1)

Calculating A^(B-1)

Another loop which works roughly the same as the print loop, but it's quite a bit shorter. The bottom part is moved to the left-most side of the cube in the final version, but it's moved back here for clarity and brevity.

   ( U
   ! t
=> \ # =>
   q .
s> * ; s>
   U s

The fat arrows (=>) show where the IP enters and exits the loop. The s> arrows show where the IP can safely go through some instructions.

This loop doesn't need any conditional things inside it, so it's pretty simple. Note that the stack initially is A, 1, B-1. The loop body is the following:

q*s;.#t( # Explanation
q        # Move the top element to the bottom
 *       # Multiply the top two elements
  s      # Shift the top two elements
   ;     # Delete the top element
    .    # No-op
     #t  # Move the bottom element to the top
       ( # Decrease it

The resulting stack is A, A^(B-1), 0, 3.

Cleaning up the stack (again)

Now we need to get to the print loop again, with the top of the stack containing A, +, A^(B-1). To do this, we execute the following.

;;s:qq:q'+s # Explanation
;;          # Delete top two elements (Stack: A, A^(B-1))
  s         # Shift elements
   :qq      # Duplicate the top element and send both to the
            # bottom (Stack: A, A, A^(B-1))
      :q    # Duplicate the top element and send it to the
            # bottom (Stack: A^(B-1), A, A, A^(B-1))
        '+  # Push the +
          s # Shift top two elements

The stack now contains: A^(B-1), A, A, +, A^(B-1), so we can enter the print loop.

Step 7 (preparing for last loop)

Explanation in progress...

share|improve this answer
2  
Yes please make an explanation, at least make an ungolfed cube net version – Destructible Watermelon 10 hours ago
    
I started writing the explanation. The last part will come soon... – Luke 7 mins ago

C (gcc), 156 149 bytes

#define q for(x=0;x
x,y;p(a,b,c){printf("%c%d",x?b:61,c),x+=a;}e(n,i){x=1;p(0,0,n);p(0,94,i);y=!!i;q<i;y*=n)p(1,42,n);q<y;)p(n,43,n);q<y;)p(1,43,1);}

-2 bytes if we can ignore 0 powers; y=!!i may become y=1

Try it online!

share|improve this answer

Pip, 38 35 34 bytes

33 bytes of code, +1 for -n flag.

Ya**b[gJ'^aRLbJ'*aRLy/aJ'+1XyJ'+]

Takes A and B as command-line arguments; prints one expression per line. Try it online!

Explanation

Ya**b is setup code: yank a**b into the y variable. After that, we have a list (in []) containing our four expressions:

  • gJ'^: take full arglist (here, a list containing a and b) and Join it on ^
  • aRLbJ'*: use RepeatList to create a list with b copies of a, then Join it on *
  • aRLy/aJ'+: use RL to create a list with y/a (i.e. a**(b-1)) copies of a, then Join it on +
  • 1XyJ'+: 1, string-multiplied by y, Joined on +

The list is printed with newline as the separator thanks to the -n flag.

share|improve this answer

Java 7, 170 bytes

String c(int a,int b){String s="=",r=a+"^"+b+s+a;double i=0,x=Math.pow(a,b);for(;++i<b;r+="*"+a);r+=s+a;for(i=0;++i<x/a;r+="+"+a);r+=s+1;for(i=0;++i<x;r+="+1");return r;}

Ungolfed:

String c(int a, int b){
  String s = "=",
         r = a+"^"+b+s+a;
  double i = 0,
         x = Math.pow(a,b);
  for(; ++i < b; r += "*"+a);
  r += s+a;
  for(i = 0; ++i < x/a; r += "+"+a);
  r += s+1;
  for(i = 0; ++i < x; r += "+1");
  return r;
}

Test code:

Try it here.

class M{
  static String c(int a,int b){String s="=",r=a+"^"+b+s+a;double i=0,x=Math.pow(a,b);for(;++i<b;r+="*"+a);r+=s+a;for(i=0;++i<x/a;r+="+"+a);r+=s+1;for(i=0;++i<x;r+="+1");return r;}

  public static void main(String[] a){
    System.out.println(c(2,2));
    System.out.println(c(2,3));
    System.out.println(c(2,4));
    System.out.println(c(2,5));
    System.out.println(c(3,2));
    System.out.println(c(3,3));
    System.out.println(c(3,4));
    System.out.println(c(3,5));
    System.out.println(c(4,2));
    System.out.println(c(4,3));
    System.out.println(c(10,2));
    System.out.println(c(13,2));
  }
}

Output:

2^2=2*2=2+2=1+1+1+1
2^3=2*2*2=2+2+2+2=1+1+1+1+1+1+1+1
2^4=2*2*2*2=2+2+2+2+2+2+2+2=1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1
2^5=2*2*2*2*2=2+2+2+2+2+2+2+2+2+2+2+2+2+2+2+2=1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1
3^2=3*3=3+3+3=1+1+1+1+1+1+1+1+1
3^3=3*3*3=3+3+3+3+3+3+3+3+3=1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1
3^4=3*3*3*3=3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3=1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1
3^5=3*3*3*3*3=3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3+3=1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1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+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1
share|improve this answer

Javascript 115 113 104 bytes

Thanks to @Neil and @TuukkaX for golfing off one byte each and @ETHproductions and Luke for golfing off 9 bytes

a=>b=>[a+'^'+b,(a+'*').repeat(b-1)+a,(a+'+').repeat(Math.pow(a,b-1)-1)+a,1+'+1'.repeat(Math.pow(a,b)-1)]

Try it Online

share|improve this answer
    
You could use the currying syntax: a=>b=>. – TuukkaX yesterday
    
Why not '=1'+'+1'.repeat? – Neil yesterday
    
You can save a further byte with .join: a=>b=>[a+'^'+b,(a+'*').repeat(b-1)+a,(a+'+').repeat(Math.pow‌​(a,b-1)-1)+a,1+'+1'.‌​repeat(Math.pow(a,b)‌​-1)].join`=` – ETHproductions 16 hours ago
    
If you interpret "The four expressions may be output in any reasonable way as long as they are in order and clearly distinct." to also include returning an array with four different elements, you don't even need the .join()... – Luke 13 hours ago

tinylisp repl, 178 186 bytes

(load library
(d W(q((_ N S #)(i(e # 1)(c N _)(W(c S(c N _))N S(s # 1
(d ^(q((x y)(i y(*(^ x(s y 1))x)1
(d X(q((A B)(list(list A(q ^)B)(W()A(q *)B)(W()A(q +)(^ A(s B 1)))(W()1(q +)(^ A B

Using the repl saves 8 bytes in implied closing parentheses at the ends of lines. Defines a function X which takes two numbers and returns a list of expressions. Each expression is parenthesized, with spaces around the operators (actually, it's a list of numbers and operator symbols):

((2 ^ 3) (2 * 2 * 2) (2 + 2 + 2 + 2) (1 + 1 + 1 + 1 + 1 + 1 + 1 + 1))

Hopefully this output format is acceptable. Try it online! (with several test cases).

Explanation

(load library)

We need two functions from the standard library: list and *.

(d W(q((_ N S #)(i(e # 1)(c N _)(W(c S(c N _))N S(s # 1))))))

Define a function W (short for "weave") that takes an accumulator _, a number N, a symbol S, and a count #. We will use this function to generate most our expressions: for example, (W () 2 (q +) 3) will result in (2 + 2 + 2).

If the count is 1 (e # 1), then cons the number to the front of the accumulator (c N _) and return that. Otherwise, recurse:

  • New accumulator is (c S(c N _)): the symbol and the number cons'd to the front of the previous accumulator;
  • N and S are the same;
  • New count is (s # 1): count - 1.

The accumulator idiom is needed to achieve proper tail recursion, preventing a recursion depth error. (This change is responsible for the +8 to the byte count. The 13^2 case didn't work in the previous version.)

(d ^(q((x y)(i y(*(^ x(s y 1))x)1))))

Unfortunately, the library has no exponentiation function at this time, so we have to define one. ^ takes x and y. If y is truthy (nonzero), we recurse with y-1 ((s y 1)) and multiply the result by x. Otherwise, y is zero and we return 1.

(Note: This function does not use proper tail recursion. I assume that the exponents will be small enough that it won't matter. Some experimenting on TIO indicated a maximum exponent of 325, which I would argue should be sufficient for this question. If the OP disagrees, I will change it.)

(d X(q((A B)(list(list A(q ^)B)(W()A(q *)B)(W()A(q +)(^ A(s B 1)))(W()1(q +)(^ A B))))))

Finally, the function we're interested in, X, takes A and B and returns a list of four items:

  • (list A(q ^)B): a list containing A, a literal ^, and B;
  • (W()A(q *)B): call W to get a list of B copies of A, interwoven with literal *;
  • (W()A(q +)(^ A(s B 1))): call W to get a list of A^(B-1) copies of A, interwoven with literal +;
  • (W()1(q +)(^ A B)): call W to get a list of A^B copies of 1, interwoven with literal +.
share|improve this answer

Pyth, 32 31 bytes

AQjMC,"^*++"[Q*]GH*^GtH]G*]1^GH

Takes input as [2,10], outputs as ["2^10", "2*2*2*2*2*2*2*2*2*2", "2+2+...

Explanation:

Q = eval(input())                     #  
G, H = Q                              #  AQ
operators = "^*++"                    #        "^*++"
operands = [Q,                        #              [Q
  [G]*H,                              #                *]GH
  G**(H-1)*[G]                        #                    *^GtH]G
  [1]*G**H                            #                           *]1^GH
]                                     #
map( lambda d: join(*d),              #    jM
     zip(operators, operands)         #      C,...
)                                     #

Try it here.

share|improve this answer

Perl, 81 bytes

78 bytes of code + -n flag (counted as 3 bytes since the code contains $').

$,=$/;print s/ /^/r,map{/\D/;join$&,($`)x$'}"$`*$'","$`+".$`**~-$',"1+".$`**$'

Try it online!

share|improve this answer

R, 147 bytes

w=switch;function(A,B)for(s in letters[1:4]){cat(rep(w(s,d=1,a=c(A,B),b=,c=A),w(s,a=1,b=B,c=A^B/A,d=A^B)),sep=w(s,a="^",b="*",d=,c="+"));cat("\n")}

Anonymous function that outputs the required, well, outputs, line-by-line. This solution makes extensive use of the switch function.

The switch function takes an expression (here, s) that evaluates to a number or a character string (see ?switch), followed by the alernatives corresponding to s. If an alternative is missing (e.g. switch(s, a=, b= "PPCG"), the next non-missing alternative is evaluated (in the example, s="a" outputs "PPCG").

The rep functions repeats (replicates, actually) its first argument the number of times indicated in the second argument.

cat, to finish, concatenate and prints the objects, with a separator that can be chosen with the sep = argument. The second cat function is here for the line-break.

Ungolfed:

f=function(A,B)
    for(s in letters[1:4]){
        cat(
            rep(switch(s, d = 1, a = c(A,B), b =, c = A),
            switch(s, a = 1, b = B, c = A^B/A, d = A^B)),
        sep=switch(s,a = "^", b = "*", d =, c = "+"))
    cat("\n")
}
share|improve this answer

CJam, 42 bytes

q_'#/)i*_'**\__:i:*_@0=i/@0=*'+*\'1*'+*]N*

Try it online!

share|improve this answer

Jelly, 29 bytes

*⁹’,¤;@
,`;1ẋ"ç
,W;çj"“^*++”Y

Try it online!

How?

*⁹’,¤;@ - Link 1, get list lengths: a, b
    ¤   - nilad followed by link(s) as a nilad
 ⁹      - right: b
  ’     - decrement: b-1
   ,    - pair with right: [b-1, b]
*       - exponentiate: [a^(b-1), a^b]
     ;@ - concatenate with reversed arguments: [b, a^(b-1), a^b]

,`;1ẋ"ç - Link 2, create lists: a, b
 `      - monad from dyad by repeating argument
,       - pair: [a, a]
  ;1    - concatenate 1: [a, a, 1]
      ç - last link (1) as a dyad: [b, a^(b-1), a^b]
     "  - zip with the dyad...
    ẋ   -     repeat list:  [[a, a, ..., a], [a, a, ..., a], [1, 1, ..., 1]]
                                <- b ->       <- a^(b-1) ->     <- a^b ->

,W;çj"“^*++”Y - Main link: a, b
,             - pair: [a, b]
 W            - wrap: [[a, b]]
   ç          - last link (2) as a dyad: [[a, a, ..., a], [a, a, ..., a], [1, 1, ..., 1]]
  ;           - concatenate [[a, b], [a, a, ..., a], [a, a, ..., a], [1, 1, ..., 1]]
      “^*++”  - list of characters: ['^', '*', '+', '+']
     "        - zip with the dyad...
    j         -     join: ["a^b", "a*a*...*a", "a+a+...+a", "1+1+...+1"]
            Y - join with line feeds
              - implicit print
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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