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,56 @@
PROGRAM ROOTS_FUNCTION
!VAR E,X,STP,VALUE,S%,I%,LIMIT%,X1,X2,D
FUNCTION F(X)
F=X*X*X-3*X*X+2*X
END FUNCTION
BEGIN
X=-1
STP=1.0E-6
E=1.0E-9
S%=(F(X)>0)
PRINT("VERSION 1: SIMPLY STEPPING X")
WHILE X<3.0 DO
VALUE=F(X)
IF ABS(VALUE)<E THEN
PRINT("ROOT FOUND AT X =";X)
S%=NOT S%
ELSE
IF ((VALUE>0)<>S%) THEN
PRINT("ROOT FOUND AT X =";X)
S%=NOT S%
END IF
END IF
X=X+STP
END WHILE
PRINT
PRINT("VERSION 2: SECANT METHOD")
X1=-1.0
X2=3.0
E=1.0E-15
I%=1
LIMIT%=300
LOOP
IF I%>LIMIT% THEN
PRINT("ERROR: FUNCTION NOT CONVERGING")
EXIT
END IF
D=(X2-X1)/(F(X2)-F(X1))*F(X2)
IF ABS(D)<E THEN
IF D=0 THEN
PRINT("EXACT ";)
ELSE
PRINT("APPROXIMATE ";)
END IF
PRINT("ROOT FOUND AT X =";X2)
EXIT
END IF
X1=X2
X2=X2-D
I%=I%+1
END LOOP
END PROGRAM

View file

@ -0,0 +1,11 @@
(lib 'math.lib)
Lib: math.lib loaded.
(define fp ' ( 0 2 -3 1))
(poly->string 'x fp) → x^3 -3x^2 +2x
(poly->html 'x fp) → x<sup>3</sup> -3x<sup>2</sup> +2x
(define (f x) (poly x fp))
(math-precision 1.e-6) → 0.000001
(root f -1000 1000) → 2.0000000133245677 ;; 2
(root f -1000 (- 2 epsilon)) → 1.385559938161431e-7 ;; 0
(root f epsilon (- 2 epsilon)) → 1.0000000002190812 ;; 1

View file

@ -0,0 +1,12 @@
: findRoots(f, a, b, st)
| x y lasty |
a f perform dup ->y ->lasty
a b st step: x [
x f perform -> y
y ==0 ifTrue: [ System.Out "Root found at " << x << cr ]
else: [ y lasty * sgn -1 == ifTrue: [ System.Out "Root near " << x << cr ] ]
y ->lasty
] ;
: f(x) x 3 pow x sq 3 * - x 2 * + ;

View file

@ -0,0 +1,21 @@
load "stdlib.ring"
function = "return pow(x,3)-3*pow(x,2)+2*x"
rangemin = -1
rangemax = 3
stepsize = 0.001
accuracy = 0.1
roots(function, rangemin, rangemax, stepsize, accuracy)
func roots funct, min, max, inc, eps
oldsign = 0
for x = min to max step inc
num = sign(eval(funct))
if num = 0
see "root found at x = " + x + nl
num = -oldsign
else if num != oldsign and oldsign != 0
if inc < eps
see "root found near x = " + x + nl
else roots(funct, x-inc, x+inc/8, inc/8, eps) ok ok ok
oldsign = num
next

View file

@ -0,0 +1,20 @@
func f(x) {
x*x*x - 3*x*x + 2*x;
}
var step = 0.001;
var start = -1;
var stop = 3;
range(start+step, stop, step).each { |x|
static sign = false;
given (var value = f(x)) {
when (0) {
say "Root found at #{x}";
}
case (sign && ((value > 0) != sign)) {
say "Root found near #{x}";
}
}
sign = value>0;
}

View file

@ -0,0 +1,22 @@
def sign:
if . < 0 then -1 elif . > 0 then 1 else 0 end;
def printRoots(f; lowerBound; upperBound; step):
lowerBound as $x
| ($x|f) as $y
| ($y|sign) as $s
| reduce range($x; upperBound+step; step) as $x
# state: [ox, oy, os, roots]
( [$x, $y, $s, [] ];
.[0] as $ox | .[1] as $oy | .[2] as $os
| ($x|f) as $y
| ($y | sign) as $s
| if $s == 0 then [$x, $y, $s, (.[3] + [$x] )]
elif $s != $os and $os != 0 then
($x - $ox) as $dx
| ($y - $oy) as $dy
| ($x - ($dx * $y / $dy)) as $cx # by geometry
| [$x, $y, $s, (.[3] + [ "~\($cx)" ])] # an approximation
else [$x, $y, $s, .[3] ]
end )
| .[3] ;

View file

@ -0,0 +1,14 @@
printRoots( .*.*. - 3*.*. + 2*.; -1.0; 4; 1/256)
[
0,
1,
2
]
printRoots( .*.*. - 3*.*. + 2*.; -1.0; 4; .001)
[
"~1.320318770141425e-18",
"~1.0000000000000002",
"~1.9999999999999993"
]