Il s'agit d'un court défi vraiment soigné.
Ecrire une fonction ou d' une procédure qui prend deux paramètres, x
et y
et renvoie le résultat de SANS utiliser des boucles ou des fonctions intégrées de puissance.xy
Le gagnant est la solution la plus créative et sera choisi en fonction du plus grand nombre de votes après 3 jours.
popularity-contest
math
restricted-source
CodyBugstein
la source
la source
exp(log(x)*y)
?Réponses:
APL (7)
L'argument de gauche est la base, l'argument de droite est l'exposant, par exemple:
Explication:
⍵/⍺
reproduit les⍺
⍵
temps, par exemple5 {⍵/⍺} 6
->5 5 5 5 5 5
×/
prend le produit, par exemple×/5 5 5 5 5 5
->5×5×5×5×5×5
->15625
la source
*/@$~
×/⍴⍨
C #: exposants en virgule flottante
OK, cette solution est assez fragile. Vous pouvez facilement le casser en y jetant des nombres ridiculement gros comme 6. Mais cela fonctionne à merveille pour des choses comme
DoublePower(1.5, 3.4)
, et n'utilise pas la récursivité!la source
C ++
Que diriez-vous d'une programmation de méta modèle? Cela détourne le peu de règles, mais ça vaut le coup:
la source
Python
Ne fonctionne pas pour les pouvoirs non entiers.
la source
join
?eval('*'.join([str(x)] * y))
.**
opérateur, donc vous pourriez avoir eval () d cela.Haskell - 25 caractères
Après la version APL de Marinus:
Avec le commentaire de mniip et les espaces supprimés, 27 caractères:
la source
replicate y x
au lieu detake y $ repeat x
f=(product.).flip replicate
c'est exactement le même nombre de caractères.Python
Si
y
est un entier positifla source
JavaScript (ES6), 31
Usage:
Explication:
La fonction ci-dessus construit une expression qui se multiplie
x
y
puis l'évalue.la source
Je suis surpris de voir que personne n'a encore écrit de solution avec le Y Combinator ... ainsi:
Python2
Pas de boucles, pas d'opérations vectorielles / liste et pas de récursivité (explicite)!
la source
fix
, upvoting him...C# : 45
Works for integers only:
la source
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:
la source
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.
la source
Mathematica
Probably cheating to use the fact that x^(1/y) = y√x
la source
JavaScript
la source
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"
.la source
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.la source
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
la source
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):la source
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:
la source
Similar logic as many others, in PHP:
Run it with
php file.php 5 3
to get 5^3la source
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.
la source
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!
la source
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
la source
Python
la source
=/=
functionJavascript
With tail recursion, works if
y
is a positive integerla source
Bash
Everyone knows
bash
can do whizzy map-reduce type stuff ;-)If thats too trolly for you then there's this:
la source
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):
la source
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
la source
Windows Batch
Like most of the other answers here, it uses recursion.
x^y is stored in the environment variable
z
.la source
perl
Here's a tail recursive perl entry. Usage is echo $X,$Y | foo.pl:
Or for a more functional-type approach, how about:
la source
Python
I am not sure if this is against the requirements, but if not here is my attempt.
la source