September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -1,19 +0,0 @@
% -spec attribute is for documentation and dialyzer static analysis purposes only.
% It does not constrain the function like a guard (when ... ).
-spec exp_int(X :: integer(), N :: non_neg_integer()) -> integer().
% X ^ 0
exp_int(X, 0) when is_integer(X) -> % is_integer guard is required, otherwise X would match anything.
1;
% X ^ odd
exp_int(X, N) when is_integer(X), N >= 1, N rem 2 == 1 ->
Part = exp_int(X, (N-1) div 2),
X * Part * Part;
% X ^ even
exp_int(X, N) when is_integer(X), N >= 2, N rem 2 == 0 ->
Part = exp_int(X, N div 2),
Part * Part.
% X ^ negative is excluded because it would return float.

View file

@ -1,21 +0,0 @@
% -spec attribute is for documentation and dialyzer static analysis purposes only.
% It does not constrain the function like a guard (when ... ).
-spec exp_float(X :: float(), N :: integer()) -> float().
% X ^ 0
exp_float(X, 0) when is_float(X) -> % is_float guard is required, otherwise X would match anything.
1.0;
% X ^ negative
exp_float(X, N) when is_float(X), N < 0 ->
1.0 / exp_float(X, -N);
% X ^ even
exp_float(X, N) when is_float(X), N >= 1, N rem 2 == 1 ->
Part = exp_float(X, (N-1) div 2),
X * Part * Part;
% X ^ odd
exp_float(X, N) when is_float(X), N >= 2, N rem 2 == 0 ->
Part = exp_float(X, N div 2),
Part * Part.

View file

@ -1,11 +0,0 @@
: pow ( n m -- n^m )
( written in Retro Forth )
1 2rot
[
dup 1 and 0 <>
[ [ tuck * swap ] dip ] ifTrue
[ dup * ] dip
1 >>
dup 0 <>
] while
drop drop ;

View file

@ -0,0 +1,7 @@
function pow(base::Number, exp::Integer)
r = one(base)
for i = 1:exp
r *= base
end
return r
end

View file

@ -0,0 +1,43 @@
// version 1.0.6
infix fun Int.ipow(exp: Int): Int =
when {
this == 1 -> 1
this == -1 -> if (exp % 2 == 0) 1 else -1
exp < 0 -> throw IllegalArgumentException("invalid exponent")
exp == 0 -> 1
else -> {
var ans = 1
var base = this
var e = exp
while (e > 0) {
if (e and 1 == 1) ans *= base
e = e shr 1
base *= base
}
ans
}
}
infix fun Double.dpow(exp: Int): Double {
var ans = 1.0
var e = exp
var base = if (e < 0) 1.0 / this else this
if (e < 0) e = -e
while (e > 0) {
if (e and 1 == 1) ans *= base
e = e shr 1
base *= base
}
return ans
}
fun main(args: Array<String>) {
println("2 ^ 3 = ${2 ipow 3}")
println("1 ^ -10 = ${1 ipow -10}")
println("-1 ^ -3 = ${-1 ipow -3}")
println()
println("2.0 ^ -3 = ${2.0 dpow -3}")
println("1.5 ^ 0 = ${1.5 dpow 0}")
println("4.5 ^ 2 = ${4.5 dpow 2}")
}

View file

@ -0,0 +1,24 @@
extern crate num;
use num::traits::One;
use std::ops::Mul;
fn pow<T>(mut base: T, mut exp: usize) -> T
where T: Clone + One + Mul<T, Output=T>
{
if exp == 0 { return T::one() }
while exp & 1 == 0 {
base = base.clone() * base;
exp >>= 1;
}
if exp == 1 { return base }
let mut acc = base.clone();
while exp > 1 {
exp >>= 1;
base = base.clone() * base;
if exp & 1 == 1 {
acc = acc * base.clone();
}
}
acc
}

View file

@ -0,0 +1,16 @@
fcn pow(n,exp){
reg v;
if(n.isType(1)){ // Int
if (exp<0) return(if(n*n!=1) 0 else (if(exp.isOdd) n else 1));
v=1;
}else{
if(exp<0){ n=1.0/n; exp=-exp; }
v=1.0;
}
while(exp>0){
if(exp.isOdd) v*=n;
n*=n;
exp/=2;
}
v
}

View file

@ -0,0 +1,4 @@
println("2^6 = %d".fmt(pow(2,6)));
println("2^-6 = %d".fmt(pow(2,-6)));
println("2.71^6 = %f".fmt(pow(2.71,6)));
println("2.71^-6 = %f".fmt(pow(2.71,-6)));