all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
21
Task/Trabb-Pardo-Knuth-algorithm/0DESCRIPTION
Normal file
21
Task/Trabb-Pardo-Knuth-algorithm/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
The TPK algorithm is an early example of programming chrestomathy. It was used in Donald Knuth and Luis Trabb Pardo's Stanford tech report [http://bitsavers.org/pdf/stanford/cs_techReports/STAN-CS-76-562_EarlyDevelPgmgLang_Aug76.pdf The Early Development of Programming Languages]. The report traces the early history of work in developing computer languages in the 1940s and 1950s, giving several translations of the algorithm.
|
||||
|
||||
From the [[wp:Trabb Pardo–Knuth algorithm|wikipedia entry]]:
|
||||
|
||||
'''ask''' for 11 numbers to be read into a sequence ''S''
|
||||
'''reverse''' sequence ''S''
|
||||
'''for each''' ''item'' '''in''' sequence ''S''
|
||||
''result'' ''':=''' '''call''' a function to do an ''operation''
|
||||
'''if''' ''result'' overflows
|
||||
'''alert''' user
|
||||
'''else'''
|
||||
'''print''' ''result''
|
||||
|
||||
The task is to implement the algorithm:
|
||||
# Use the function <math>f(x) = |x|^{0.5} + 5x^3</math>
|
||||
# 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).
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
begin
|
||||
integer i; real y; real array a[0:10];
|
||||
real procedure f(t); value t; real t;
|
||||
f:=sqrt(abs(t))+5*t^3;
|
||||
for i:=0 step 1 until 10 do inreal(0, a[i]);
|
||||
for i:=10 step -1 until 0 do
|
||||
begin
|
||||
y:=f(a[i]);
|
||||
if y > 400 then outstring(1, "TOO LARGE")
|
||||
else outreal(1,y);
|
||||
outchar(1, "\n", 1)
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
with Ada.Text_IO, Ada.Numerics.Generic_Elementary_Functions;
|
||||
|
||||
procedure Trabb_Pardo_Knuth is
|
||||
|
||||
type Real is digits 6 range -400.0 .. 400.0;
|
||||
|
||||
package TIO renames Ada.Text_IO;
|
||||
package FIO is new TIO.Float_IO(Real);
|
||||
package Math is new Ada.Numerics.Generic_Elementary_Functions(Real);
|
||||
|
||||
function F(X: Real) return Real is
|
||||
begin
|
||||
return (Math.Sqrt(abs(X)) + 5.0 * X**3);
|
||||
end F;
|
||||
|
||||
Values: array(1 .. 11) of Real;
|
||||
|
||||
begin
|
||||
TIO.Put("Please enter 11 Numbers:");
|
||||
for I in Values'Range loop
|
||||
FIO.Get(Values(I));
|
||||
end loop;
|
||||
|
||||
for I in reverse Values'Range loop
|
||||
TIO.Put("f(");
|
||||
FIO.Put(Values(I), Fore => 2, Aft => 3, Exp => 0);
|
||||
TIO.Put(")=");
|
||||
begin
|
||||
FIO.Put(F(Values(I)), Fore=> 4, Aft => 3, Exp => 0);
|
||||
exception
|
||||
when Constraint_Error => TIO.Put("-->too large<--");
|
||||
end;
|
||||
TIO.New_Line;
|
||||
end loop;
|
||||
|
||||
end Trabb_Pardo_Knuth;
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
; Trabb Pardo–Knuth algorithm
|
||||
; by James1337 (autoit.de)
|
||||
; AutoIt Version: 3.3.8.1
|
||||
|
||||
Local $S, $i, $y
|
||||
|
||||
Do
|
||||
$S = InputBox("Trabb Pardo–Knuth algorithm", "Please enter 11 numbers:", "1 2 3 4 5 6 7 8 9 10 11")
|
||||
If @error Then Exit
|
||||
$S = StringSplit($S, " ")
|
||||
Until ($S[0] = 11)
|
||||
|
||||
For $i = 11 To 1 Step -1
|
||||
$y = f($S[$i])
|
||||
If ($y > 400) Then
|
||||
ConsoleWrite("f(" & $S[$i] & ") = Overflow!" & @CRLF)
|
||||
Else
|
||||
ConsoleWrite("f(" & $S[$i] & ") = " & $y & @CRLF)
|
||||
EndIf
|
||||
Next
|
||||
|
||||
Func f($x)
|
||||
Return Sqrt(Abs($x)) + 5*$x^3
|
||||
EndFunc
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
dim s(11)
|
||||
print 'enter 11 numbers'
|
||||
for i = 0 to 10
|
||||
input i + ">" , s[i]
|
||||
next i
|
||||
|
||||
for i = 10 to 0 step -1
|
||||
print "f(" + s[i] + ")=";
|
||||
x = f(s[i])
|
||||
if x > 400 then
|
||||
print "--- too large ---"
|
||||
else
|
||||
print x
|
||||
endif
|
||||
next i
|
||||
end
|
||||
|
||||
function f(n)
|
||||
return sqrt(abs(n))+5*n^3
|
||||
end function
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <iomanip>
|
||||
|
||||
int main( ) {
|
||||
std::vector<double> input( 11 ) , results( 11 ) ;
|
||||
double number = 0.0 ;
|
||||
std::cout << "Please enter 11 numbers!\n" ;
|
||||
for ( int i = 0 ; i < input.size( ) ; i++ ) {
|
||||
std::cin >> number ;
|
||||
input[ i ] = number ;
|
||||
}
|
||||
std::transform( input.begin( ) , input.end( ) , results.begin( ) ,
|
||||
[ ]( double n )-> double { return sqrt( abs( n ) ) + 5 * pow( n , 3 ) ; } ) ;
|
||||
for ( int i = 10 ; i > -1 ; i-- ) {
|
||||
std::cout << "f( " << std::setw( 3 ) << input[ i ] << " ) : " ;
|
||||
if ( results[ i ] > 400 )
|
||||
std::cout << "too large!" ;
|
||||
else
|
||||
std::cout << results[ i ] << " !" ;
|
||||
std::cout << std::endl ;
|
||||
}
|
||||
return 0 ;
|
||||
}
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
/* Abhishek Ghosh
|
||||
27th August, 2012 */
|
||||
|
||||
#include<math.h>
|
||||
#include<stdio.h>
|
||||
|
||||
int
|
||||
main ()
|
||||
{
|
||||
double inputs[11], check = 400, result;
|
||||
int i;
|
||||
|
||||
printf ("\nPlease enter 11 numbers :");
|
||||
|
||||
for (i = 0; i < 11; i++)
|
||||
{
|
||||
scanf ("%lf", &inputs[i]);
|
||||
}
|
||||
|
||||
printf ("\n\n\nEvaluating f(x) = |x|^0.5 + 5x^3 for the given inputs :");
|
||||
|
||||
for (i = 10; i >= 0; i--)
|
||||
{
|
||||
result = sqrt (fabs (inputs[i])) + 5 * pow (inputs[i], 3);
|
||||
|
||||
printf ("\nf(%lf) = ");
|
||||
|
||||
if (result > check)
|
||||
{
|
||||
printf ("Overflow!");
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
printf ("%lf", result);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
import std.stdio, std.math, std.range, std.conv, std.algorithm;
|
||||
|
||||
double f(in double x) pure nothrow {
|
||||
return x.abs().sqrt() + 5 * x ^^ 3;
|
||||
}
|
||||
|
||||
void main() {
|
||||
double[] data;
|
||||
while (true) {
|
||||
write("Please enter eleven numbers on a line: ");
|
||||
data = readln().split().map!(to!double)().array();
|
||||
if (data.length == 11)
|
||||
break;
|
||||
writeln("Those aren't eleven numbers.");
|
||||
}
|
||||
foreach (x; data.retro()) {
|
||||
immutable y = f(x);
|
||||
writefln("f(%0.3f) = %s", x, y > 400 ? "Too large" : text(y));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
open random number list console format read
|
||||
|
||||
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)
|
||||
p x = x < 400.0
|
||||
iter [] = ()
|
||||
iter (x::xs)
|
||||
| p res = printfn "f({0}) = {1}" x res $ iter xs
|
||||
| else = printfn "f({0}) :: Overflow" x $ iter xs
|
||||
where res = f x
|
||||
|
||||
run ()
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"math"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// prompt
|
||||
fmt.Print("Enter 11 numbers: ")
|
||||
// accept sequence
|
||||
var s [11]float64
|
||||
for i := 0; i < 11; {
|
||||
if _, err := fmt.Scanf("%f", &s[i]); err == nil {
|
||||
i++
|
||||
}
|
||||
}
|
||||
// reverse sequence
|
||||
for i, item := range s[:5] {
|
||||
s[i], s[10-i] = s[10-i], item
|
||||
}
|
||||
// iterate
|
||||
for _, item := range s {
|
||||
if result, overflow := f(item); overflow {
|
||||
// send alerts to stderr
|
||||
log.Printf("f(%g) overflow", item)
|
||||
} else {
|
||||
// send normal results to stdout
|
||||
fmt.Printf("f(%g) = %g\n", item, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func f(x float64) (float64, bool) {
|
||||
result := math.Pow(math.Abs(x), .5) + 5*x*x*x
|
||||
return result, result > 400
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
import Control.Monad (replicateM_)
|
||||
|
||||
f x = (abs x) ** 0.5 + 5 * x ** 3
|
||||
|
||||
main = do
|
||||
putStrLn "Enter 11 numbers for evaluation"
|
||||
replicateM_ 11 $ getLine >>= (\x -> if x>400
|
||||
then putStrLn "OVERFLOW"
|
||||
else print x).f.read
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
// Initialize objects to be used
|
||||
in_num := File standardInput()
|
||||
nums := List clone
|
||||
result := Number
|
||||
|
||||
// Prompt the user and get numbers from standard input
|
||||
"Please enter 11 numbers:" println
|
||||
11 repeat(nums append(in_num readLine() asNumber()))
|
||||
|
||||
// Reverse the numbers received
|
||||
nums reverseInPlace
|
||||
|
||||
// Apply the function and tell the user if the result is above
|
||||
// our limit. Otherwise, tell them the result.
|
||||
nums foreach(v,
|
||||
// v needs parentheses around it for abs to properly convert v to its absolute value
|
||||
result = (v) abs ** 0.5 + 5 * v ** 3
|
||||
if (result > 400,
|
||||
"Overflow!" println
|
||||
,
|
||||
result println
|
||||
)
|
||||
)
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
tpk=: 3 :0
|
||||
smoutput 'Enter 11 numbers: '
|
||||
t1=: ((5 * ^&3) + (^&0.5@* *))"0 |. _999&".;._1 ' ' , 1!:1 [ 1
|
||||
smoutput 'Values of functions of reversed input: ' , ": t1
|
||||
; <@(,&' ')@": ` ((<'user alert ')&[) @. (>&400)"0 t1
|
||||
)
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
tpk ''
|
||||
Enter 11 numbers:
|
||||
1 2 3 4 5 6 7 8.8 _9 10.123 0
|
||||
Values of functions of reversed input: 0 5189.96 _3642 3410.33 1717.65 1082.45 627.236 322 136.732 41.4142 6
|
||||
0 user alert _3642 user alert user alert user alert user alert 322 136.732 41.4142 6
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
get11numbers=: 3 :0
|
||||
smoutput 'Enter 11 numbers: '
|
||||
_&". 1!:1]1
|
||||
)
|
||||
|
||||
f_x=: %:@| + 5 * ^&3
|
||||
|
||||
overflow400=: 'user alert'"_`":@.(<:&400)"0
|
||||
|
||||
tpk=: overflow400@f_x@|.@get11numbers
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
tpk''
|
||||
Enter 11 numbers:
|
||||
1 2 3 4 5 6 7 8.8 _9 10.123 0
|
||||
0
|
||||
user alert
|
||||
_3642
|
||||
user alert
|
||||
user alert
|
||||
user alert
|
||||
user alert
|
||||
322
|
||||
136.732
|
||||
41.4142
|
||||
6
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
/**
|
||||
* Alexander Alvonellos
|
||||
*/
|
||||
import java.util.*;
|
||||
import java.io.*;
|
||||
|
||||
public class TPKA {
|
||||
public static void main(String... args) {
|
||||
double[] input = new double[11];
|
||||
double userInput = 0.0;
|
||||
Scanner in = new Scanner(System.in);
|
||||
for(int i = 0; i < 11; i++) {
|
||||
System.out.print("Please enter a number: ");
|
||||
String s = in.nextLine();
|
||||
try {
|
||||
userInput = Double.parseDouble(s);
|
||||
} catch (NumberFormatException e) {
|
||||
System.out.println("You entered invalid input, exiting");
|
||||
System.exit(1);
|
||||
}
|
||||
input[i] = userInput;
|
||||
}
|
||||
for(int j = 10; j >= 0; j--) {
|
||||
double x = input[j]; double y = f(x);
|
||||
if( y < 400.0) {
|
||||
System.out.printf("f( %.2f ) = %.2f\n", x, y);
|
||||
} else {
|
||||
System.out.printf("f( %.2f ) = %s\n", x, "TOO LARGE");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static double f(double x) {
|
||||
return Math.pow(Math.abs(x), 0.5) + (5*(Math.pow(x, 3)));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
#!/usr/bin/env js
|
||||
|
||||
function main() {
|
||||
var nums = getNumbers(11);
|
||||
nums.reverse();
|
||||
for (var i in nums) {
|
||||
pardoKnuth(nums[i], fn, 400);
|
||||
}
|
||||
}
|
||||
|
||||
function pardoKnuth(n, f, max) {
|
||||
var res = f(n);
|
||||
putstr('f(' + String(n) + ')');
|
||||
if (res > max) {
|
||||
print(' is too large');
|
||||
} else {
|
||||
print(' = ' + String(res));
|
||||
}
|
||||
}
|
||||
|
||||
function fn(x) {
|
||||
return Math.pow(Math.abs(x), 0.5) + 5 * Math.pow(x, 3);
|
||||
}
|
||||
|
||||
function getNumbers(n) {
|
||||
var nums = [];
|
||||
print('Enter', n, 'numbers.');
|
||||
for (var i = 1; i <= n; i++) {
|
||||
putstr(' ' + i + ': ');
|
||||
var num = readline();
|
||||
nums.push(Number(num));
|
||||
}
|
||||
return nums;
|
||||
}
|
||||
|
||||
main();
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
f(x) = abs(x)^.5 + 5x^3
|
||||
for i in map(parse_int,reverse(split(chomp(readline(STDIN)),' ')))
|
||||
println("$i: ",f(i)>400?"TOO LARGE":f(i))
|
||||
end
|
||||
1 2 3 4 5 6 7 8 9 10 11
|
||||
11: TOO LARGE
|
||||
10: TOO LARGE
|
||||
9: TOO LARGE
|
||||
8: TOO LARGE
|
||||
7: TOO LARGE
|
||||
6: TOO LARGE
|
||||
5: TOO LARGE
|
||||
4: 322.0
|
||||
3: 136.73205080756887
|
||||
2: 41.41421356237309
|
||||
1: 6.0
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
let f x = sqrt x +. 5.0 *. (x ** 3.0)
|
||||
let p x = x < 400.0
|
||||
|
||||
let () =
|
||||
print_endline "Please enter 11 Numbers:";
|
||||
let lst = Array.to_list (Array.init 11 (fun _ -> read_float ())) in
|
||||
List.iter (fun x ->
|
||||
let res = f x in
|
||||
if p res
|
||||
then Printf.printf "f(%g) = %g\n%!" x res
|
||||
else Printf.eprintf "f(%g) :: Overflow\n%!" x
|
||||
) (List.rev lst)
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
//
|
||||
// TPKA.m
|
||||
// RosettaCode
|
||||
//
|
||||
// Created by Alexander Alvonellos on 5/26/12.
|
||||
// Trabb Pardo-Knuth algorithm
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
double f(double x);
|
||||
|
||||
double f(double x) {
|
||||
return pow(abs(x), 0.5) + 5*(pow(x, 3));
|
||||
}
|
||||
|
||||
int main (int argc, const char * argv[])
|
||||
{
|
||||
@autoreleasepool {
|
||||
NSMutableArray *input = [[NSMutableArray alloc] initWithCapacity:0];
|
||||
|
||||
printf("%s", "Instructions: please enter 11 numbers.\n");
|
||||
for(int i = 0; i < 11; i++) {
|
||||
double userInput = 0.0;
|
||||
printf("%s", "Please enter a number: ");
|
||||
scanf("%lf", &userInput);
|
||||
[input addObject: [NSNumber numberWithDouble: (double) userInput]];
|
||||
}
|
||||
|
||||
for(int i = 10; i >= 0; i--) {
|
||||
double x = [[input objectAtIndex: i] doubleValue];
|
||||
double y = f(x);
|
||||
printf("f(%.2f) \t=\t", x);
|
||||
if(y < 400.0) {
|
||||
printf("%.2f\n", y);
|
||||
} else {
|
||||
printf("%s\n", "TOO LARGE");
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
print("11 numbers: ");
|
||||
v=vector(11, n, eval(input()));
|
||||
v=apply(x->x=sqrt(abs(x))+5*x^3;if(x>400,"overflow",x), v);
|
||||
vector(11, i, v[12-i])
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
my @nums = prompt("Please type 11 space-separated numbers: ").words
|
||||
until @nums == 11;
|
||||
for @nums.reverse -> $n {
|
||||
my $r = $n.abs.sqrt + 5 * $n ** 3;
|
||||
say "$n\t{ $r > 400 ?? 'Urk!' !! $r }";
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
#!/usr/bin/perl
|
||||
use strict ;
|
||||
use warnings ;
|
||||
|
||||
my $number ;
|
||||
my @sequence ;
|
||||
print "Please enter 11 numbers!\n" ;
|
||||
for my $i ( 0..10 ) {
|
||||
$number = <STDIN> ;
|
||||
chomp $number ;
|
||||
push @sequence , $number ;
|
||||
}
|
||||
map { my $result = sqrt( abs ( $_ ) ) + 5 * $_** 3 ; print "f( $_ ) " ; $result > 400 ? print "too large!\n" : print ": $result\n" ; }
|
||||
reverse @sequence ;
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
(de f (X)
|
||||
(+ (sqrt (abs X)) (* 5 X X X)) )
|
||||
|
||||
(trace 'f)
|
||||
|
||||
(in NIL
|
||||
(prin "Input 11 numbers: ")
|
||||
(for X (reverse (make (do 11 (link (read)))))
|
||||
(when (> (f X) 400)
|
||||
(prinl "TOO LARGE") ) ) )
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
Input 11 numbers: 1 2 3 4 5 6 7 8 9 10 11
|
||||
f : 11
|
||||
f = 6658
|
||||
TOO LARGE
|
||||
f : 10
|
||||
f = 5003
|
||||
TOO LARGE
|
||||
f : 9
|
||||
f = 3648
|
||||
TOO LARGE
|
||||
f : 8
|
||||
f = 2562
|
||||
TOO LARGE
|
||||
f : 7
|
||||
f = 1717
|
||||
TOO LARGE
|
||||
f : 6
|
||||
f = 1082
|
||||
TOO LARGE
|
||||
f : 5
|
||||
f = 627
|
||||
TOO LARGE
|
||||
f : 4
|
||||
f = 322
|
||||
f : 3
|
||||
f = 136
|
||||
f : 2
|
||||
f = 41
|
||||
f : 1
|
||||
f = 6
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
Procedure.d f(x.d)
|
||||
ProcedureReturn Pow(Abs(x), 0.5) + 5 * x * x * x
|
||||
EndProcedure
|
||||
|
||||
Procedure split(i.s, delimeter.s, List o.d())
|
||||
Protected index = CountString(i, delimeter) + 1 ;add 1 because last entry will not have a delimeter
|
||||
|
||||
While index > 0
|
||||
AddElement(o())
|
||||
o() = ValD(Trim(StringField(i, index, delimeter)))
|
||||
index - 1
|
||||
Wend
|
||||
|
||||
ProcedureReturn ListSize(o())
|
||||
EndProcedure
|
||||
|
||||
Define i$, entriesAreValid = 0, result.d, output$
|
||||
NewList numbers.d()
|
||||
|
||||
If OpenConsole()
|
||||
Repeat
|
||||
PrintN(#crlf$ + "Enter eleven numbers that are each separated by spaces or commas:")
|
||||
|
||||
i$ = Input(
|
||||
i$ = Trim(i$)
|
||||
If split(i$, ",", numbers.d()) < 11
|
||||
ClearList(numbers())
|
||||
If split(i$, " ", numbers.d()) < 11
|
||||
PrintN("Not enough numbers were supplied.")
|
||||
ClearList(numbers())
|
||||
Else
|
||||
entriesAreValid = 1
|
||||
EndIf
|
||||
Else
|
||||
entriesAreValid = 1
|
||||
EndIf
|
||||
Until entriesAreValid = 1
|
||||
|
||||
ForEach numbers()
|
||||
output$ = "f(" + RTrim(RTrim(StrD(numbers(), 3), "0"), ".") + ") = "
|
||||
result.d = f(numbers())
|
||||
If result > 400
|
||||
output$ + "Too Large"
|
||||
Else
|
||||
output$ + RTrim(RTrim(StrD(result, 3), "0"), ".")
|
||||
EndIf
|
||||
PrintN(output$)
|
||||
Next
|
||||
|
||||
Print(#crlf$ + #crlf$ + "Press ENTER to exit"): Input()
|
||||
CloseConsole()
|
||||
EndIf
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
Python 3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win32
|
||||
Type "copyright", "credits" or "license()" for more information.
|
||||
>>> def f(x): return abs(x) ** 0.5 + 5 * x**3
|
||||
|
||||
>>> print(', '.join('%s:%s' % (x, v if v<=400 else "TOO LARGE!")
|
||||
for x,v in ((y, f(float(y))) for y in input('\nnumbers: ').strip().split()[:11][::-1])))
|
||||
|
||||
11 numbers: 1 2 3 4 5 6 7 8 9 10 11
|
||||
11:TOO LARGE!, 10:TOO LARGE!, 9:TOO LARGE!, 8:TOO LARGE!, 7:TOO LARGE!, 6:TOO LARGE!, 5:TOO LARGE!, 4:322.0, 3:136.73205080756887, 2:41.41421356237309, 1:6.0
|
||||
>>>
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
def f(x):
|
||||
return abs(x) ** 0.5 + 5 * x**3
|
||||
|
||||
def ask():
|
||||
return [float(y)
|
||||
for y in input('\n11 numbers: ').strip().split()[:11]]
|
||||
|
||||
if __name__ == '__main__':
|
||||
s = ask()
|
||||
s.reverse()
|
||||
for x in s:
|
||||
result = f(x)
|
||||
if result > 400:
|
||||
print(' %s:%s' % (x, "TOO LARGE!"), end='')
|
||||
else:
|
||||
print(' %s:%s' % (x, result), end='')
|
||||
print('')
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
S <- scan(n=11)
|
||||
|
||||
f <- function(x) sqrt(abs(x)) + 5*x^3
|
||||
|
||||
for (i in rev(S)) {
|
||||
res <- f(i)
|
||||
if (res > 400)
|
||||
print("Too large!")
|
||||
else
|
||||
print(res)
|
||||
}
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
/*REXX program to implement the Trabb-Pardo-Knuth algorthm for N nums.*/
|
||||
N=11 /*N is the number of numbers. */
|
||||
maxValue=400 /*the maximum value f(x) can have*/
|
||||
precDigs=200 /*compute with this many digits. */
|
||||
showDigs=20 /*...but only show this many digs*/
|
||||
numeric digits precDigs /*the number of digits precision.*/
|
||||
prompt='enter' N "nunbers for the Trabb-Pardo-Knuth algorthm: (or Quit)"
|
||||
say ' _____ ' /*vinculum.*/
|
||||
say 'function: ƒ(x) ≡ √ │x│ + (5 * x^3)'
|
||||
/*██████████████████████████████████████████████████████████████████████*/
|
||||
do ask=0; say; say prompt; say; parse pull yyyU . 1 yyy; say
|
||||
upper yyyU; if abbrev('QUIT',yyyU,1) then exit
|
||||
|
||||
do validate=0
|
||||
select
|
||||
when yyy='' then say 'no numbers entered'
|
||||
when words(yyy)<N then say 'not enough numbers entered'
|
||||
when words(yyy)>N then say 'too many numbers entered'
|
||||
otherwise leave validate
|
||||
end /*select*/
|
||||
iterate ask
|
||||
end /*validate*/
|
||||
do j=1 for N; _=word(yyy,j)
|
||||
if \datatype(_,'N') then do
|
||||
say _ "isn't numeric"
|
||||
iterate ask
|
||||
end
|
||||
end /*j*/
|
||||
leave ask
|
||||
end /*ask*/
|
||||
say 'numbers entered:' yyy; say
|
||||
/*██████████████████████████████████████████████████████████████████████*/
|
||||
do i=N by -1 to 1; p=word(yyy,i)/1 /*process #s in reverse.*/
|
||||
g=f(p)
|
||||
numeric digits showdigs; g=g/1 /*scale down the result.*/
|
||||
if g>maxValue then say 'f('p") is > " maxValue ' ['g"]"
|
||||
else say 'f('p") = " g /*show the (good) result*/
|
||||
numeric digits precDigs /*re-instate big digits.*/
|
||||
end /*i*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────F function──────────────────────────*/
|
||||
f: procedure; arg x; return sqrt(abs(x)) + 5 * x**3
|
||||
/*──────────────────────────────────SQRT function───────────────────────*/
|
||||
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits();numeric digits 11
|
||||
g=.sqrtGuess(); do j=0 while p>9; m.j=p; p=p%2+1; end
|
||||
do k=j+5 to 0 by -1; if m.k>11 then numeric digits m.k; g=.5*(g+x/g); end
|
||||
numeric digits d; return g/1
|
||||
.sqrtGuess: numeric form; m.=11; p=d+d%4+2
|
||||
parse value format(x,2,1,,0) 'E0' with g 'E' _ .; return g*.5'E'_%2
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
nums = [];
|
||||
|
||||
puts "Please enter 11 numbers:"
|
||||
11.times{nums << gets.chomp.to_f}
|
||||
|
||||
nums.reverse.each do |n|
|
||||
res = n.abs ** 0.5 + 5 * n ** 3
|
||||
if res > 400
|
||||
puts "Overflow!"
|
||||
else
|
||||
puts res
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
# Helper procedures
|
||||
proc f {x} {expr {abs($x)**0.5 + 5*$x**3}}
|
||||
proc overflow {y} {expr {$y > 400}}
|
||||
|
||||
# Read in 11 numbers, with nice prompting
|
||||
fconfigure stdout -buffering none
|
||||
for {set n 1} {$n <= 11} {incr n} {
|
||||
puts -nonewline "number ${n}: "
|
||||
lappend S [scan [gets stdin] "%f"]
|
||||
}
|
||||
|
||||
# Process and print results in reverse order
|
||||
foreach x [lreverse $S] {
|
||||
set result [f $x]
|
||||
if {[overflow $result]} {
|
||||
puts "${x}: TOO LARGE!"
|
||||
} else {
|
||||
puts "${x}: $result"
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
include c:\cxpl\codes;
|
||||
|
||||
func real F(X);
|
||||
real X;
|
||||
return sqrt(abs(X)) + 5.0*X*X*X;
|
||||
|
||||
real Result, S(11); int I;
|
||||
[Text(0, "Please enter 11 numbers: ");
|
||||
for I:= 0 to 11-1 do S(I):= RlIn(0);
|
||||
|
||||
for I:= 11-1 downto 0 do
|
||||
[RlOut(0, S(I));
|
||||
Result:= F(S(I));
|
||||
if Result > 400.0 then
|
||||
Text(0, " overflows")
|
||||
else RlOut(0, Result);
|
||||
CrLf(0)];
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue