Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -1,4 +1,4 @@
Most all programming languages have a built-in implementation of exponentiation.
Most programming languages have a built-in implementation of exponentiation.
Re-implement integer exponentiation for both int<sup>int</sup> and float<sup>int</sup> as both a procedure, and an operator (if your language supports operator definition).
If the language supports operator (or procedure) overloading, then an overloaded form should be provided for both int<sup>int</sup> and float<sup>int</sup> variants.

View file

@ -0,0 +1,31 @@
defmodule My do
def exp(x,y) when is_integer(x) and is_integer(y) and y>=0 do
IO.write("int> ") # debug test
exp_int(x,y)
end
def exp(x,y) when is_integer(y) do
IO.write("float> ") # debug test
exp_float(x,y)
end
def exp(x,y), do: (IO.write(" "); :math.pow(x,y))
defp exp_int(_,0), do: 1
defp exp_int(x,y), do: Enum.reduce(1..y, 1, fn _,acc -> x * acc end)
defp exp_float(_,y) when y==0, do: 1.0
defp exp_float(x,y) when y<0, do: 1/exp_float(x,-y)
defp exp_float(x,y), do: Enum.reduce(1..y, 1, fn _,acc -> x * acc end)
end
list = [{2,0}, {2,3}, {2,-2},
{2.0,0}, {2.0,3}, {2.0,-2},
{0.5,0}, {0.5,3}, {0.5,-2},
{-2,2}, {-2,3}, {-2.0,2}, {-2.0,3},
]
IO.puts " ___My.exp___ __:math.pow_"
Enum.each(list, fn {x,y} ->
sxy = "#{x} ** #{y}"
sexp = inspect My.exp(x,y)
spow = inspect :math.pow(x,y) # For the comparison
:io.fwrite("~10s = ~12s, ~12s~n", [sxy, sexp, spow])
end)

View file

@ -0,0 +1,10 @@
pow(X, Y) when Y < 0 ->
1/pow(X, -Y);
pow(X, Y) when is_integer(Y) ->
pow(X, Y, 1).
pow(_, 0, B) ->
B;
pow(X, Y, B) ->
B2 = if Y rem 2 =:= 0 -> B; true -> X * B end,
pow(X * X, Y div 2, B2).

View file

@ -1,22 +1,15 @@
>>> import operator
>>> class num(int):
MULTIPLY = lambda x, y: x*y
class num(float):
# the following method has complexity O(b)
# rather than O(log b) via the rapid exponentiation
def __pow__(self, b):
print "Empowered"
return operator.__pow__(self+0, b)
return reduce(MULTIPLY, [self]*b, 1)
# works with ints as function or operator
print num(2).__pow__(3)
print num(2) ** 3
>>> x = num(3)
>>> x**2
Empowered
9
>>> class num(float):
def __pow__(self, b):
print "Empowered"
return operator.__pow__(self+0, b)
>>> x = num(2.5)
>>> x**2
Empowered
6.25
>>>
# works with floats as function or operator
print num(2.3).__pow__(8)
print num(2.3) ** 8

View file

@ -1,4 +1,4 @@
/*REXX program to show various (integer) exponentations. */
/*REXX program to show various (integer) exponentiations. */
say center('digits='digits(),79,'')
say '17**65 is:'
say 17**65

View file

@ -2,12 +2,7 @@ class Numeric
def pow(m)
raise TypeError, "exponent must be an integer: #{m}" unless m.is_a? Integer
puts "pow!!"
# below requires Ruby 1.8.7
Array.new(m, self).reduce(1, :*)
# for earlier versions of Ruby
#Array.new(m, self).inject(1) { |res, n| res * n }
end
end

View file

@ -0,0 +1,23 @@
Function pow(x,y)
pow = 1
If y < 0 Then
For i = 1 To Abs(y)
pow = pow * (1/x)
Next
Else
For i = 1 To y
pow = pow * x
Next
End If
End Function
WScript.StdOut.Write "2 ^ 0 = " & pow(2,0)
WScript.StdOut.WriteLine
WScript.StdOut.Write "7 ^ 6 = " & pow(7,6)
WScript.StdOut.WriteLine
WScript.StdOut.Write "3.14159265359 ^ 9 = " & pow(3.14159265359,9)
WScript.StdOut.WriteLine
WScript.StdOut.Write "4 ^ -6 = " & pow(4,-6)
WScript.StdOut.WriteLine
WScript.StdOut.Write "-3 ^ 5 = " & pow(-3,5)
WScript.StdOut.WriteLine