Ver código fonte

Add a faster power function

with O(ln(n)) complexiy, instead of O(n)
DricomDragon 5 anos atrás
pai
commit
568ce763d4
1 arquivos alterados com 7 adições e 0 exclusões
  1. 7 0
      mypower.hs

+ 7 - 0
mypower.hs

@@ -1,3 +1,10 @@
 e1 x n = case n of
 	0 -> 1
 	_ -> x * e1 x (n - 1)
+
+e2 x n =
+	if n == 0
+		then 1
+		else if even n
+			then e2 (x * x) (div n 2)
+			else x * e2 (x * x) (div n 2)