Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,16 @@
PROGRAM POWER
PROCEDURE POWER(A,B->POW) ! this routine handles only *INTEGER* powers
LOCAL FLAG%
IF B<0 THEN B=-B FLAG%=TRUE
POW=1
FOR X=1 TO B DO
POW=POW*A
END FOR
IF FLAG% THEN POW=1/POW
END PROCEDURE
BEGIN
POWER(11,-2->POW) PRINT(POW)
POWER(π,3->POW) PRINT(POW)
END PROGRAM

View file

@ -0,0 +1,25 @@
;; this exponentiation function handles integer, rational or float x.
;; n is a positive or negative integer.
(define (** x n) (cond
((zero? n) 1)
((< n 0) (/ (** x (- n)))) ;; x**-n = 1 / x**n
((= n 1) x)
((= n 0) 1)
((odd? n) (* x (** x (1- n)))) ;; x**(2p+1) = x * x**2p
(else (let ((m (** x (/ n 2)))) (* m m))))) ;; x**2p = (x**p) * (x**p)
(** 3 0) → 1
(** 3 4) → 81
(** 3 5) → 243
(** 10 10) → 10000000000
(** 1.3 10) → 13.785849184900007
(** -3 5) → -243
(** 3 -4) → 1/81
(** 3.7 -4) → 0.005335720890574502
(** 2/3 7) → 128/2187
(lib 'bigint)
(** 666 42) →
38540524895511613165266748863173814985473295063157418576769816295283207864908351682948692085553606681763707358759878656

View file

@ -0,0 +1,32 @@
' FB 1.05.0
' Note that 'base' is a keyword in FB, so we use 'base_' instead as a parameter
Function Pow Overload (base_ As Double, exponent As Integer) As Double
If exponent = 0.0 Then Return 1.0
If exponent = 1.0 Then Return base_
If exponent < 0.0 Then Return 1.0 / Pow(base_, -exponent)
Dim power As Double = base_
For i As Integer = 2 To exponent
power *= base_
Next
Return power
End Function
Function Pow Overload(base_ As Integer, exponent As Integer) As Double
Return Pow(CDbl(base_), exponent)
End Function
' check results of these functions using FB's built in '^' operator
Print "Pow(2, 2) = "; Pow(2, 2)
Print "Pow(2.5, 2) = "; Pow(2.5, 2)
Print "Pow(2, -3) = "; Pow(2, -3)
Print "Pow(1.78, 3) = "; Pow(1.78, 3)
Print
Print "2 ^ 2 = "; 2 ^ 2
Print "2.5 ^ 2 = "; 2.5 ^ 2
Print "2 ^ -3 = "; 2 ^ -3
Print "1.78 ^ 3 = "; 1.78 ^ 3
Print
Print "Press any key to quit"
Sleep

View file

@ -0,0 +1,14 @@
-- As for built-in power() function:
-- base can be either integer or float; returns float.
on pow (base, exp)
if exp=0 then return 1.0
else if exp<0 then
exp = -exp
base = 1.0/base
end if
res = float(base)
repeat with i = 2 to exp
res = res*base
end repeat
return res
end

View file

@ -0,0 +1,23 @@
proc `^`[T: float|int](base: T; exp: int): T =
var (base, exp) = (base, exp)
result = 1
if exp < 0:
when T is int:
if base * base != 1: return 0
elif (exp and 1) == 0: return 1
else: return base
else:
base = 1.0 / base
exp = -exp
while exp != 0:
if (exp and 1) != 0:
result *= base
exp = exp shr 1
base *= base
echo "2^6 = ", 2^6
echo "2^-6 = ", 2 ^ -6
echo "2.71^6 = ", 2.71^6
echo "2.71^-6 = ", 2.71 ^ -6

View file

@ -0,0 +1,9 @@
: powint(r, n)
| i |
1 n abs loop: i [ r * ]
n isNegative ifTrue: [ inv ] ;
2 3 powint println
2 powint(3) println
1.2 4 powint println
1.2 powint(4) println

View file

@ -0,0 +1,13 @@
function powi(atom b, integer i)
atom v=1
b = iff(i<0 ? 1/b : b)
i = abs(i)
while i>0 do
if and_bits(i,1) then v *= b end if
b *= b
i = floor(i/2)
end while
return v
end function
?powi(-3,-5)
?power(-3,-5)

View file

@ -0,0 +1,20 @@
see "11^5 = " + ipow(11, 5) + nl
see "pi^3 = " + fpow(3.14, 3) + nl
func ipow a, b
p2 = 1
for i = 1 to 32
p2 *= p2
if b < 0 p2 *= a ok
b = b << 1
next
return p2
func fpow a, b
p = 1
for i = 1 to 32
p *= p
if b < 0 p *= a ok
b = b << 1
next
return p

View file

@ -0,0 +1,20 @@
func expon(_, {.is_zero}) { 1 }
func expon(base, exp {.is_neg}) {
expon(1/base, -exp)
}
func expon(base, exp {.is_int}) {
var c = 1
while (exp > 1) {
c *= base if exp.is_odd
base *= base
exp >>= 1
}
return (base * c)
}
say expon(3, 10)
say expon(5.5, -3)

View file

@ -0,0 +1,8 @@
class Number {
method ⊙(exp) {
expon(self, exp)
}
}
say (3 ⊙ 10)
say (5.5 ⊙ -3)

View file

@ -0,0 +1,24 @@
# these implementations ignore negative exponents
def intpow (int m, int n)
if (< n 1)
return 1
end if
decl int ret
set ret 1
for () (> n 0) (dec n)
set ret (* ret m)
end for
return ret
end intpow
def floatpow (double m, int n)
if (or (< n 1) (and (= m 0) (= n 0)))
return 1
end if
decl int ret
set ret 1
for () (> n 0) (dec n)
set ret (* ret m)
end for
return ret
end floatpow

View file

@ -0,0 +1,15 @@
# 0^0 => 1
# NOTE: jq converts very large integers to floats.
# This implementation uses reduce to avoid deep recursion
def power_int(n):
if n == 0 then 1
elif . == 0 then 0
elif n < 0 then 1/power_int(-n)
elif ((n | floor) == n) then
( (n % 2) | if . == 0 then 1 else -1 end ) as $sign
| if (. == -1) then $sign
elif . < 0 then (( -(.) | power_int(n) ) * $sign)
else . as $in | reduce range(1;n) as $i ($in; . * $in)
end
else error("This is a toy implementation that requires n be integral")
end ;

View file

@ -0,0 +1,13 @@
def demo(x;y):
x | [ power_int(y), (log*y|exp) ] ;
demo(2; 3),
demo(2; 64),
demo(1.1; 1024),
demo(1.1; -1024)
# Output:
[8, 7.999999999999998]
[18446744073709552000, 18446744073709525000]
[2.4328178969536854e+42, 2.4328178969536693e+42]
[4.1104597317052596e-43, 4.1104597317052874e-43]