2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -14,10 +14,11 @@ From the [[wp:Trabb PardoKnuth algorithm|wikipedia entry]]:
'''print''' ''result''
The task is to implement the algorithm:
# Use the function <math>f(x) = |x|^{0.5} + 5x^3</math>
# Use the function: &nbsp; &nbsp; <big><math>f(x) = |x|^{0.5} + 5x^3</math></big>
# The overflow condition is an answer of greater than 400.
# The 'user alert' should not stop processing of other items of the sequence.
# Print a prompt before accepting '''eleven''', textual, numeric inputs.
# You may optionally print the item as well as its associated result, but the results must be in reverse order of input.
# The sequence S may be 'implied' and so not shown explicitly.
# ''Print and show the program in action from a typical run here''. (If the output is graphical rather than text then either add a screendump or describe textually what is displayed).
<br><br>

View file

@ -1,15 +1,24 @@
open random number list console format read
open monad io number string
run () =
writen "Please enter 11 numbers:" $
xs () |> iter
where xs () = [0..10] |> map (\_ -> readStr <| readn ())
f x = sqrt (toSingle x) + 5.0 * (x ** 3.0)
:::IO
take_numbers 0 xs = do
return $ iter xs
where f x = sqrt (toSingle x) + 5.0 * (x ** 3.0)
p x = x < 400.0
iter [] = ()
iter [] = return ()
iter (x::xs)
| p res = printfn "f({0}) = {1}" x res $ iter xs
| else = printfn "f({0}) :: Overflow" x $ iter xs
| p res = do
putStrLn (format "f({0}) = {1}" x res)
iter xs
| else = do
putStrLn (format "f({0}) :: Overflow" x)
iter xs
where res = f x
take_numbers n xs = do
x <- readAny
take_numbers (n - 1) (x::xs)
run ()
do
putStrLn "Please enter 11 numbers:"
take_numbers 11 []

View file

@ -0,0 +1,24 @@
defmodule Trabb_Pardo_Knuth do
def task do
Enum.reverse( get_11_numbers )
|> Enum.each( fn x -> perform_operation( &function(&1), 400, x ) end )
end
defp alert( n ), do: IO.puts "Operation on #{n} overflowed"
defp get_11_numbers do
ns = IO.gets( "Input 11 integers. Space delimited, please: " )
|> String.split
|> Enum.map( &String.to_integer &1 )
if 11 == length( ns ), do: ns, else: get_11_numbers
end
defp function( x ), do: :math.sqrt( abs(x) ) + 5 * :math.pow( x, 3 )
defp perform_operation( fun, overflow, n ), do: perform_operation_check_overflow( n, fun.(n), overflow )
defp perform_operation_check_overflow( n, result, overflow ) when result > overflow, do: alert( n )
defp perform_operation_check_overflow( n, result, _overflow ), do: IO.puts "f(#{n}) => #{result}"
end
Trabb_Pardo_Knuth.task

View file

@ -12,7 +12,7 @@ func main() {
// accept sequence
var s [11]float64
for i := 0; i < 11; {
if _, err := fmt.Scanf("%f", &s[i]); err == nil {
if n, _ := fmt.Scan(&s[i]); n > 0 {
i++
}
}
@ -33,6 +33,6 @@ func main() {
}
func f(x float64) (float64, bool) {
result := math.Pow(math.Abs(x), .5) + 5*x*x*x
result := math.Sqrt(math.Abs(x)) + 5*x*x*x
return result, result > 400
}

View file

@ -0,0 +1,24 @@
package main
import (
"fmt"
"math"
)
func f(t float64) float64 {
return math.Sqrt(math.Abs(t)) + 5*math.Pow(t, 3)
}
func main() {
var a [11]float64
for i := range a {
fmt.Scan(&a[i])
}
for i := len(a) - 1; i >= 0; i-- {
if y := f(a[i]); y > 400 {
fmt.Println(i, "TOO LARGE")
} else {
fmt.Println(i, y)
}
}
}

View file

@ -0,0 +1,18 @@
function f (x) return math.abs(x)^0.5 + 5*x^3 end
function reverse (t)
local rev = {}
for i, v in ipairs(t) do rev[#t - (i-1)] = v end
return rev
end
local sequence, result = {}
print("Enter 11 numbers...")
for n = 1, 11 do
io.write(n .. ": ")
sequence[n] = io.read()
end
for _, x in ipairs(reverse(sequence)) do
result = f(x)
if result > 400 then print("Overflow!") else print(result) end
end

View file

@ -0,0 +1,10 @@
local a, y = {}
function f (t)
return math.sqrt(math.abs(t)) + 5*t^3
end
for i = 0, 10 do a[i] = io.read() end
for i = 10, 0, -1 do
y = f(a[i])
if y > 400 then print(i, "TOO LARGE")
else print(i, y) end
end

View file

@ -1,45 +1,50 @@
/*REXX program to implement the Trabb─Pardo-Knuth algorithm for N numbers.*/
N=11 /*N is the number of numbers to be used*/
maxValue=400 /*the maximum value f(x) can have. */
compDigs=200 /*compute with this many decimal digits*/
showDigs=20 /* ··· but only show this many digits.*/
numeric digits compDigs /*the number of digits precision to use*/
say ' _____ ' /*vinculum.*/
/*REXX program implements the Trabb─Pardo-Knuth algorithm for N numbers (default is 11).*/
parse arg N .; if N=='' | N=="," then N=11 /*Not specified? Then use the default.*/
maxValue=400 /*the maximum value f(x) can have. */
wid= 20 /* ··· but only show this many digits.*/
frac= 5 /* ··· show this # of fractional digs.*/
numeric digits 200 /*the number of digits precision to use*/
say ' _____ ' /* ◄───── display a vinculum.*/
say 'function: ƒ(x) x + (5 * x^3)'
prompt= 'enter ' N " numbers for the Trabb─Pardo─Knuth algorithm: (or Quit)"
prompt= 'enter ' N " numbers for the Trabb─Pardo─Knuth algorithm: (or Quit)"
/*▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ */
do ask=0; say; say prompt; say; pull $; say /**/
if abbrev('QUIT',$,1) then exit /*does the user want to QUIT this pgm? */ /**/
do ask=0; say; say prompt; say; pull $; say /**/
if abbrev('QUIT',$,1) then do; say 'quitting.'; exit 1; end /**/
ok=0 /**/
select /*validate that there are N numbers. */ /**/
when $='' then say 'no numbers entered' /**/
when words($)<N then say 'not enough numbers entered' /**/
when words($)>N then say 'too many numbers entered' /**/
select /*validate there're N numbers.*/ /**/
when $='' then say "no numbers entered" /**/
when words($)<N then say "not enough numbers entered" /**/
when words($)>N then say "too many numbers entered" /**/
otherwise ok=1 /**/
end /*select*/ /**/
if \ok then iterate /* [↓] is max width*/ /**/
if \ok then iterate /* [↓] is max width.*/ /**/
w=0; do v=1 for N; _=word($,v); w=max(w,length(_)) /**/
if datatype(_,'N') then iterate /*numeric ? */ /**/
if datatype(_,'N') then iterate /*numeric ? */ /**/
say _ "isn't numeric"; iterate ask /**/
end /*v*/ /**/
leave /**/
end /*ask*/ /**/
say 'numbers entered: ' $; say /**/
/*▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ */
do i=N by -1 to 1; #=word($,i)/1 /*process nums in reverse. */
numeric digits compDigs; g=f(#) /*for func. ƒ, use big digs*/
numeric digits showdigs; g=g/1 /*scale down output digits.*/
gw=right('ƒ('#") ",w+7) /*nice formatted ƒ(number)*/
if g>maxValue then say gw "is > " maxValue ' ['g"]"
else say gw " = " g /*display the (good) result*/
end /*i*/
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
f: procedure; parse arg x; return sqrt(abs(x)) + 5 * x**3
/*────────────────────────────────────────────────────────────────────────────*/
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); i=; m.=9
numeric digits 9; numeric form; h=d+6; if x<0 then do; x=-x; i='i'; end
parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g*.5'e'_%2
do j=0 while h>9; m.j=h; h=h%2+1; end /*j*/
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/
numeric digits d; return (g/1)i /*make complex if X < 0.*/
say 'numbers entered: ' $
say
do i=N by -1 to 1; #=word($,i)/1 /*process the numbers in reverse. */
g = fmt( f( # ) ) /*invoke function ƒ with arg number.*/
gw=right( 'ƒ('#") ", w+7) /*nicely formatted ƒ(number). */
if g>maxValue then say gw "is > " maxValue ' ['space(g)"]"
else say gw " = " g
end /*i*/ /* [+] display the result to terminal.*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
f: procedure; parse arg x; return sqrt( abs(x) ) + 5 * x**3
/*──────────────────────────────────────────────────────────────────────────────────────*/
fmt: z=right(translate(format(arg(1),wid,frac),'e',"E"),wid) /*right adjust; use e*/
if pos(.,z)\==0 then z=left(strip(strip(z,'T',0),"T",.),wid) /*strip trailing 0 &.*/
return right(z,wid-4*(pos('e',z)==0)) /*adjust: no exponent*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); m.=9; numeric form; h=d+6
numeric digits; parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g *.5'e'_ % 2
do j=0 while h>9; m.j=h; h=h%2+1; end /*j*/
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/
return g