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

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

In Internet pages like this, calculation procedures ("programs") can be inserted which allow various types of processing to be carried out. These procedures are called "scripts". It is the most used way of making programs (coding).  Let's see various examples, some accessible since basic school.  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).  For ready-made scripts, which you can use as a "black box", see here;  in particular, for basic school,  see:  how to draw with a computera game  and  these.

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.  You can also copy the following by trying to put the program lines in place of ... and removing "$" (or other symbol you used) to run it, and possibly putting back "$" to make new changes before running it again ( removing "$").
<pre><script> with(Math) {$ 
...
} </script></pre>
[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 C, in this documentc]

A)   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 get output with a word or phrase use
document.write(" ... ")
B)   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>

C)   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

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
D)   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


What does the following program calculate if you put any number in A and any natural number in N? Experiment with some values of A and of N.
<pre><script> with(Math) {

A = 2; N = 3
P=1
for(i=0; i<=N; i=i+1) P = P*A
document.write( P )

} </script></pre>
     

      read A
      read N
     set P = 1
     set I = 0
        |       yes
----> I = N ? ------->  write P
|       | no
|    set P = P*A
|    set I = I+1
|       |
--------

The sum of two natural numbers however long, having put the digits in two sequences (using a "decreasing" for loop and an instruction if, where "equal" is written "==""):
<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]
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]
n=A.length; R=0; C=""
for(i=n-1; i>=0; i=i-1) {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 +
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 =
35712545831689141105424671
or the sum of two non-integer numbers
<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

E)   If instead we had used:
<pre><script> with(Math) {

A=3; for(i=1; i<10; i=i+1) {A = -A; document.write(A,"  ")}

} </script></pre>
we would have obtained:
-3  3  -3  3  -3  3  -3  3  -3
There are now two loop instructions; are placed in braces and separated by ";".

F)   Another useful statement is  if
<pre><script> with(Math) {

 time = 10
 phrase = "It's night"
 if(time == 12) phrase = "It's noon"
 if(time < 21 && time > 12) phrase = "It's afternoon"
 if(time < 12 && time >= 7) phrase = "It's morning"
 document.write( phrase )


} </script></pre>
Output:  It's morning
If I put  time = 22   I get:  It's night

In a statement if (as we had already seen)  "equals"  is written   "==",   "and"  is written  "&&";  "not" is written "!";  "different" is written "!=";  "or" is written "||".

More examples

G)
<pre><script> with(Math) {

part = 163; total = 384
document.write( "percentage = ", round(part/total*100) )


} </script></pre>
Output:  percentage = 42

<pre><script> with(Math) {

P1 = 163; P2 = 247; P3 = 102; total = P1+P2+P3
document.write( "total = ", total, "<br>" )
document.write( "percentages: ",round(P1/total*100)," ",round(P2/total*100)," ",round(P3/total*100) )


} </script></pre>
Output:
total = 512
percentages: 32 48 20


By changing the data I get the new outputs immediately

H)   Two tables.
<pre><script> with(Math) {

for(i=1; i<10; i=i+1)
   { for(j=1; j<10; j=j+1)
        {if(i*j<10) document.write(" "); document.write(i*j); document.write(" ")}
    document.write("<br>") }


} </script></pre>
Output:
 1  2  3  4  5  6  7  8  9 
 2  4  6  8 10 12 14 16 18 
 3  6  9 12 15 18 21 24 27 
 4  8 12 16 20 24 28 32 36 
 5 10 15 20 25 30 35 40 45 
 6 12 18 24 30 36 42 48 54 
 7 14 21 28 35 42 49 56 63 
 8 16 24 32 40 48 56 64 72 
 9 18 27 36 45 54 63 72 81 
Two for loops appear, one boxed within the other:  the "i" loop prints the lines;  each row is printed by a loop "j";  the first document.write(" ") writes a blank space before the number if it has only one digit;  the final document.write(" ") writes the blank space that separates one number from the next;  when exiting the boxed loop, document.write("<br>") generates a "new line" between one line and the next.

With small modifications (which ones?) a multiplication table with more numbers is produced:
<pre><script> with(Math) {

for(i=1; i<21; i=i+1)
   { for(j=1; j<21; j=j+1)
       {if(i*j<10) document.write(" ");if(i*j<100) document.write(" ");document.write(i*j);document.write(" ")}
    document.write("<br>") }


} </script></pre>
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20 
  2   4   6   8  10  12  14  16  18  20  22  24  26  28  30  32  34  36  38  40 
  3   6   9  12  15  18  21  24  27  30  33  36  39  42  45  48  51  54  57  60 
  4   8  12  16  20  24  28  32  36  40  44  48  52  56  60  64  68  72  76  80 
  5  10  15  20  25  30  35  40  45  50  55  60  65  70  75  80  85  90  95 100 
  6  12  18  24  30  36  42  48  54  60  66  72  78  84  90  96 102 108 114 120 
  7  14  21  28  35  42  49  56  63  70  77  84  91  98 105 112 119 126 133 140 
  8  16  24  32  40  48  56  64  72  80  88  96 104 112 120 128 136 144 152 160 
  9  18  27  36  45  54  63  72  81  90  99 108 117 126 135 144 153 162 171 180 
 10  20  30  40  50  60  70  80  90 100 110 120 130 140 150 160 170 180 190 200 
 11  22  33  44  55  66  77  88  99 110 121 132 143 154 165 176 187 198 209 220 
 12  24  36  48  60  72  84  96 108 120 132 144 156 168 180 192 204 216 228 240 
 13  26  39  52  65  78  91 104 117 130 143 156 169 182 195 208 221 234 247 260 
 14  28  42  56  70  84  98 112 126 140 154 168 182 196 210 224 238 252 266 280 
 15  30  45  60  75  90 105 120 135 150 165 180 195 210 225 240 255 270 285 300 
 16  32  48  64  80  96 112 128 144 160 176 192 208 224 240 256 272 288 304 320 
 17  34  51  68  85 102 119 136 153 170 187 204 221 238 255 272 289 306 323 340 
 18  36  54  72  90 108 126 144 162 180 198 216 234 252 270 288 306 324 342 360 
 19  38  57  76  95 114 133 152 171 190 209 228 247 266 285 304 323 342 361 380 
 20  40  60  80 100 120 140 160 180 200 220 240 260 280 300 320 340 360 380 400
I)   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
We can introduce the function with  function.  Let's see how:
<pre><script> with(Math) {

function F(x) { return  pow(x,5)+6*pow(x,3)+x/7 }
document.writeln("F(x) = pow(x,5)+6*pow(x,3)+x/7")

x = 1.5; document.write( "x=",x, " -> ",F(x), "\n" )
x = 2; document.write( "x=",x, " -> ",F(x), "\n" )
x = 3.5; document.write( "x=",x, " -> ",F(x), "\n" )
x = 3; document.write( "x=",x, " -> ",F(x), "\n" )

} </script></pre>
F(x) = pow(x,5)+6*pow(x,3)+x/7
x=1.5 -> 28.058035714285715
x=2 -> 80.28571428571429
x=2.5 -> 191.76339285714286
x=3 -> 405.42857142857144

I can define a function that can be called from other functions. For example, a function to round to the nth digit after the ".":
    function APPR(x,n) {return round(x*pow(10,n))/pow(10,n)}
<pre><script> with(Math) {

function APPR(x,n) {return round(x*pow(10,n))/pow(10,n)}
function F(x) { return  pow(x,5)+6*pow(x,3)+x/7 }
x = 3; document.writeln( "x=",x, " -> ",F(x) )
x = 3; document.writeln( "x=",x, " -> ",APPR(F(x),10) )
x = 3; document.writeln( "x=",x, " -> ",APPR(F(x),5) )
x = 3; document.writeln( "x=",x, " -> ",APPR(F(x),0) )
x = 3; document.writeln( "x=",x, " -> ",APPR(F(x),-1) )
x = 3; document.writeln( "x=",x, " -> ",APPR(F(x),-2) )

} </script></pre>
x=3 -> 405.42857142857144
x=3 -> 405.4285714286
x=3 -> 405.42857
x=3 -> 405
x=3 -> 410
x=3 -> 400
J)   The mean (and sum, min, max) of a sequence of data
<pre><script> with(Math) {

n=5; A = [172,169,183,181,167]
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>

                             n = 5
                               |
  A[0] = 172, A[1] = 169, A[2] = 183, A[3] = 181, A[4] = 167 
                               |
                min = A[0], max = A[0], SUM = 0
                               |
                             i = 0
                               |
                       SUM = SUM + A[i]  <——————
                               |                  |
                   if A[i] > max put max = A[i] |
                               |                  |
                   if A[i] < min put min = A[i] |
                               |                  |
                          i = i + 1               |
                               |                  |
                           i < n ?  — YES ————————
                               |
                               |  NO
                               |
           write the values of SUM, SUM/n, min, max
                               |
                             STOP
Outputs:
the sum of 172,169,183,181,167  is  872
the mean is 872/5 = 174.4
min = 167   max = 183
Alternative, using "length", with the same outputs:
<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>
I can also order the  words:
<pre><script> with(Math) {

A = ["casa", "pino", "cielo", "zio", "aria"]
n = A.length; min = A[0]; max = A[0]
for(i=0; i<n; i=i+1) {if(A[i]>max) max=A[i]; if(A[i]<min) min=A[i]}
document.write ("min = ",min ,"   max = ", max )


} </script></pre>
Output:   min = aria    max = zio

Not just "minimum" and "maximum", but the complete sort
<pre><script> with(Math) {

A = ["casa", "pino", "cielo", "zio", "aria", "io", "tu", "nonna", "zia"]
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:
casa,pino,cielo,zio,aria,io,tu,nonna,zia
aria,casa,cielo,io,nonna,pino,tu,zia,zio

Of course, by just changing the input, I can sort numbers as well:
<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]

K)   Bar charts / histograms:
<pre><script> with(Math) {

data = [127,585,430,1256,148]
n = data.length
total = 0; for(i=0; i<n; i=i+1) total = total+data[i]
document.write(data); document.write("   HISTOGRAM:\n\n")
for(i=0; i<n; i=i+1) { p = round(data[i]/total*150); 
                      for(k=1; k<=p; k=k+1) document.write("#");
                      document.write("  ",data[i],"\n") }


} </script></pre>
127,585,430,1256,148   HISTOGRAM:

#######  127
##################################  585
#########################  430
##########################################################################  1256
#########  148
Alternative:
<pre><script> with(Math) {

data = [127,585,430,1256,148]
n = data.length
total = 0; for(i=0; i<n; i=i+1) total = total+data[i]
document.write(data); document.write("   HISTOGRAM:\n\n")
for(i=0; i<n; i=i+1) { p = round(data[i]/total*100); 
                      for(k=1; k<=p; k=k+1) document.write("#");
                      document.write("  ", p,"%\n") }


} </script></pre>
127,585,430,1256,148   HISTOGRAM:

#####  5%
#######################  23%
#################  17%
#################################################  49%
######  6%

A slightly more complex example (female population of Ethiopia by age group)
<pre><script> with(Math) {

dati = [9055301, 7980654, 7334048, 6885687, 6167008, 5339883, 4491727, 3583550, 2853732, 2342455,
1942273, 1584971, 1226116, 869631, 623680, 404849, 201780, 73468, 17910, 2455, 173]
n = dati.length
totale = 0; for(i=0; i<n; i=i+1) totale = totale+dati[i]
document.write("HISTOGRAM female population of Ethiopia by age groups:\n\n")
for(i=0; i<n; i=i+1) { p = round(dati[i]/totale*1000); p2 = round(dati[i]/totale*100000)/1000; 
                      if(i<2) document.write(" "); if(i<20) document.write(" ")
                      document.write(i/2*10); document.write(" ")
                      for(k=1; k<=p; k=k+1) document.write("#");
                      document.write("  ", p2,"%\n") }


} </script></pre>
HISTOGRAM female population of Ethiopia by age groups: [0,5), [5,10), ...

  0 ################################################################################################################################################  14.378%
  5 ###############################################################################################################################  12.671%
 10 ####################################################################################################################  11.645%
 15 #############################################################################################################  10.933%
 20 ##################################################################################################  9.792%
 25 #####################################################################################  8.479%
 30 #######################################################################  7.132%
 35 #########################################################  5.69%
 40 #############################################  4.531%
 45 #####################################  3.719%
 50 ###############################  3.084%
 55 #########################  2.517%
 60 ###################  1.947%
 65 ##############  1.381%
 70 ##########  0.99%
 75 ######  0.643%
 80 ###  0.32%
 85 #  0.117%
 90   0.028%
 95   0.004%
100   0%

L)   Simple designs
<pre><script> with(Math) {

document.writeln( "    0000000000000000")
document.writeln( "   0                0")
document.writeln( "  0                  0")
document.writeln( " 0                    0")
document.writeln( "0----------------------0")
document.writeln( "0                      0")
document.writeln( "0       =======        0")
document.writeln( "0       |     |        0")
document.writeln( "0       |     |        0")
document.writeln( "0       |     |        0")
document.writeln( "000000000000000000000000")


} </script></pre>
    0000000000000000
   0                0
  0                  0
 0                    0
0----------------------0
0                      0
0       =======        0
0       |     |        0
0       |     |        0
0       |     |        0
000000000000000000000000
M)   What should I put in place of "..." to get the output below reproduced?
<pre><script> with(Math) {

...
for(i=1; i<=n; i=i+1) {for(k=1;k<=i;k=k+1) document.write(0); document.write("<br>")}


} </script></pre>
0
00
000
0000
00000
000000
0000000
00000000
000000000
0000000000
N)   In this example, the user only has to manage the assignment to "go", to command the movements, to the Right, Left, Up and Down of "0", like the movement of a snake.  As in other cases, the user can gradually modify the input, immediately seeing the changes in the outputs.  The following lines are understandable by older kids: a 50×50 array is built which contains for each column and each row the characters to appear on the screen: "0" or " " (i.e. a blank space character).
<pre><script> with(Math) {

R=1; L=2; U=3; D=4
go = [R,R,R,R,R,R,R,R,R,R,R,R,D,D,D,D,D,D,D,D,D,L,L,L,L,L,L,L,L,L,L,L,
    U,U,U,U,U,U,U,R,R,R,R,R,R,R,R,D,D,D,D,D,L,L,L,L,L,L,U,U,U,R,R,R,R,D]

var T = new Array(50);
for (i=0; i<50; i=i+1) {T[i]=new Array(50)}
for (i=0; i<50; i=i+1) {for(j=0; j<50; j=j+1) T[i][j]=" "}
n = go.length;  u=0; v=0
for(i=0; i<n; i=i+1) {
  if(go[i] == R) {v=v+1; T[u][v]=0}
  if(go[i] == L) {v=v-1; T[u][v]=0}
  if(go[i] == U) {u=u-1; T[u][v]=0}
  if(go[i] == D) {u=u+1; T[u][v]=0}
}
for (i=0; i<50;i=i+1) {for(j=0; j<50; j=j+1) {document.write(T[i][j]); if(j==49) document.write("<br>")} }


} </script></pre>
 000000000000                                     
            0                                     
 000000000  0                                     
 0       0  0                                     
 0 00000 0  0                                     
 0 0   0 0  0                                     
 0 0     0  0                                     
 0 0000000  0                                     
 0          0                                     
 000000000000
Variant, introducing the possibility of inserting empty spaces (to break up the "snake") using "0"
<pre><script> with(Math) {

R=1; L=2; U=3; D=4
go = [R,R,R,L,L,L,D,D,D,R,R,R, 0,R,R,0,
      R,U,U,U, 0,R,R,0,
      R,D,D,D,U,U,U,R,R,R,D,D,D,U,L,L, 0,R,R,R,R,0,
      R,D,R,R,R,U,U,U,L,L,L,D]

var T = new Array(50);
for (i=0; i<50; i=i+1) {T[i]=new Array(50)}
for (i=0; i<50; i=i+1) {for(j=0; j<50; j=j+1) T[i][j]=" "}
n = go.length; u=0; v=0; traccia=1
for(i=0; i<n; i=i+1) {if(go[i] == 0) traccia = -traccia
  if(go[i] == R) {v=v+1; if(traccia==1) T[u][v]=0}
  if(go[i] == L) {v=v-1; if(traccia==1) T[u][v]=0}
  if(go[i] == U) {u=u-1; if(traccia==1) T[u][v]=0}
  if(go[i] == D) {u=u+1; if(traccia==1) T[u][v]=0}
}
for (i=0; i<50;i=i+1) {for(j=0; j<50; j=j+1) {document.write(T[i][j]); if(j==49) document.write("<br>")} }


} </script></pre>
0000  0  0000  0000                               
0     0  0  0  0  0                               
0     0  0000  0  0                               
0000  0  0  0  0000                               

O)   A convenient statement (non-trivial for the first classes) 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


P)   Various examples
First of all, let us recall the names of other functions (of a) that can be used in addition to those called in C):
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).
p0)
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
The "experimental" evaluation of the probability that by throwing two fair dice the difference of the outputs is 1:
<pre><script> with(Math) {
n=1e4; x=0; for(i=0; i<n; i=i+1)
      { U=abs( floor(random()*6+1)-floor(random()*6+1) ); if(U==1) x = x+1 }
document.writeln (x/n*100,"%")
n=n*10; x=0; for(i=0; i<n; i=i+1)
      { U=abs( floor(random()*6+1)-floor(random()*6+1) ); if(U==1) x = x+1 }
document.writeln (x/n*100,"%")
n=n*10; x=0; for(i=0; i<n; i=i+1)
      { U=abs( floor(random()*6+1)-floor(random()*6+1) ); if(U==1) x = x+1 }
document.writeln (x/n*100,"%")
} </script></pre>
27.56%
27.79%
27.7626%
(theoretically I would find that the possible cases are 6*6 = 36 and the good ones are 10, and 10/36 = 5/18 = 0.2777…)
 
If I want to quantify the precision of the experimental estimate (by means of an interval in which the probability falls at 99.7%), I can add "±3·σ√n" (for theoretical justifications see the topic "central limit theorem"):
<pre><script> with(Math) {
n=1e4; x=0; for(i=0; i<n; i=i+1)
      { U=abs( floor(random()*6+1)-floor(random()*6+1) ); if(U==1) x = x+1 }
document.writeln (x/n*100,"%  +/- ",sqrt(x/n*(1-x/n)/sqrt(n-1)*300),"%" )
n=n*10; x=0; for(i=0; i<n; i=i+1)
      { U=abs( floor(random()*6+1)-floor(random()*6+1) ); if(U==1) x = x+1 }
document.writeln (x/n*100,"%  +/- ",sqrt(x/n*(1-x/n)/sqrt(n-1)*300),"%" )
n=n*10; x=0; for(i=0; i<n; i=i+1)
      { U=abs( floor(random()*6+1)-floor(random()*6+1) ); if(U==1) x = x+1 }
document.writeln (x/n*100,"%  +/- ",sqrt(x/n*(1-x/n)/sqrt(n-1)*300),"%" )
} </script></pre>
26.94%  +/- 0.7684402681799349%
27.979%  +/- 0.4372274105711382%
27.7392%  +/- 0.24522180483732334%
Stopping at 1 million experimental trials I have (27.74±0.25)%
 
p1)

<pre><script> with(Math) {
n = 20; S = 0
for(i=0; i<=n; i=i+1) S = S+i
document.write(S)
} </script></pre>

           210 ( = 0+1+2+...+20 )


      N = 20
        |
  S = 0, i = 0
        |
   S = S + i  <———————
        |             | 
   i = i + 1          |
        |             |
     i ≤ N ?  — YES ——
        |
        |  NO
        |
     write S
        |
      STOP
    
S = 0,  i = 0
S = S+i = 0,  i = i+1 = 1
i ≤ 20?  yes
S = S+i = 1,  i = i+1 = 2
i ≤ 20?  yes
S = S+i = 3,  i = i+1 = 3
i ≤ 20?  yes
S = S+i = 6,  i = i+1 = 4
i ≤ 20?  yes
...
S = S+i = 190,  i = i+1 = 20
i ≤ 20?  sì
S = S+i = 210,  i = i+1 = 21
i ≤ 20?  no
write 210
p2) <pre><script> with(Math) { n = 5; P = 1 for(i=1; i<=n; i=i+1) P = P*i document.write(P) } </script></pre> 120 ( = 1*2*3*4*5 )

      N = 5
        |
  P = 1, i = 1
        |
   P = P * i  <———————
        |             | 
   i = i + 1          |
        |             |
     i ≤ N ?  — YES ——
        |
        |  NO
        |
     write P
        |
      STOP
    
P = 1,  i = 1
P = P*1 = 1,  i = i+1 = 2
i ≤ 5?  yes
P = P*2 = 2,  i = i+1 = 3
i ≤ 5?  yes
P = P*3 = 6,  i = i+1 = 4
i ≤ 5?  yes
P = P*4 = 24,  i = i+1 = 5
i ≤ 5?  yes
P = P*5 = 120,  i = i+1 = 6
i ≤ 5?  no
write 120
p3) <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
p4) <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.
That is, I can generate a triangle with this script and then check the response with what we have just seen.
p5)
2nd degree equations:
<pre><script> with(Math) {
title = "Solutions of  a*x^2 + b*x + c = 0"
a = 1
b = 2
c = -15
document.write(title+"\n     a = ",a,", b = ",b, ", c = ",c+"<br>")
d = b*b-4*a*c
k1 = -b/(2*a) + sqrt(d)/(2*a); k2 = -b/(2*a) - sqrt(d)/(2*a)
if(d < 0) document.write("no solution")
if(d > 0) document.write(k1, "  ", k2)
if(d == 0) document.write(k1)
} </script></pre>
Solutions of  a*x^2 + b*x + c = 0
     a = 1, b = 2, c = -15
3  -5
3rd degree equations
Let's put the solution procedure, without explaining it.
<pre><script> with(Math) {
A = 1; B = 0; C = 0; D = 2
document.writeln("solutions of A*x^3 + B*x^2 + C*x + D = 0")
document.writeln("A = ", A,", B = ",B,", C = ",C,", D = ",D)
B=B/A; C=C/A; D=D/A; P=C-B*B/3; Q=D-C*B/3+2*B*B*B/27; T = 4*P*P*P/27+Q*Q; S=[]
if(T<0) { ang=acos(3*Q/2/P*sqrt(-3/P))/3; for(i=0;i<3;i=i+1) S[i] = sqrt(-4*P/3)*cos(ang+i*PI*2/3)-B/3 }
if(T==0) { S[0] = 2*cbrt(-Q/2)-B/3; S[1] = -cbrt(-Q/2)-B/3 }
if(T>0) { T=sqrt(T); S[0] = cbrt((-Q+T)/2)+cbrt((-Q-T)/2)-B/3}
if(T < 0) {document.write("3 sol: ", S[0], " ", S[1], " ", S[2])}
if(T ==0) {document.write("2 sol: ", S[0], " ", S[1])}
if(T > 0) {document.write("1 sol: ", S[0])}
} </script></pre>
solutions of A*x^3 + B*x^2 + C*x + D = 0
A = 1, B = 3, C = -10, D = -24
3 sol: 3 -3.9999999999999996 -2.0000000000000013  (or 3 -4, -2)

solutions of A*x^3 + B*x^2 + C*x + D = 0
A = 1, B = 9, C = 15, D = -25
2 sol: 1 -5

solutions of A*x^3 + B*x^2 + C*x + D = 0
A = 1, B = 0, C = 0, D = 2
1 sol: -1.2599210498948732

p6)
How is the square root of a number X calculated?  For example proceeding by bisection:  I start from an interval that contains the square root ([0,X] if X<1, [0,1/X] otherwise); little by little I find half of it, M; if M*M < X I take the right one as the new interval, otherwise I take the left one; and so on.
<pre><script> with(Math) {
x = 1000
a = 0;  if(x > 1) {b = x} else {b = 1/x}
for(n=1; n<15; n=n+1) {
    m = (a+b)/2; y = m*m
    if(y < x) {a = m} else {b = m}
    document.writeln("n = ",n , "   a = ", a,"   b = ",b)
    }
} </script></pre>
n = 1   a = 0   b = 500
n = 2   a = 0   b = 250
n = 3   a = 0   b = 125
n = 4   a = 0   b = 62.5
n = 5   a = 31.25   b = 62.5
n = 6   a = 31.25   b = 46.875
     ...
n = 14  a = 31.6162109375   b = 31.67724609375
If x = 0.1 I get:
n = 1   a = 0   b = 5
n = 2   a = 0   b = 2.5
     ...
n = 14  a = 0.316162109375   b = 0.3167724609375
Analogously I 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...)
p7)
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 (using WolframAlpha we could easily draw the graph) 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).

p8)
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.
p9)
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?
<pre><script> with(Math) {
document.write(25/15*24/14*23/13*22/12*21/11*20/10*19/9*18/8*17/7*16/6*15/5*14/4*13/3*12/2*11/1)
} </script></pre>

3268760    More than 3 million!
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
The "round" instruction is used to not display the possible variation from the whole numbers deriving from the approximations.

An example of a calculation using binomial coefficients: a calculation related to the binomial distribution law.
In a certain population of adults in a given region, it is known that a particular childhood disease has affected an average of 1 person out of 8; if 100 adults from that region are taken completely at random, what is the probability that no more than 10 have been affected by it?
To understand, let's see how to calculate the probability that 3 have been affected:
    the probability that the top 3 (or bottom 3 or 1st, 3rd and 9th or …) have been affected: (1/8)3·(1-1/8)97
    the ways i can choose the 3 people out of 100: C(100,3)
    the probability that exactly 3 were affected: C(100,3)·(1/8)3·(1-1/8)97
The solution of the problem: C(100,0)·(1/8)0·(1-1/8)100+C(100,1)·(1/8)1·(1-1/8)99+ ... + C(100,10)·(1/8)10·(1-1/8)90
<pre><script> with(Math) {

Q=0; P=1/8; n=100
for(k=0; k<11; k=k+1)
{ c=1; for(i=0; i<k; i=i+1) { c=c*(n-i)/(k-i) }
  Q = Q + c*pow(P,k)*pow(1-P,n-k) }
document.write(Q)

} </script></pre>
0.28099917939852637
The probability that no more than 10 people have been affected by the disease is 28.1%.
p10)
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 n 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, integral of F on [a,b] = ", s*h); n=n*2
   }
} </script></pre>
F(x) = 2*x*x + 3*x + 1; a = 1; b = 5
10 rectangles, integral of F on [a,b] = 122.56000000000004
20 rectangles, integral of F on [a,b] = 122.64000000000001
40 rectangles, integral of F on [a,b] = 122.66000000000003
80 rectangles, integral of F on [a,b] = 122.66500000000003
160 rectangles, integral of F on [a,b] = 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, integral of F on [a,b] = 122.66666655999998
20000 rectangles, integral of F on [a,b] = 122.66666663999993
40000 rectangles, integral of F on [a,b] = 122.66666665999948
80000 rectangles, integral of F on [a,b] = 122.6666666649996
160000 rectangles, integral of F on [a,b] = 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, integral of F on [a,b] = 2.6550000000000002
20 rectangles, integral of F on [a,b] = 2.6634374999999997
...
81920 rectangles, integral of F on [a,b] = 2.6666666664804004
163840 rectangles, integral of F on [a,b] = 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 …)

Another example: ∫[0,π/2] cos(x) dx
function F(x) { return  cos(x) };  a = 0; b = PI/2
5000 rectangles, integral of F on [a,b] = 1.0000000041123318
...
80000 rectangles, integral of F on [a,b] = 1.0000000000160585
Another example:  1 − ∫[0,35] 0.1·exp(-0.1·x) dx
function F(x) { return  0.1*exp(-0.1*x) };  a = 0; b = 35
[document.writeln(n, " rettangoli,  1 - integrale di F su [a,b] = ", 1-s*h); n=n*2]
5000 rettangoli, 1 - integrale di F su [a,b] = 0.030197403222453345
10000 rettangoli, 1 - integrale di F su [a,b] = 0.030197388372357636
20000 rettangoli, 1 - integrale di F su [a,b] = 0.03019738465983124
40000 rettangoli, 1 - integrale di F su [a,b] = 0.030197383731682903
80000 rettangoli, 1 - integrale di F su [a,b] = 0.03019738349965262

p11)
It is very easy to study sequences defined by recursion. Let's take just 3 examples:
  x[0] = A, x[n+1] = sqrt(x[n])
  x[0] = 1, x[n+1] = (x[n] + A/x[n]) / 2
  x[0] = A, x[1]=B, x[n+2] = (3*x[n+1]-x[n]) / 2
<pre><script> with(Math) {
A = 13
document.writeln("x[0] = ", A, ", x[n+1] = sqrt(x[n])")
x = A; N = 1
for(i=0; i<N; i=i+1) x = sqrt(x)
document.writeln("x[", i, "] = ", x)
} </script></pre>
    With A=13, changing N:
x[0] = 13, x[n+1] = sqrt(x[n])
x[1] = 3.605551275463989

x[20] = 1.0000024461293169

x[54] = 1

    With A=0.17:
x[0] = 0.17, x[n+1] = sqrt(x[n])
x[1] = 0.41231056256176607

x[54] = 0.9999999999999999
                            x[n] -> 1  as n -> ∞  whatever A is
<pre><script> with(Math) {
A = 36
document.writeln("x[0] = ", 1, ", x[n+1] = (x[n] + A/x[n]) / 2")
x = A; N = 1
for(i=0; i<N; i=i+1) x = (x + A/x)/2
document.writeln("x[", i, "] = ", x)
} </script></pre>
    With A=36 for increasing values of N:
x[0] = 1, x[n+1] = (x[n] + A/x[n]) / 2
x[1] = 18.5

x[2] = 10.222972972972974

x[5] = 6.0002529841194185

x[7] = 6
                            x[n] -> √A  as n -> ∞   whatever A > 0 is
A recursive sequence with 2 initial values:
<pre><script> with(Math) {
A = 1; B = 2
document.writeln("x[0] = ", A, ", x[1] = ", B, ", x[n+2] = (3*x[n+1]-x[n]) / 2")
x = [A, B]
N = 2
for(i=2; i<=N; i=i+1) x[i] = (3*x[i-1]-x[i-2]) / 2
document.writeln("x[", N, "] = ", x[N])
} </script></pre>
    With A = 1, B = 2  for increasing values of N:
x[0] = 1, x[1] = 2, x[n+2] = (3*x[n+1]-x[n]) / 2

x[0] = 1, x[1] = 2, x[n+2] = (3*x[n+1]-x[n]) / 2

x[55] = 2.999999999999999
Try different values of A and B  (e.g.: A = -3; B = 9 → x[55] = 21)  and verify that:
                                          x[n] -> 2*B - A
p12)
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
 
p13)
To print day and time use Date().
In any script you can add a space to make notes.
<pre><script> with(Math) {
document.write( Date() )
} </script></pre>
<input size=70 value="Notes">
<input size=70>
<input size=70>
<input size=70>



NOTE.  Specific INPUT statements could be inserted into scripts, but this would not lead to improvements in the case of simple scripts like these, in which one can directly change the values assigned to variables and immediately see the changes in the outputs.  Those interested see here.