# Work out $x^y$ using only addition and subtraction [duplicate]

The challenge is to implement a function or program that takes two numbers, $$\x\$$ and $$\y\$$ and return the result of $$\x^y\$$.

The program cannot use any other mathematical operation other than $$\+\$$ or $$\-\$$, so no multiplication or division. Functions built into languages are also not allowed.

The code in the least amount of bytes wins.

• Hi, maybe this question belongs to puzzling.stackexchange.com or math.stackexchange.com rather than here – Luis felipe De jesus Munoz Jan 16 '19 at 13:44
• In general, we avoid challenges of the type "do x without y". – Adám Jan 16 '19 at 13:53
• Are x and y guaranteed to be integers? What counts as "other mathematical operation"s ... does equality and inequality count? How about array or string multiplication that isn't a mathematical operator but creates copies of the array or string? – AdmBorkBork Jan 16 '19 at 13:59
• Duplicate of Calculate the a ^ b WITHOUT using *, / and ^ which itself is a duplicate of this which is a duplicate of this – Adám Jan 16 '19 at 14:12

# APL (Dyalog Unicode), 7 bytesSBCS

Anonymous tacit infix function. y is left argument and x is right argument.

(+/⍴)/⍴


Try it online!

⍴ cyclically reshape (gives y copies of x)

()/ reduce by the following function:

⍴ cyclically reshape to left argument copies of right argument

+/ sum

• I was groping toward this solution in a "traditional" mode; however, for some reason, I can never get APL to produce output on TIO - what might I be doing wrong? The test case I was trying with was +/4⍴+/3⍴3, to see if I was on the right track for 3^4. – Jeff Zeitlin Jan 16 '19 at 14:12
• @JeffZeitlin If you put code in TIO's Code field, you must use ⎕← to output. – Adám Jan 16 '19 at 14:16
• Thanks. I don't know a facepalm emoticon/emoji, but consider it included here... – Jeff Zeitlin Jan 16 '19 at 14:29
• @JeffZeitlin 🤦︎ – Adám Jan 16 '19 at 14:34

# Python 2, 53 bytes

lambda x,y:y>1and sum(x for _ in range(f(x,y-1)))or x


Try it online!