langs a-z

This commit is contained in:
Ingy döt Net 2013-04-10 22:43:41 -07:00
parent db842d013d
commit d066446780
11389 changed files with 98361 additions and 1020 deletions

View file

@ -0,0 +1,25 @@
let pow one mul a n =
let rec g p x = function
| 0 -> x
| i ->
g (mul p p) (if i mod 2 = 1 then mul p x else x) (i/2)
in
g a one n
;;
pow 1 ( * ) 2 16;; (* 65536 *)
pow 1.0 ( *. ) 2.0 16;; (* 65536. *)
(* pow is not limited to exponentiation *)
pow 0 ( + ) 2 16;; (* 32 *)
pow "" ( ^ ) "abc " 10;; (* "abc abc abc abc abc abc abc abc abc abc " *)
pow [ ] ( @ ) [ 1; 2 ] 10;; (* [1; 2; 1; 2; 1; 2; 1; 2; 1; 2; 1; 2; 1; 2; 1; 2; 1; 2; 1; 2] *)
(* Thue-Morse sequence *)
Array.init 32 (fun n -> (1 - pow 1 ( - ) 0 n) lsr 1);;
(* [|0; 1; 1; 0; 1; 0; 0; 1; 1; 0; 0; 1; 0; 1; 1; 0;
1; 0; 0; 1; 0; 1; 1; 0; 0; 1; 1; 0; 1; 0; 0; 1|]
See http://en.wikipedia.org/wiki/Thue-Morse_sequence
*)

View file

@ -0,0 +1,9 @@
ex(a, b)={
my(c = 1);
while(b > 1,
if(b % 2, c *= a);
a = a^2;
b >>= 1
);
a * c
};

View file

@ -0,0 +1 @@
ex2(a, b) = a ^ b;

View file

@ -0,0 +1,23 @@
declare exp generic
(iexp when (fixed, fixed),
fexp when (float, fixed) );
iexp: procedure (m, n) returns (fixed binary (31));
declare (m, n) fixed binary (31) nonassignable;
declare exp fixed binary (31) initial (m), i fixed binary;
if m = 0 & n = 0 then signal error;
if n = 0 then return (1);
do i = 2 to n;
exp = exp * m;
end;
return (exp);
end iexp;
fexp: procedure (a, n) returns (float (15));
declare (a float, n fixed binary (31)) nonassignable;
declare exp float initial (a), i fixed binary;
if a = 0 & n = 0 then signal error;
if n = 0 then return (1);
do i = 2 to n;
exp = exp * a;
end;
return (exp);
end fexp;

View file

@ -0,0 +1,38 @@
Program ExponentiationOperator(output);
function intexp (base, exponent: integer): longint;
var
i: integer;
begin
if (exponent < 0) then
if (base = 1) then
intexp := 1
else
intexp := 0
else
begin
intexp := 1;
for i := 1 to exponent do
intexp := intexp * base;
end;
end;
function realexp (base: real; exponent: integer): real;
var
i: integer;
begin
realexp := 1.0;
if (exponent < 0) then
for i := exponent to -1 do
realexp := realexp / base
else
for i := 1 to exponent do
realexp := realexp * base;
end;
begin
writeln('2^30: ', intexp(2, 30));
writeln('2.0^30: ', realexp(2.0, 30));
end.

View file

@ -0,0 +1,7 @@
subset Natural of Int where { $^n >= 0 }
multi pow (0, 0) { fail '0**0 is undefined' }
multi pow ($base, Natural $exp) { [*] $base xx $exp }
multi pow ($base, Int $exp) { 1 / pow $base, -$exp }
sub infix:<***> ($a, $b) { pow $a, $b }

View file

@ -0,0 +1,2 @@
say pow .75, -5;
say .75 *** -5;

View file

@ -0,0 +1,20 @@
function pow($a, [int]$b) {
if ($b -eq -1) { return 1/$a }
if ($b -eq 0) { return 1 }
if ($b -eq 1) { return $a }
if ($b -lt 0) {
$rec = $true # reciprocal needed
$b = -$b
}
$result = $a
2..$b | ForEach-Object {
$result *= $a
}
if ($rec) {
return 1/$result
} else {
return $result
}
}

View file

@ -0,0 +1,46 @@
Procedure powI(base, exponent)
Protected i, result.d
If exponent < 0
If base = 1
result = 1
EndIf
ProcedureReturn result
EndIf
result = 1
For i = 1 To exponent
result * base
Next
ProcedureReturn result
EndProcedure
Procedure.f powF(base.f, exponent)
Protected i, magExponent = Abs(exponent), result.d
If base <> 0
result = 1.0
If exponent <> 0
For i = 1 To magExponent
result * base
Next
If exponent < 0
result = 1.0 / result
EndIf
EndIf
EndIf
ProcedureReturn result
EndProcedure
If OpenConsole()
Define x, a.f, exp
x = Random(10) - 5
a = Random(10000) / 10000 * 10
For exp = -3 To 3
PrintN(Str(x) + " ^ " + Str(exp) + " = " + Str(powI(x, exp)))
PrintN(StrF(a) + " ^ " + Str(exp) + " = " + StrF(powF(a, exp)))
PrintN("--------------")
Next
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit")
Input()
CloseConsole()
EndIf

View file

@ -0,0 +1 @@
: pow ( bp-n ) 1 swap [ over * ] times nip ;

View file

@ -0,0 +1 @@
2 5 ^math'pow

View file

@ -0,0 +1,7 @@
print " 11^5 = ";11^5
print " (-11)^5 = ";-11^5
print " 11^( -5) = ";11^-5
print " 3.1416^3 = ";3.1416^3
print " 0^2 = ";0^2
print " 2^0 = ";2^0
print " -2^0 = ";-2^0

View file

@ -0,0 +1,22 @@
const func integer: intPow (in var integer: base, in var integer: exponent) is func
result
var integer: result is 0;
begin
if exponent < 0 then
raise(NUMERIC_ERROR);
else
if odd(exponent) then
result := base;
else
result := 1;
end if;
exponent := exponent div 2;
while exponent <> 0 do
base *:= base;
if odd(exponent) then
result *:= base;
end if;
exponent := exponent div 2;
end while;
end if;
end func;

View file

@ -0,0 +1,44 @@
const func float: fltIPow (in var float: base, in var integer: exponent) is func
result
var float: result is 0.0;
local
var boolean: neg_exp is FALSE;
begin
if base = 0.0 then
if exponent < 0 then
result := Infinity;
elsif exponent = 0 then
result := 1.0;
else
result := 0.0;
end if;
else
if exponent < 0 then
exponent := -exponent;
neg_exp := TRUE;
end if;
# In the twos complement representation the most
# negative number is the only one where both the
# number and its negation are negative. When the
# exponent is proven to be to the most negative
# number fltIPow returns 0.0 .
if exponent >= 0 then
if odd(exponent) then
result := base;
else
result := 1.0;
end if;
exponent >>:= 1;
while exponent <> 0 do
base *:= base;
if odd(exponent) then
result *:= base;
end if;
exponent >>:= 1;
end while;
if neg_exp then
result := 1.0 / result;
end if;
end if;
end if;
end func;

View file

@ -0,0 +1,7 @@
$ syntax expr: .(). ^* .() is <- 4;
const func integer: (in var integer: base) ^* (in var integer: exponent) is
return intPow(base, exponent);
const func float: (in var float: base) ^* (in var integer: exponent) is
return fltIPow(base, exponent);

View file

@ -0,0 +1,19 @@
x@(Number traits) raisedTo: y@(Integer traits)
[
y isZero ifTrue: [^ x unit].
x isZero \/ [y = 1] ifTrue: [^ x].
y isPositive
ifTrue:
"(x * x raisedTo: y // 2) * (x raisedTo: y \\ 2)"
[| count result |
count: 1.
[(count: (count bitShift: 1)) < y] whileTrue.
result: x unit.
[count isPositive]
whileTrue:
[result: result squared.
(y bitAnd: count) isZero ifFalse: [result: result * x].
count: (count bitShift: -1)].
result]
ifFalse: [(x raisedTo: y negated) reciprocal]
].

View file

@ -0,0 +1,8 @@
x@(Float traits) raisedTo: y@(Float traits)
"Implements floating-point exponentiation in terms of the natural logarithm
and exponential primitives - this is generally faster than the naive method."
[
y isZero ifTrue: [^ x unit].
x isZero \/ [y isUnit] ifTrue: [^ x].
(x ln * y) exp
].

View file

@ -0,0 +1,20 @@
fun expt_int (a, b) = let
fun aux (x, i) =
if i = b then x
else aux (x * a, i + 1)
in
aux (1, 0)
end
fun expt_real (a, b) = let
fun aux (x, i) =
if i = b then x
else aux (x * a, i + 1)
in
aux (1.0, 0)
end
val op ** = expt_int
infix 6 **
val op *** = expt_real
infix 6 ***

View file

@ -0,0 +1,4 @@
- 2 ** 3;
val it = 8 : int
- 2.4 *** 3;
val it = 13.824 : real

View file

@ -0,0 +1,32 @@
include c:\cxpl\codes; \intrinsic 'code' declarations
func real Power(X, Y); \X raised to the Y power; (X > 0.0)
real X; int Y;
return Exp(float(Y) * Ln(X));
func IPower(X, Y); \X raised to the Y power
int X, Y;
int P;
[P:= 1;
while Y do
[if Y&1 then P:= P*X;
X:= X*X;
Y:= Y>>1;
];
return P;
];
int X, Y;
[Format(9, 0);
for X:= 1 to 10 do
[for Y:= 0 to 7 do
RlOut(0, Power(float(X), Y));
CrLf(0);
];
CrLf(0);
for X:= 1 to 10 do
[for Y:= 0 to 7 do
[ChOut(0, 9); IntOut(0, IPower(X, Y))];
CrLf(0);
];
]

View file

@ -0,0 +1,4 @@
10 PRINT e(3,2): REM 3 ^ 2
20 PRINT e(1.5,2.7): REM 1.5 ^ 2.7
30 STOP
9950 DEF FN e(a,b)=a^b