Programming using JavaScript
(seeing both the program text and the output

     using     macosa.dima.unige.it/javascript.com )

Multiple sessions of  macosa.dima.unige.it/js.com  can be opened simultaneously.  Once you have created a program, don't click "Update" (Ctrl+R) if you don't want to lose the document (possibly copy it first or open another session).

As soon as you click   macosa.dima.unige.it/javascript.com   the following appears:

"<p>Copy and paste the following lines at the top of the page (instead of what is now). Here it will appear <b>10</b>
                            . . .
                            . . .
Copy and paste the following lines at the top of the page (instead of what is now). Here it will appear 10
<pre><script> with(Math) {
document.write(2+4*2)
} </script></pre>

to explain how it works.
If you do what is required (with a copy and paste), then appears:

<pre><script> with(Math) {
document.write(2+4*2)
} </script></pre>
10

Starting from this example, it is easy to insert modifications to create other programs.

[The programma is bounded by <script> and </script>.  The <pre> and </pre> commands cause the text to be written in a predetermined format, which reserves the same space for each character.  "document.write" causes the following to be written, indicated in parentheses.  "with(Math)" allows mathematical operations to be inserted, such as those indicated later, in (2), in this documentc]

(1)   Example:  the calculation of  (3/4 + 7.5 + 0.25 - 5/2)·(3/2 + 7/10 - 1/2 + 4.3)     Copy and Paste:
<pre><script> with(Math) {

document.write( (3/4 + 7.5 + 0.25 - 5/2)*(3/2 + 7/10 - 1/2 + 4.3) )

} </script></pre>
Output:  36

To insert a RETURN use <br> ("br" represents a "break" of the line).
<pre><script> with(Math) {

document.write( "calculation of <br>(3/4 + 7.5 + 0.25 - 5/2)*(3/2 + 7/10 - 1/2 + 4.3)" )

} </script></pre>
Output:
calculation of
(3/4 + 7.5 + 0.25 - 5/2)*(3/2 + 7/10 - 1/2 + 4.3)


Instead of <br> you can use  \n  (newline):
<pre><script> with(Math) {

document.write( "calculation of\n(3/4 + 7.5 + 0.25 - 5/2)*(3/2 + 7/10 - 1/2 + 4.3)" )

} </script></pre>

(2)   Some of the insertable operations:   +   -   *   /    abs(K)  absolute value of K,  max(H,K)  the maximum between H and K,  min(H,K)  the minimum between H and K,  pow(H,K)  H to the Kth,  round(H)  the integer closest to H,  sqrt(H)  the square root of H.
PI  denotes  π (≈ 3.141592653589793).  E  denotes  Napier's number (e ≈ 2.718281828459045).

To round x = 12345.6789 to hundredths you can use round(x*100)/100, to thousands round(x*0.001)/0.001

The names of other functions (a represents a generic input) that can be used:
cbrt(a)  cube root,   sign(a)  sign (-1, 1 o 0),  ceil(a)  intero vicino non < a,  floor(a)  integer closest to a not > a,   cos(a)  cosine,  sin(a)  sine,  tan(a)  tangent,  acos(a)  arc cosine,  asin(a)  arc sine,  atan(a)  arc tangent,   log10(a)  log base 10,  log2(a)  log base 2,  log(a)  log base e,   random()  random number in (0,1).

You can use floor(random()*6+1) to generate the random roll of a dice. To generate the output of a generic number between 3 and 7 you can use random()*(7-3)+3.  Example:
<pre><script> with(Math) {
N=10
for(i=0; i<N; i=i+1) document.write(floor(random()*6+1),"  ")
} </script></pre>
6  3  5  4  3  6  2  5  2  1


An example of calculations, in which  writeln  is used, which after printing makes a "new line" ("ln" stands for "line").
<pre><script> with(Math) {

x = 36
y = 10
document.writeln( x+y )
document.writeln( x-y )
document.writeln( x*y )
document.writeln( x/y )
document.writeln( round(x/y) )
document.writeln( min(x,y) )
document.writeln( max(x,y) )
document.writeln( pow(x,y) )

} </script></pre>
Outputs:
46
26
360
3.6
4
10
36
3656158440062976
(3)   The tabulation of a function:
<pre><script> with(Math) {

document.write( "         x^5 + 6*x^3 + x/7<br><br>" )
x = 1.5
document.write( "x=",x, " -> ",pow(x,5)+6*pow(x,3)+x/7, "<br>" )
x = 2
document.write( "x=",x, "   -> ",pow(x,5)+6*pow(x,3)+x/7, "<br>" )
x = 2.5
document.write( "x=",x, " -> ",pow(x,5)+6*pow(x,3)+x/7, "<br>" )
x = 3
document.write( "x=",x, "   -> ",pow(x,5)+6*pow(x,3)+x/7, "<br>" )


} </script></pre>
Outputs:
         x^5 + 6*x^3 + x/7

x=1.5 -> 28.058035714285715
x=2   -> 80.28571428571429
x=2.5 -> 191.76339285714286
x=3   -> 405.42857142857144
(4)   Let's see how loops can be performed, i.e. how calculations can be automatically repeated several times. Here is a first example:
<pre><script> with(Math) {

for(i=1; i<10; i=i+1) document.write( i*2," " )

} </script></pre>
Outputs:   2 4 6 8 10 12 14 16 18

   i = 1
     |
  write i*2  <—————
     |              | 
  i = i + 1         |
     |              |
  i < 10 ?  — YES ——
     |
     |  NO
     |
    STOP
A for loop appears:   the loop is executed starting from the value assigned to the "counter variable" (in this example "i") until the indicated condition is valid (i<10);   "i=i+1" indicates how it is changed the counter variable at each end of the cycle;   the instruction that is repeated is document.write( i*2," " ) , which causes the value of i*2 to be written followed by   " ", i.e. a white space.
If instead of document.write(i*2," ") we had written document.writeln(i*2) we would have obtained:
2
4
...
18


The sum of two numbers however long, having put the digits in two sequences (using a "decreasing" for loop):
<pre><script> with(Math) {

A=[3,5,7,0,3,8,0,5,3,2,1,6,5,4,3,2,1,8,9,0,1,7,3,0,6,8,".",7,1,2,5]
B=[0,0,0,0,8,7,4,0,5,1,0,0,3,4,8,1,9,2,1,5,2,5,1,6,0,3,".",7,5,0,0]
n=A.length; R=0; C=""
for(i=n-1; i>=0; i=i-1) {if(A[i]==".") {C="."+C} else {S=A[i]+B[i]+R; R=0; if(S>9) {S=S-10; R=1}; C=S+C} }
if(R==1) {C="1"+C}
document.writeln(A," +")
document.writeln(B, " =")
document.write(C)

} </script></pre>
3,5,7,0,3,8,0,5,3,2,1,6,5,4,3,2,1,8,9,0,1,7,3,0,6,8,.,7,1,2,5 +
0,0,0,0,8,7,4,0,5,1,0,0,3,4,8,1,9,2,1,5,2,5,1,6,0,3,.,7,5,0,0 =
35712545831689141105424672.4625

We also used an if statement.
In a statement  if   "equals"  is written   "==".  We can put more conditions, using  "&&"  as "and",  "||"  as "or".

The mean (and sum, min, max) of a sequence of data (A.length indicates the length of A).
<pre><script> with(Math) {

A = [172,169,183,181,167]
n = A.length; min = A[0]; max = A[0]
S = 0; for(i=0; i<n; i=i+1) {S = S+A[i]; if(A[i]>max) max=A[i]; if(A[i]<min) min=A[i]}
document.write ("the sum of ",A,"  is  ", S, "<br>")
document.write ("the mean is ",S,"/",n, " = ", S/n,"<br>")
document.write ("min = ",min,"   max = ",max )


} </script></pre>
Outputs:
the sum of 172,169,183,181,167  is  872
the mean is 872/5 = 174.4
min = 167   max = 183
I can sort numbers:
<pre><script> with(Math) {

A = [23, 4.1, PI, 3, 5, 12, -3, 1e3, 1e-4]
document.write(A,"<br>")
B = A
n = A.length
for(k=0; k<n; k=k+1) {
  for(i=k+1; i<n; i=i+1) { if(B[i]<B[k]) {C=B[k]; B[k]=B[i]; B[i]=C} }
}
document.write(B)


} </script></pre>
Output:
23,4.1,3.141592653589793,3,5,12,-3,1000,0.0001
-3,0.0001,3,3.141592653589793,4.1,5,12,23,1000
Note. 1000 and 0.0001 were written 1e3 and 1e-4, i.e. in exponential notation:  AeB indicates A*10B


We can easily process large amounts of data. The lengths in cm of the beans examined by the students of a school class:
<pre><script> with(Math) {

dat = [1.35, 1.65, 1.80, 1.40, 1.65, 1.80, 1.40, 1.65, 1.85, 1.40, 1.65, 1.85, 1.50, 1.65, 1.90, 1.50, 1.65, 1.90,
1.50, 1.65, 1.90, 1.50, 1.70, 1.90, 1.50, 1.70, 1.90, 1.50, 1.70, 2.25, 1.55, 1.70, 1.55, 1.70, 1.55, 1.70, 1.60,
1.70, 1.60, 1.75, 1.60, 1.75, 1.60, 1.80, 1.60, 1.80, 1.60, 1.80, 1.60, 1.80, 1.00, 1.55, 1.70, 1.75, 1.30, 1.55,
1.70, 1.75, 1.40, 1.60, 1.70, 1.75, 1.40, 1.60, 1.70, 1.80, 1.40, 1.60, 1.70, 1.80, 1.40, 1.60, 1.70, 1.80, 1.40,
1.60, 1.70, 1.80, 1.40, 1.60, 1.70, 1.80, 1.40, 1.60, 1.70, 1.80, 1.40, 1.60, 1.70, 1.80, 1.45, 1.60, 1.70, 1.80,
1.50, 1.60, 1.70, 1.80, 1.50, 1.60, 1.70, 1.85, 1.50, 1.60, 1.70, 1.85, 1.50, 1.60, 1.75, 1.90, 1.50, 1.60, 1.75,
1.90, 1.50, 1.65, 1.75, 1.90, 1.55, 1.65, 1.75, 1.95, 1.55, 1.65, 1.75, 2.00, 1.55, 1.65, 1.75, 2.30, 1.35, 1.65,
1.80, 1.40, 1.65, 1.80, 1.40, 1.65, 1.85, 1.40, 1.65, 1.85, 1.50, 1.65, 1.90, 1.50, 1.65, 1.90, 1.50, 1.65, 1.90,
1.50, 1.70, 1.90, 1.50, 1.70, 1.90, 1.50, 1.70, 2.25, 1.55, 1.70, 1.55, 1.70, 1.55, 1.70, 1.60, 1.70, 1.60, 1.75,
1.60, 1.75, 1.60, 1.80, 1.60, 1.80, 1.60, 1.80, 1.60, 1.80, 1.00, 1.55, 1.70, 1.75, 1.30, 1.55, 1.70, 1.75, 1.40,
1.60, 1.70, 1.75, 1.40, 1.60, 1.70, 1.80, 1.40, 1.60, 1.70, 1.80, 1.40, 1.60, 1.70, 1.80, 1.40, 1.60, 1.70, 1.80,
1.40, 1.60, 1.70, 1.80, 1.40, 1.60, 1.70, 1.80, 1.40, 1.60, 1.70, 1.80, 1.45, 1.60, 1.70, 1.80, 1.50, 1.60, 1.70,
1.80, 1.50, 1.60, 1.70, 1.85, 1.50, 1.60, 1.70, 1.85, 1.50, 1.60, 1.75, 1.90, 1.50, 1.60, 1.75, 1.90, 1.50, 1.65,
1.75, 1.90, 1.55, 1.65, 1.75, 1.95, 1.55, 1.65, 1.75, 2.00, 1.55, 1.65, 1.75, 2.30]
A = dat; n = A.length
for(k=0; k<n; k=k+1) {  for(i=k+1; i<n; i=i+1) { if(A[i] < A[k]) {C = A[k]; A[k]=A[i]; A[i]=C} }  }
document.writeln ("n = ", n, "  min = ", A[0], "  max = ", A[n-1])
document.writeln ("median = ", A[round(n/2)], "  1^ quarter = ", A[round(n/4)], "  3^ quarter = ", A[round(n*3/4)])
S = 0; for(i=0; i<n; i=i+1) {S = S + A[i]}; mean = S/n
document.writeln ("mean = ", mean)
} </script></pre>
n = 260  min = 1  max = 2.3
median = 1.65  1^ quarter = 1.55  3^ quarter = 1.75
mean = 1.6592307692307673

The middle 50% of the sorted data falls in the range [1.55 cm, 1.75 cm]

Another convenient statement is while. An example: the decomposition of a positive integer into prime factors:
<pre><script> with(Math) {

n=632
k=2; while(k <= n) {if(n/k == round(n/k)) {n = n/k; document.write(k," ")} else k=k+1 }

} </script></pre>
2 2 2 79
else  can be used to indicate what to do if the condition of an  if  is false.

With minor modifications we can use the script to find gcd and lcm of several natural numbers, without resorting to specific algorithms. Example:
<pre><script> with(Math) {

n=13068
k=2; while(k <= n) {if(n/k == round(n/k)) {n = n/k; document.write(k," ")} else k=k+1 }
document.write("<br>")
n=90882
k=2; while(k <= n) {if(n/k == round(n/k)) {n = n/k; document.write(k," ")} else k=k+1 }
document.write("<br>")
n=29376
k=2; while(k <= n) {if(n/k == round(n/k)) {n = n/k; document.write(k," ")} else k=k+1 }


} </script></pre>
2 2 3 3 3 11 11 
2 3 3 3 3 3 11 17 
2 2 2 2 2 2 3 3 3 17
gcd(13068,90882,29376) = 2*3*3*3 = 54, lcm(13068,90882,29376) = 2*2*2*2*2*2*3*3*3*3*3*11*11*17 = 31990464


(5)   Geometric examples
<pre><script> with(Math) {
x1 = 1; y1 = 2; x2 = 6; y2 = 7
document.write( "distance between (",x1,",",y1,") and (",x2,",",y2,") <br>" )
document.write( sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) ) )
} </script></pre>

     distance between (1,2) and (6,7)
     7.0710678118654755
<pre><script> with(Math) { x1 = 1; y1 = 1 x2 = 4; y2 = 0 x3 = 0; y3 = 5 L1=sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) ) L2=sqrt( (x1-x3)*(x1-x3) + (y1-y3)*(y1-y3) ) L3=sqrt( (x3-x2)*(x3-x2) + (y3-y2)*(y3-y2) ) document.write( "P1P2 P1P3 P2P3: ", L1," ", L2," ", L3,"<br>" ) document.write( "perimeter of the triangle P1,P2,P3 = ", L1+L2+L3, "<br>" ) document.write( "area of the triangle P1,P2,P3 = ", abs(x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2))/2 ) } </script></pre> P1P2 P1P3 P2P3: 3.1622776601683795 4.123105625617661 6.4031242374328485 perimeter of the triangle P1,P2,P3 = 13.688507523218888 area of the triangle P1,P2,P3 = 5.5
You can draw the triangle and calculate its area, sides, angles with this script getting this image.


(6)   I can solve the  equations  f(x) = 0  starting from an interval in which  f changes sign (proceeding by bisection: if in the middle of the interval the function has the same sign as in the left end I assume that the solution lies in the right half, otherwise I assume which is in the left half).
Example:  solve (with respect to x) 5x = 100, starting from [0,10] (since 50<100 and 510>100).
<pre><script> with(Math) {
function F(x) {return pow(5,x)-100}
a = 0;  b = 10
for(n=10; n<3000; n=n*2) {
  for(i=0; i<n; i=i+1) {
    m = a+(b-a)/2; y1 = F(a); y2 = F(m); y3 = F(b)
    if(y1*y2 > 0) {a = m} else {b = m}
  }
  document.writeln("n = ",n , "   a = ", a,"   b = ",b)
}
} </script></pre>
n = 10   a = 2.861328125   b = 2.87109375
n = 20   a = 2.861353112384677   b = 2.8613531216979027
n = 40   a = 2.861353116146786   b = 2.8613531161467862
n = 80   a = 2.861353116146786   b = 2.8613531161467862
n = 160   a = 2.861353116146786   b = 2.8613531161467862
n = 320   a = 2.861353116146786   b = 2.8613531161467862
n = 640   a = 2.861353116146786   b = 2.8613531161467862
n = 1280   a = 2.861353116146786   b = 2.8613531161467862
n = 2560   a = 2.861353116146786   b = 2.8613531161467862
I take as rounding of the solution 2.8613531161468.  (with software like WolframAlpha we would find 2.861353116146786101340213...)


(7)   It can be convenient to tabulate a function in a range. For example we tabulate x → -x4-x3+7 (with 11 values) between -50 and 50:
<pre><script> with(Math) {
function F(x) {return -pow(x,4)-pow(x,3)+7}
a = -50;  b = 50
h=(b-a)/10
for(n=0; n<=10; n=n+1) {document.writeln("x = ",a+h*n,"   F(x) = ",F(a+h*n))}
} </script></pre>
x = -50   F(x) = -6124993
x = -40   F(x) = -2495993
x = -30   F(x) = -782993
x = -20   F(x) = -151993
x = -10   F(x) = -8993
x = 0   F(x) = 7
x = 10   F(x) = -10993
x = 20   F(x) = -167993
x = 30   F(x) = -836993
x = 40   F(x) = -2623993
x = 50   F(x) = -6374993
This can be useful for having a quick assessment of the trend of the function. In this case, taking a and b as they get closer, we will be convinced that the function has a trend /\ between -10 and 10 and we could look for where it assumes the maximum value. Let's see how.

Given a function F, having identified an interval in which the trend is /\, we can find the maximum in that interval, or having identified one in which the trend is \/ we can find the minimum.
The idea is to gradually divide the interval into 3 parts and take the 2/3 where the value sought is.

Here is the program to find the maximum (in the case of the previous function):
<pre><script> with(Math) {
function F(x) {return -pow(x,4)-pow(x,3)+7}
a = -10;  b = 10
for(n=10; n<3000; n=n*2) {
  for(i=0; i<n; i=i+1) {
  h = (b-a)/3; y1 = F(a); y2 = F(a+h); y3 = F(a+h+h)
  if( (y1 < y2) & (y2  < y3) ) {a = a+h} else {b = b-h} }
  document.writeln("n=",n , "  max between ", a,", ",b, "  val= ", y2)
  }
} </script></pre>
n=10  max between -0.8421819167132386, -0.49535131839658636  val= 7.094270215667542
n=20  max between -0.7500533542630592, -0.7499490523620422  val= 7.105468749998371
n=40  max between -0.750000032934137, -0.7500000329247042  val= 7.105468749999999
n=80  max between -0.750000032934137, -0.7500000329341369  val= 7.105468749999999
n=160  max between -0.750000032934137, -0.7500000329341369  val= 7.105468749999999
n=320  max between -0.750000032934137, -0.7500000329341369  val= 7.105468749999999
n=640  max between -0.750000032934137, -0.7500000329341369  val= 7.105468749999999
n=1280  max between -0.750000032934137, -0.7500000329341369  val= 7.105468749999999
n=2560  max between -0.750000032934137, -0.7500000329341369  val= 7.105468749999999
We guess that the exact value of the point where the maximum is -0.75 = -3/4 and that here F is 7.10546875. The "strange" values on the last digits are due to the approximations made by the program. In any case, in any real problem, one must round to a relatively small number of digits (with WolframAlpha we would find exactly -3/4 and 1819/256).

The program to find the minimum applied to the case of  x → 7*x2 - x/3 + 5:
<pre><script> with(Math) {
function F(x) {return 7*x*x - x/3 + 5}
a = -10;  b = 10
for(n=10; n<3000; n=n*2) {
  for(i=0; i<n; i=i+1) {
  h = (b-a)/3; y1 = F(a); y2 = F(a+h); y3 = F(a+h+h)
  if( (y1 > y2) & (y2  > y3) ) {a = a+h} else {b = b-h} }
  document.writeln("n=",n , "  min between ", a,", ",b, "  val= ", y2)
  }
} </script></pre>
n=10  min between -0.21998679063150905, 0.12684380768514317  val= 5.030706157217319
n=20  min between 0.0237545988945348, 0.02385890079555173  val= 4.9960317671489705
n=40  min between 0.023809512484711453, 0.02380951249414428  val= 4.996031746031747
n=80  min between 0.023809512484711453, 0.023809512484711456  val= 4.996031746031747
n=160  min between 0.023809512484711453, 0.023809512484711456  val= 4.996031746031747
n=320  min between 0.023809512484711453, 0.023809512484711456  val= 4.996031746031747
n=640  min between 0.023809512484711453, 0.023809512484711456  val= 4.996031746031747
n=1280  min between 0.023809512484711453, 0.023809512484711456  val= 4.996031746031747
n=2560  min between 0.023809512484711453, 0.023809512484711456  val= 4.996031746031747
The minimum is 0.0238095124847 where F is 4.99603174603 (with WolframAlpha we would find 1/42 and 1259/252)

Another example. We figured out (by tabulating the function) that (x*sin(x)+sqrt(x)-x*x)3-x4 has a maximum between 0 and 0.8. Let's find it more precisely.
<pre><script> with(Math) {
function F(x) {return pow(x*sin(x)+sqrt(x)-x*x,3)-pow(x,4) }
a = 0;  b = 0.8
for(n=10; n<3000; n=n*2) {
  for(i=0; i<n; i=i+1) {
  h = (b-a)/3; y1 = F(a); y2 = F(a+h); y3 = F(a+h+h)
  if( (y1 < y2) & (y2  <y3) ) {a = a+h} else {b = b-h} }
  document.writeln("n=",n , "  max tra ", a,", ",b, "  val= ", y2)
  }
} </script></pre>
n=10  max tra 0.5923893715388915, 0.6062625954715576  val= 0.2980093461765785
n=20  max tra 0.599325390462832, 0.5993295625388726  val= 0.2980093461811849
n=40  max tra 0.599327315089737, 0.5993273150901144  val= 0.2980093461812495
n=80  max tra 0.599327315089737, 0.5993273150897371  val= 0.29800934618124975
n=160  max tra 0.599327315089737, 0.5993273150897371  val= 0.29800934618124975
n=320  max tra 0.599327315089737, 0.5993273150897371  val= 0.29800934618124975
n=640  max tra 0.599327315089737, 0.5993273150897371  val= 0.29800934618124975
n=1280  max tra 0.599327315089737, 0.5993273150897371  val= 0.29800934618124975
n=2560  max tra 0.599327315089737, 0.5993273150897371  val= 0.29800934618124975
I could take x = 0.5993273, y = 0.29800934618125, but, if the problem is "practical", it is sufficient to take x = 0.5993, y = 0.2980  (with Geogebra I would instead get x = 0.6, y = 0.3!!! With WolframAlpha I would get correctly x ≈ 0.599327, y ≈ 0.298009).


(8)   The minimum and maximum of a function in an interval, if its trend is not known (\/ or/\ or ...), can be approximated with a dense tabulation..  Consider the same function studied above:
<pre><script> with(Math) {
function F(x) {return 7*x*x - x/3 + 5 }
a = -10; b = 10
min = 1e300; max = -1e300
n = 8*9*5*7*11*13
for(i=0; i<=n; i=i+1) {x = a+(b-a)/n*i; y = F(x); if(y<min) {min=y; x1=x}; if(y>max) {max=y; x2=x}}
document.write("min ≈ ", min, " (x=", x1, ")   max ≈ ", max, " (x=", x2, ")")
} </script></pre>
min ≈ 4.996031746031746 (x=0.023809523809523725)   max ≈ 708.3333333333334 (x=-10)
I have an estimate of the point where the function assumes the minimum value and an estimate of this value:  0.023809523809523725 instead of 0.0238095124847,  4.996031746031746 instead of 4.99603174603.


(9)   A biologist has to do an experiment on 2 mice, chosen from the 4 (A, B, C and D) at his disposal. In how many ways can he make the choice?
6: AB, AC, AD, BC, BD, CD.  It is the number of subsets of 2 elements of a set of 4 elements, i.e. the combinations of 4 elements 2 to 2.  A formula for counting them is 4/2·3/1 (= 6).  If he had to choose 15 mice out of 25, the possible choices would be  25/15·24/14·...·11/1.  How many are they?
The calculation can be done easily, and in general, with a for loop. The combinations C(25,15) of 25 elements 15 to 15 are:
<pre><script> with(Math) {
n=25; k=15
c=1; for(i=0; i<k; i=i+1) { c=c*(n-i)/(k-i) }
document.write("C("+n+","+k+") = "+round(c))
} </script></pre>

C(25,15) = 3268760    More than 3 million!

(10)   The integration.  Consider the integral between 1 and 5 of x → 2*x*x + 3*x + 1.
We proceed by approximating the area with n small rectangles (I divide the interval into 10 small intervals which I take as the bases of the rectangles; as their heights I take the values of the function in their center; if the values are negative I take the small rectangles facing downwards).  We start with n=10 and we gradually double n:
<pre><script> with(Math) {
function F(x) { return  2*x*x + 3*x + 1 }; a = 1; b = 5
document.writeln("F(x) = 2*x*x + 3*x + 1; a = 1; b = 5")
n=10
for(i = 0; i < 5; i = i+1) {
  s=0; h=(b-a)/n; for (var j=0; j < n; j=j+1) {s = s + F(a+(j+1/2)*h)}
  document.writeln(n, " rectangles, ∫[a,b]F = ", s*h); n=n*2
   }
} </script></pre>
F(x) = 2*x*x + 3*x + 1; a = 1; b = 5
10 rectangles, ∫[a,b]F = 122.56000000000004
20 rectangles, ∫[a,b]F = 122.64000000000001
40 rectangles, ∫[a,b]F = 122.66000000000003
80 rectangles, ∫[a,b]F = 122.66500000000003
160 rectangles, ∫[a,b]F = 122.66624999999999
We increase n, putting n=10000 instead of n=10:
F(x) = 2*x*x + 3*x + 1; a = 1; b = 5
10000 rectangles, ∫[a,b]F = 122.66666655999998
20000 rectangles, ∫[a,b]F = 122.66666663999993
40000 rectangles, ∫[a,b]F = 122.66666665999948
80000 rectangles, ∫[a,b]F = 122.6666666649996
160000 rectangles, ∫[a,b]F = 122.66666666625022
I assume that ∫[a,b]F = 122.666… = 122+2/3 = 368/3

Another example: calculation of  ∫[0,3] |x·(x−2)| dx
F(x) = abs(x*(x-2)); a = 0; b = 3
10 rectangles, ∫[a,b]F = 2.6550000000000002
20 rectangles, ∫[a,b]F = 2.6634374999999997
...
81920 rectangles, ∫[a,b]F = 2.6666666664804004
163840 rectangles, ∫[a,b]F = 2.6666666666201007
I assume that ∫[0,3] |x·(x−2)| dx = 2+2/3 = 8/3  (with Geogebra I would have gotten 2.67!!!  For integration, search for maximum/minimum, inflections, … it is certainly not a reliable software …)


(11)   The linear regression.  What can I say about the linear function F such that F(5)=10 whose graph passes through the following points? (there are many such problems; a simple example: the elongation of a rubber band or spring detected by hanging objects of different weights on it)
x:  12±1  17 ±1  24±1   33±1.5
y:  18±2  27±2  31±2.5  45±3

Look at these figures. The first represents the straight line that best approximates the points without taking into account their precisions. The second represents the lines of minimum and maximum slope that pass through all the points, or, better, through all the approximate points, which are therefore in reality small rectangles.
The first straight line is found with a non-trivial algorithm on which this script is based; from it we obtain that y = 1.225*x+3.875.
The straight lines of the second figure are instead easy to find. We can use this simple program:
<pre><script> with(Math) {
P = [5,10]; n = 4
x = [12, 17, 24, 33]; dx = [1, 1, 1, 1.5]
y = [18, 27, 31, 45]; dy = [2, 2, 2.5, 3]
pend1 = (y[0]-dy[0]-P[1])/(x[0]+dx[0]-P[0])
for(i=1; i<n; i=i+1) {p = (y[i]-dy[i]-P[1])/(x[i]+dx[i]-P[0]); if(p > pend1) pend1 = p}
pend2 = (y[0]+dy[0]-P[1])/(x[0]-dx[0]-P[0])
for(i=1; i<n; i=i+1) {p = (y[i]+dy[i]-P[1])/(x[i]-dx[i]-P[0]); if(p < pend2) pend2 = p}
document.write( P[1] + " + (x-" + P[0] + ") * " +pend1 + "  <=  y  <<=  " + P[1] + " + (x-" + P[0] + ") * " +pend2 )
} </script></pre>
10 + (x-5) * 1.1538461538461537  <=  y  <=  10 + (x-5) * 1.3055555555555556
which I can round to  10 + (x-5) * 1.154  <=  y  <=  10 + (x-5) * 1.306