To naprawdę miłe krótkie wyzwanie.
Napisz funkcję lub procedurę, która ma dwa parametry, x
a y
i zwraca wynik bez użycia pętli, lub wbudowany w funkcji mocy.xy
Zwycięzca jest najbardziej kreatywnym rozwiązaniem i zostanie wybrany na podstawie największej liczby głosów po 3 dniach.
popularity-contest
math
restricted-source
CodyBugstein
źródło
źródło
exp(log(x)*y)
?Odpowiedzi:
APL (7)
Lewy argument jest podstawą, prawy argument jest wykładnikiem, np .:
Wyjaśnienie:
⍵/⍺
replikuje się⍺
⍵
czasy, np5 {⍵/⍺} 6
->5 5 5 5 5 5
×/
bierze produkt, np.×/5 5 5 5 5 5
->5×5×5×5×5×5
->15625
źródło
*/@$~
×/⍴⍨
C #: wykładniki zmiennoprzecinkowe
OK, to rozwiązanie jest dość kruche. Możesz go łatwo złamać, rzucając w niego absurdalnie ogromnymi liczbami, takimi jak 6. Ale działa pięknie dla takich rzeczy
DoublePower(1.5, 3.4)
i nie używa rekurencji!źródło
C ++
Co powiesz na niektóre meta programowanie szablonów? Zagina to, jakie były małe reguły, ale warto spróbować:
źródło
Pyton
Nie działa dla mocy niecałkowitych.
źródło
join
?eval('*'.join([str(x)] * y))
.**
operator, więc możesz mieć eval () d.Haskell - 25 chars
Following Marinus' APL version:
With mniip's comment and whitespace removed, 27 chars:
źródło
replicate y x
instead oftake y $ repeat x
f=(product.).flip replicate
is exactly the same number of chars.Python
If
y
is a positive integerźródło
JavaScript (ES6), 31
Usage:
Explanation:
The above function builds an expression which multiply
x
y
times then evaluates it.źródło
I'm surprised to see that nobody wrote a solution with the Y Combinator, yet... thus:
Python2
No loops, No vector/list operations and No (explicit) recursion!
źródło
fix
, upvoting him...C# : 45
Works for integers only:
źródło
return --y?x:x*P(x,y);
insteadbash & sed
No numbers, no loops, just an embarrasingly dangerous glob abuse. Preferably run in an empty directory to be safe. Shell script:
źródło
Javascript
Uses regular expressions to create an array of size y+1 whose first element is 1. Then, reduce the array with multiplication to compute power. When y=0, the result is the first element of the array, which is 1.
Admittedly, my goal was i) not use recursion, ii) make it obscure.
źródło
Mathematica
Probably cheating to use the fact that x^(1/y) = y√x
źródło
JavaScript
źródło
Golfscript, 8 characters (including I/O)
Explanation:
TLDR: another "product of repeated array" solution.
The expected input is two numbers, e.g.
2 5
. The stack starts with one item, the string"2 5"
.źródło
Ruby
Sample use:
This ultimately is the same as several previous answers: creates a y-length array every element of which is x, then takes the product. It's just gratuitously obfuscated to make it look like it's using the forbidden
**
operator.źródło
C, exponentiation by squaring
golfed version in 46 bytes (thanks ugoren!)
should be faster than all the other recursive answers so far o.O
slightly slower version in 45 bytes
źródło
b
,~-b/2 == b/2
.pow(n, x)
better than O(n)?"Haskell - 55
There's already a shorter Haskell entry, but I thought it would be interesting to write one that takes advantage of the
fix
function, as defined inData.Function
. Used as follows (in the Repl for the sake of ease):źródło
Q
9 chars. Generates array with
y
instances ofx
and takes the product.Can explicitly cast to float for larger range given int/long x:
źródło
Similar logic as many others, in PHP:
Run it with
php file.php 5 3
to get 5^3źródło
I'm not sure how many upvotes I can expect for this, but I found it somewhat peculiar that I actually had to write that very function today. And I'm pretty sure this is the first time any .SE site sees this language (website doesn't seem very helpful atm).
ABS
Works for negative exponents and rational bases.
I highlighted it in Java syntax, because that's what I'm currently doing when I'm working with this language. Looks alright.
źródło
Pascal
The challenge did not specify the type or range of x and y, therefore I figure the following Pascal function follows all the given rules:
No loop, no built-in power or exponentiation function, not even recursion or arithmetics!
źródło
J - 5 or 4 bytes
Exactly the same as marinus' APL answer.
For
x^y
:For
y^x
:For example:
x $~ y
creates a list ofx
repeatedy
times (same asy $ x
*/ x
is the product function,*/ 1 2 3
->1 * 2 * 3
źródło
Python
źródło
=/=
functionJavascript
With tail recursion, works if
y
is a positive integerźródło
Bash
Everyone knows
bash
can do whizzy map-reduce type stuff ;-)If thats too trolly for you then there's this:
źródło
C
Yet another recursive exponentiation by squaring answer in C, but they do differ (this uses a shift instead of division, is slightly shorter and recurses one more time than the other):
źródło
Mathematica
This works for integers.
Example
How it works
Table
makes a list ofy
x
's.Times
takes the product of all of them.`Another way to achieve the same end:
Example
źródło
Windows Batch
Like most of the other answers here, it uses recursion.
x^y is stored in the environment variable
z
.źródło
perl
Here's a tail recursive perl entry. Usage is echo $X,$Y | foo.pl:
Or for a more functional-type approach, how about:
źródło
Python
I am not sure if this is against the requirements, but if not here is my attempt.
źródło