Expand Exponentiation

31

3

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

Calvin's Hobbies

Posted 2017-02-20T03:27:27.193

Reputation: 84 000

@JonathanAllan Yeah, you got all the typos I saw. Nice job! You get nothing. – R. Kap – 2017-02-20T03:51:20.243

2You get thanks from me @JonathanAllan. R. Kap had me worrying about all my 3's – Calvin's Hobbies – 2017-02-20T03:52:36.200

Well, (luckily) there were no misplaced ='s to the left of any 3's. – R. Kap – 2017-02-20T03:55:26.100

5Please don't do "expand knuth's up arrows notation" next time – Matthew Roh – 2017-02-20T09:06:23.540

1If 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 – 2017-02-20T12:41:02.060

Funny. Just today, I thought of making a challenge like this. But my idea was just for expanding squaring, by showing N copies of N, adding the first N odds, or the sum of N times its binary representation's powers of 2. – mbomb007 – 2017-02-20T22:06:42.683

@Martin that sounds ok – Calvin's Hobbies – 2017-02-21T00:57:29.757

Answers

11

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!

Jonathan Allan

Posted 2017-02-20T03:27:27.193

Reputation: 67 804

Can't you replace (b-1) with ~-b ? – Dada – 2017-02-20T08:39:16.433

Yes you are right, I missed that. – Jonathan Allan – 2017-02-20T09:28:16.440

1you 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 – 2017-02-20T12:31:31.447

@Erwan yes, I was planning on using f strings when I returned (now). I can save a couple more too. – Jonathan Allan – 2017-02-20T18:58:06.160

1Always good to see the tadpole operators. – Jack Brounstein – 2017-02-20T23:50:30.257

11

Cubix, 238 234 217 151 110 100 bytes

Saved 14 bytes thanks to ETHProductions

u'^.:s+.;;;\-?W?rsos\(rrOIO:ur>'=o;^u.;;.>$.vUo^'rsu1;;@!\q?s*su;;IOu*+qU../;(*\(s.;<..r:''uq....qu\

Expanded:

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

Try it online!

Try it here

Explanation

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

Step 1 (A^B)

          . . . . .
          . . . . .
          . . . . .
          . . . . .
          . . . . .
I O : u . . . . . . . . . . . . . . . .
U o ^ ' . . . . . . . . . . . . . . . .
; I O u . . . . . . / ; ( * \ . . . . .
? ? r : . . . . . . ? . . . \ ? ? ? ? ?
. . . . ? . . . . . ? . . . . . . . . .
          ? ? ? ? ?
          . . . . .
          . . . . .
          . . . . .
          . . . . .

This is the cube with the parts that are irrelevant to the first step removed. The question mark shows the no-ops the IP will visit, to make its path more clear.

IO:'^o;IO:r*(; # Explanation
I              # Push the first input (A)
 O             #   output that
  :            #   duplicate it
   '^          # Push the character "^"
     o         #   output that
      ;        #   pop it from the stack
       I       # Push the second input (B)
        O      #   output that 
         :     #   duplicate
          r    #   rotate top 3 elements
           *   # Push the product of the top two elements
            (  #   decrease it by one
             ; #   pop it from the stack (making the last
               #   two operations useless, but doing it
               #   this way saves 10B)

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

Step 2 (prepare for print loop)

The print loop takes 3 arguments (the top 3 elements on the stack): P, Q and R. P is the amount of repetitions, Q is the separator (character code) and R is the number to repeat. Luckily, the loop also takes care of the requirement that the resulting string should end in R, not Q.

We want to repeat A* exactly B times, so the separator is *. Note that the stack starts as A, B, A, B. Once again, I removed all irrelevant instructions. The IP start at the S pointing north.

          . . . . .
          . . . . .
          . . . . .
          . . . . .
          . . . . .
. . . . r . . . . . . . . . . . . . . .
. . . . r . . . . . . . . . . . . . . .
. . . . * . . . . . . . . . . . . . . .
. . . . ' . . . . . . . . . . . . . . .
. . . . S . . . . . . . . . . . . . . .
          . . . . .
          . . . . .
          . . . . .
          . . . . .
          . . . . .

'*rr # Explanation
'*   # Push * (Stack: A, B, A, B, *)
  rr # Rotate top three elements twice

The stack is now A, B, B, *, A.

Step 3/6/8 (the print loop)

Concept

E . . . . .
? r s o s u 
\ ( r r O <
. . . . . S

The IP enters the loop through S, pointing north, and exits the loop at E, pointing north again. For this explanation, the stack is set to [..., A, B, C]. The following instructions are executed. Note that the IP can't leave the loop before the question mark, so the first four instructions will always be executed.

Orr(?rsos # Explanation
O         # Output `C`
 rr       # Rotate top three elements twice (Stack: [..., B, C, A])
   (      # Decrease A by one (Stack: [..., B, C, A-1])
    ?     # If top of stack (A) > 0:
     r    #    Rotate top of stack (Stack: [..., A-1, B, C])
      s   #    Swap top elements (Stack: [..., A-1, C, B])
       o  #    Output top of stack (B) as character code
        s #    Swap top elements (Stack: [..., A-1, B, C]
          #
          # ... and repeat ...

Implementation

Here's the cube again, with the irrelevant parts removed. The IP starts at S, pointing east.

          . . . . .
          . . . . .
          . . . . .
          ? r s o s
          \ ( r r O
. . . . . S ' = o ; ^ u . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
          . . . . .
          . . . . .
          . . . . .
          . . . . .
          . . . . .

As you can see, the IP comes across four instructions before it enters the loop. Since the character code is removed again, we reach the loop with the exact same stack as we entered this part.

'=o; # Explanation
'=   # Push =
  o  #     Output
   ; #     Pop from stack

Inside the loop, the explanation above holds.

Step 4 (differentiating the IPs)

Since we use the above loop multiple times, and they all cause the IP to end up in the same spot, we have to differentiate between multiple runs. First, we can distinguish between the separator (first run has a *, whereas runs two and three have a + as separator). We can differentiate between runs 2 and 3 by checking the value of the number that is repeated. If that is one, the program should terminate.

First comparison

Here's what it looks like on the cube. The IP starts at S and points north. The stack contains [..., * or +, A or 1, 0]. The number 1 shows where the IP will end up if this is the first loop (pointing north) and the number 2 shows where the IP will end up if this is the second (or third) loop (pointing east).

          u ' . . .
          s + . 1 .
          ; \ - ? 2
          S . . . .
          . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
          . . . . .
          . . . . .
          . . . . .
          . . . . .
          . . . . .

;s'+-? # Explanation
;      # Delete top element (0)
 s     # Swap the top two elements (Stack: 1/A, */+)
  '+   # Push the character code of + 
    -  # Subtract the top two elements and push
       #  that to the stack (Stack: 1/A, */+, +, (*/+)-+)
     ? # Changes the direction based on the top
       # item on the stack. If it's 0 (if (*/+) == +)
       # the IP continues going right, otherwise, it
       # turns and continues going north.

If the IP now is at 1, the stack is [A, *, +, -1]. Otherwise, the stack is [A or 1, +, +, 0]. As you can see, there still is an unknown in the stack of the second case, so we have to do another comparison.

Second comparison

Because the IP has gone through step 5, the stack looks like this: [A^(B-1) or nothing, A or 1, +, +, 0]. If the first element is nothing, the second element is 1, and the reverse holds too. The cube looks like this, with the IP starting at S and pointing east. If this is the second loop, the IP ends up at E, pointing west. Otherwise, the program hits the @ and terminates.

          . . . . .
          . . . . ;
          . . . S W
          . . . . .
          . . . . .
. . . . . . . . . . . . . ; . . . . . .
. . . . . . . . . E @ ! \ q . . . . . .
. . . . . . . . . . . . ( * . . . . . .
. . . . . . . . . . . . q u . . . . . .
. . . . . . . . . . . . . . . . . . . .
          . . . . .
          . . . . .
          . . . . .
          . . . . .
          . . . . .

The instructions executed that don't do anything to the control flow are listed below.

;;q*q(!@
;;       # Delete top two elements (Stack [A^(B-1)/null, A/1, +])
  q      # Send top element to the bottom (Stack [+, A^(B-1)/0, A/1])
   *     # Push product of top two elements 
         #    (Stack [+, A^(B-1)/0, A/1, A^B/0])
    q    # Send top element to the bottom 
         #    (Stack [A^B/0, +, A^(B-1)/0, A/1])
     (   # Decrease the top element by 1 
         #    (Stack [A^B/0, +, A^(B-1)/0, (A-1)/0])
      !  # If (top element == 0):
       @ #  Stop program

The stack is now [A^B, +, A^(B-1), A-1], provided the program didn't terminate.

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, *, +, -1].

Cleaning up

Here's the cube again. As usual, the IP starts at S (pointing north), and ends at E, pointing west.

          . . . ? .
          . . . ; .
          . . . S .
          . . . . .
          . . . . .
. . . . . . . . . . . . . . . . > $ . v
. . . . . . . . . . . . . . . . . . . ;
. . . . . . . . . . . . . . . . . . E <
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
          . . . . .
          . . . . .
          . . . . .
          . . . . .
          . . . . .

;; # Explanation
;; # Remove top 2 elements (Stack: [B, A, *])

Calculating A^(B-1)

Another loop which works roughly the same as the print loop, but it's a bit more compact. The IP starts at S, pointing west, with stack [B, A, *]. The IP exits at E pointing north.

          . . . . .
          . . . . .
          . . . . .
          . . . . .
          . . . . .
. . . . . . . . . . . . . . E . . . . .
. . . . . . . . . . . . . . ? s * s u .
. . . . . . . . . . . . . . \ ( s . ; S
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
          . . . . .
          . . . . .
          . . . . .
          . . . . .
          . . . . .

The loop body is the following.

;s(?s*s # Explanation
;       # Pop top element.
 s      # Shift top elements.
  (     # Decrease top element by one
   ?    # If not 0:
    s   #    Shift top elements again
     *  #    Multiply
      s #    Shift back
        #
        # ... and repeat ...

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

Cleaning up the stack (again)

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

          . . ^ ? :
          . . . . .
          . . . . .
          . . . . .
          E . . . .
. . . . . s . . . . . . . . ; . . $ . .
. . . . . + q U . . . . . . S . . s . .
. . . . . ' u q . . . . . . . . . ? . .
. . . . . . . ? . . . . . . . . . ? . .
. . . . . . . ? . . . . . . . . . ? . .
          . . ? . .
          . . ? . .
          . . ? . .
          . . ? . .
          . . ? . .

;:$sqq'+s # Explanation
;         # Delete top element (Stack: [A, A^(B-1)])
 :        # Copy top element
  $s      # No-op
    qq    # Send top two elements to the bottom
          #   (Stack: [A^(B-1), A^(B-1), A])
      '+  # Push +
          #   (Stack: [A^(B-1), A^(B-1), A, +])
        s # Swap top two elements
          #   (Stack: [A^(B-1), A^(B-1), +, A])

Step 7 (preparing for last loop)

The stack is now [A^B, +, A^(B-1), A-1], the IP starts at S, going west, and ends at E, going right.

          . . . . .
          . . . . .
          . . . . .
          . . . . .
          . . . . .
. . . . . E . . . . . . . . . . . . . .
. . . . . . u 1 ; ; S . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
          . . . . .
          . . . . .
          . . . . .
          . . . . .
          . . . . .

The instructions executed:

;;1 # Explanation
;;  # Delete top two elements
  1 # Push 1

The stack now looks like [A^B, +, 1], and the IP is about to enter the print loop, so we're done.

Luke

Posted 2017-02-20T03:27:27.193

Reputation: 4 675

2Yes please make an explanation, at least make an ungolfed cube net version – Destructible Lemon – 2017-02-21T00:08:00.690

I have completed the explanation – Luke – 2017-02-24T18:43:19.450

Who made this language... It's like hex-agony but cube-agony. Someone make a 3D hex-agony. – Magic Octopus Urn – 2017-03-21T20:55:55.103

ETHproductions did. After I created this program, I added a few commands, and I'm currently working on a special code page for this, because it's such an awesome language. – Luke – 2017-03-21T21:43:02.360

1@carusocomputing And what polyhedron would be a 3D hex, hm? – mbomb007 – 2017-04-05T20:14:01.147

@mbomb007 Ha, good point! I suppose it'd be something closer to a soccer ball. "Truncated icosahedrony" :P? – Magic Octopus Urn – 2017-04-05T21:17:46.537

1You could probably make a language that runs on a 3D structure of tessellated tetrahedrons. – mbomb007 – 2017-04-05T21:21:32.590

7

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

B. Mehta

Posted 2017-02-20T03:27:27.193

Reputation: 763

6

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

Arnauld

Posted 2017-02-20T03:27:27.193

Reputation: 111 334

3

Ruby, 52 bytes

->a,b{[[a,b]*?^,[a]*b*?*,[a]*a**~-b*?+,[1]*a**b*?+]}

G B

Posted 2017-02-20T03:27:27.193

Reputation: 11 099

3

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!

Okx

Posted 2017-02-20T03:27:27.193

Reputation: 15 025

¹²'^ý ¹'*¹«²<׫¹²m¹÷¹s×S'+ý 1¹²m×S'+ý», why you beat me by 5 bytes :(? – Magic Octopus Urn – 2017-03-21T20:53:05.697

1Because I am. :P – Okx – 2017-03-21T20:59:50.997

For some reason I saw your answer and read "80 bytes" and thought to myself, "easy to beat", then I created mine with the goal of 80 in mind... Then again, I think mines shorter at some things and so is yours haha. – Magic Octopus Urn – 2017-03-21T21:01:58.877

¹²«¹²×¹¹²m©¹÷×1®×)vySN"^*++"èý, is closer ;). You're only winning by 1 now ;). – Magic Octopus Urn – 2017-03-21T21:19:47.670

2

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!

Ahemone

Posted 2017-02-20T03:27:27.193

Reputation: 608

2

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^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4*4=4+4+4+4+4+4+4+4+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

Kevin Cruijssen

Posted 2017-02-20T03:27:27.193

Reputation: 67 575

1

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

fəˈnɛtɪk

Posted 2017-02-20T03:27:27.193

Reputation: 4 166

You could use the currying syntax: a=>b=>. – Yytsi – 2017-02-20T05:52:19.353

Why not '=1'+'+1'.repeat? – Neil – 2017-02-20T08:48:16.413

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 – 2017-02-20T18:15:56.997

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 – 2017-02-20T20:52:03.027

1

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.

DLosc

Posted 2017-02-20T03:27:27.193

Reputation: 21 213

1

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

Jonathan Allan

Posted 2017-02-20T03:27:27.193

Reputation: 67 804

1

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

DLosc

Posted 2017-02-20T03:27:27.193

Reputation: 21 213

1

Brainfuck, 372 bytes

,.>++++++++[>++++++++++++>++++++++>+++++<<<-]>--.<,.>>---.>++>++++++++[<<<<------>>>>-]<<<<-[<.>>>>.>>+<<<<<-]<.>>>.<<++++++++[<------>-]<[>+>>>>+<<<<<-]>>>>>>>+<[->[-<<[->>>+>+<<<<]>>>>[-<<<<+>>>>]<<]>[-<+>]<<]>[<+>>+<-]>[<+>-]<<<<<<<<++++++++[>++++++<-]>>>>+>>-[<<<<<.>>>.>>-]<<<<<.>>.>>>>[<+>-]<<[->[->+>+<<]>>[-<<+>>]<<<]>>>++++++++[<<<++++++>>>-]<<<+>>-[<<.<.>>>-]<<.

Try it online!

Notes

  1. The two inputs must be chosen in such a way, that A**B does not exceed 255. This is because brainfuck can only store values of one byte.
  2. If one input is bigger than 9, use the next ASCII character. 10 becomes :, 11 becomes ; etc. This is because Brainfuck can only take inputs of one byte.

Explanation

Here is my somewhat commented code. I'll expand on this later.

,.                      print A
> +++++ +++
[
    > +++++ +++++ ++    set up 96 (for ^ sign)
    > +++++ +++         set up 64 (for = sign)
    > +++++             set up 40 (for plus and * sign)
    <<< -
]
> --.                   print ^
< ,.                    print B
>
> ---.                  print =
> ++                    prepare *

> +++++ +++             convert B from char code
[
    <<<< ----- -
    >>>> -
]

<<<< -                  print loop "A*"
[
    < .
    >>>> .
    >> +
    <<<<< -
]
< .                     print final A
>>> .                   print =


<< +++++ +++            convert A from char code
[
    < ----- -
    > -
]

<
[                       duplicate A
    > +
    >>>> +
    <<<<< -
]

>>>>>                   exponentiation (A**(B minus 1))
>>+<[->[-<<[->>>+>+<<<<]>>>>[-<<<<+>>>>]<<]>[-<+>]<<]<

>>
[                       duplicate
    < +
    >> +
    < -
]

>[<+>-]                 move

<<<<< <<< +++++ +++     convert A to char code
[
    > +++++ +
    < -
]

>>>> +                  convert * to plus sign
>> -                    print loop "A plus"
[
    <<<< < .
    >>> .
    >> -
]
<<<<< .                 print final A
>> .                    print =

>>>>                    move
[
    < +
    > -
]

                        multiply A**(B minus 1) by A
<<[->[->+>+<<]>>[-<<+>>]<<<]        

>>> +++++ +++           generate the char code for 1 (49)
[                           generate 8*6 = 48
    <<< +++++ +
    >>> -
]
<<< +                       add one

>> -                    print loop "1 plus"
[
    << .
    < .
    >>> -
]
<< .                    print final 1

Luke

Posted 2017-02-20T03:27:27.193

Reputation: 4 675

0

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.

busukxuan

Posted 2017-02-20T03:27:27.193

Reputation: 2 728

0

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!

Dada

Posted 2017-02-20T03:27:27.193

Reputation: 8 279

0

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

Frédéric

Posted 2017-02-20T03:27:27.193

Reputation: 2 059

0

CJam, 42 bytes

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

Try it online!

Business Cat

Posted 2017-02-20T03:27:27.193

Reputation: 8 927

0

k, 44 bytes

{"^*++"{x/$y}'(x,y;y#x;(x*y-1)#x;(*/y#x)#1)}

Test cases.

zgrep

Posted 2017-02-20T03:27:27.193

Reputation: 1 291

0

Cardinal 202 bytes

%:.~:#"^"."=">v
x            "~
             *.
     >~=088v "~
     v88+88< ?
     8       -
     8      x#<
     v     < 0
     >   t / <
v~.~ <#"="?<
-
#?"+"^t
0
V
#?"="  >"1"-v
/ {   <^"+"?<
>     ^

Will only work for numbers where the calculated value <256 due to limitations in the values that can be held by pointers in Cardinal

Try it Online

Explanation:

Step 1

%:.~:#"^"."="

Receives two numbers a and b as input and outputs as "a^b="
Passes a pointer with active value a and inactive value b

Step 2

        >v
        "~
        *.
>~=088v "~
v88+88< ?
8       -
8      x#<
v     < 0
>   t / <

Receives a pointer with active value a and inactive value b printing "a"+("*a")(b-1) times
Passes a pointer with active value a^(b-1) to the next part

Step 3

v~.~ <#"="?<
-
#?"+"^t
0
V
#?"="  
/ {   <
>     ^ 

Receives a pointer with value of a^(b-1) and outputs "=a"+("+a")repeated(a^(b-1)-1)times+"="
Passes a pointer with value a^b to the next part

Step 4

>"1"-v
^"+"?<

Receives a pointer with value a^b and prints out "1"+("+1")repeated a^b-1 times

fəˈnɛtɪk

Posted 2017-02-20T03:27:27.193

Reputation: 4 166

0

Retina, 89 88 bytes

*`,
^
+`(1+),1
$1*$1,
:`.1+.$

{`^[1+]+
a$&z
+`a(.+)z.1
$1+a$1z*
)`.a.+z.

:`a

+`11
1+1

Input is comma separated unary numbers.

Try it online!

CalculatorFeline

Posted 2017-02-20T03:27:27.193

Reputation: 2 608