Translated using translate google. The original language is Russian. Original page https://hi-aga.ru/index.php/homepage/for-11-20

Example eleven. Find the sum of squares from N to 2* N. The scheme of work is similar to the previous examples, we also set up a counter on the stack, the limits of the loop parameter are absolutely the same, before summing we square each element.

: FOR11 ( N>0 -> ) \ N^2+[N+1]^2+[N+2]^2+…+[2*N]^2
0 \ N -> N 0 = Sum – total amount
OVER 1+ 0 \ N>0 Sum -> N Sum N+1 0 – the last 2 numbers are the limits of the cycle
DO\N Sum N+1 0 -> N Sum
    OVER I + DUP * + \ N Sum -> N Sum+(N+I)^2
LOOP. DROP; \ Sum – print the result, N – delete the excess
\ Test example eleven:
3 FOR11
86 OK
4 FOR11
190 Ok
5 FOR11
355 OK

Let's check for three: (3+0)^2+(3+1)^2+(3+2)^2+(3+3)^2= 9+16+25+36=86 – everything is correct.

Example twelve. Find the product of N factors 1.1*1.2*…. Here, in a loop for N elements, you need to calculate each one using the formula (1+0.1*N) and multiply it with the total value of the product, the initial value of which will be one.

: FOR12 ( N>0 -> ) \ 1.1*1.2*1.3*...
1+ 1 \ I: N -> N+1 1
1E\F: 1=P
DO\I: N+1 1 ->
    I 0 D>F 10E F/ 1E F+ \ F: P -> P 1+0.1*I
    F* \ P 1+0.1*I -> P*(1+0.1*I)
LOOP F. ; \ P*(1+0.1*I) – print the final product
\ Tests for the twelfth example:
1 FOR12
1.1000000 Ok
2 FOR12
1.3200000 Ok
3 FOR12
1.7160000 Ok
4 FOR12
2.4024000 Ok
5 FOR12
3.6036000 Ok

1.1 is the first element (1 FOR12), for two elements we get (1.1*1.2)=1.32 (2 FOR12), for three - we multiply the previous one by 1.3 we get (1.1*1, 2*1.3)=1.716 (3 FOR12), then check it yourself.

Example thirteen. Similar to the previous one, we replace the product with a sum and for each element we change the sign to the opposite, multiplying by minus one. To do this, we create a variable, which we initialize to one, then after calculating the next member of the series, we change its sign.

FVARIABLE S\S – SIGN variable sign
: FOR13 ( N>0 -> ) \ 1.1-1.2+1.3-...
1E S F! \ initially assign one, each time the word is called again
1+ 1 \ I: N -> N+1 1 – cycle parameter limits
0E\F: 0 = Sum
DO\I: N+1 1 ->
    I 0 D>F 10E F/ 1E F+ \ F: Sum -> Sum 1+0.1*I
    S F@ FDUP FNEGATE S F! F* \ F: Sum 1+0.1*I -> Sum (1+0.1*I)*S , S = - S
    F+ \ Sum (1+0.1*I)*S -> Sum+(1+0.1*I)*S
LOOP F. ;
\ Traditionally, a test word that will help you avoid entering the same type of data many times:
: TEST-FOR13 10 1 DO I DUP . FOR13 CR LOOP ;
TEST-FOR13
1 1.1000000
2 -0.1000000
3 1.2000000
4 -0.2000000
5 1.3000000
6 -0.3000000
7 1.4000000
8 -0.4000000
9 1.5000000
 Ok

Example fourteen. We calculate the square of a number using the formula (sum of the series) without using multiplication. The word must print the squares of all integers from 1 to N inclusive.

: FOR14 ( N>0 -> ) \ N^2=1+3+5+...+{2*N-1}
1+ 1 0 ROT ROT \ N -> 0 N+1 1 - standard cycle parameter limits
DO \ 0 N+1 1 -> 0=Sum – sum of the series
    I 2* 1- + DUP . \ Sum -> Sum+(2*I-1) – current sum of the series, duplicate and print
LOOP DROP ; \ before exiting, leave the stack as it was, removing the original parameter
\ Test word for automatic testing, displays the following beautiful data:
: TEST-FOR14 11 1 DO I FOR14 CR LOOP ;
TEST-FOR14
1
14
1 4 9
1 4 9 16
1 4 9 16 25
1 4 9 16 25 36
1 4 9 16 25 36 49
1 4 9 16 25 36 49 64
1 4 9 16 25 36 49 64 81
1 4 9 16 25 36 49 64 81 100
 Ok

Example fifteen. Raise a given real number to an integer power by multiplying by itself the appropriate number of times.

: FOR15 ( F: A I: N>0 -> ) \ 1.5E 5 FOR15 – word usage format, outputs A^N
1E \ F: A –> A 1=P – power of the original number, initially equal to one
0 \ I: N>0 -> I: N>0 0 - lower limit of the loop parameter
DO
    FOVER F* \ A P -> A P*A – simply P equal to one multiplied 15 times by A
LOOP F. FDROP ; \ Print the degree and delete the original number A
\ Let's test by raising two to the power from 1 to 10, using a test word:
: TEST-FOR15 11 1 DO 2E I FOR15 LOOP ;
TEST-FOR15
2.0000000 4.0000000 8.0000000 16.000000 32.000000 64.000000 128.00000 256.00000 512.00000 1024.0000 Ok

A small joke in the work of the word FOR15, since both parameters of the word are on different stacks, it does not matter in what order they are entered, which means that the examples

2E 1 FOR15
1 2E FOR15

will give the same and correct result, despite the incorrect syntax and logic.

In Forth, you can raise a number to a power in a more traditional way “in one action”:

: FOR15 ( F: A N -> ) \ A^N
F**F. ;
2E 10E FOR15
1024.0000 Ok

Two to a tenth 1024. Here the order is important and both parameters are real, because for an integer it is necessary to transfer it to a real stack. The code has become much shorter, but the first option serves as a clear example of how loops work, rather than effectively raising a number to a power.

Example sixteen. We get from the previous one with small changes. Now we print the result not just once (at the end after “LOOP”), but each time inside the loop, after calculating the next degree “FOVER F*”.

: FOR16 ( F: A I: N>0 -> ) \ 1.5E 5 FOR15 – word usage format, outputs A A^2 ... A^N
1E \ F: A –> A 1=P – power of the original number, initially equal to one
0 \ I: N>0 -> I: N>0 0 - lower limit of the loop parameter
DO
    FOVER F* FDUP F. CR \ A P -> A P*A – duplicate P*A and display it on the screen
LOOP FDROP ; \ remove the original number A
\ Example of how a word works:
2E 10 FOR16
2.0000000
4.0000000
8.0000000
16.000000
32.000000
64.000000
128.00000
256.00000
512.00000
1024.0000
 Ok
\ If we remove “CR” in the loop after outputting the next degree, we get:
2E 10 FOR16
2.0000000 4.0000000 8.0000000 16.000000 32.000000 64.000000 128.00000 256.00000 512.00000 1024.0000 Ok

The word test from example 15 (TEST-FOR15) is absolutely identical to how it works.

Example seventeen. Now we simply sum up the derived degrees in the previous example and output one result. We use a variable of real type; we will store the total amount in it.

FVARIABLE F1
: FOR17 ( F: A I: N>0 -> ) \ 1.5E 5 FOR17 – word call form, output 1+A+A^2+A^3+...+A^N
1E FDUP F1 F! \ 1 = P – degree A is first assigned one, duplicated and saved in F1
0\N -> N 0 - lower bound of the cycle
DO\N0 ->
    FOVER F* FDUP \ F: A 1 -> A 1*A 1*A
    F1 F@ F+ F1 F! \ F1=1*A+F1
LOOP
F1 F@ F. FDROP FDROP ; \F1. – printout of the result, removal of unnecessary data

Let's test for A=2 (in this case we will get very interesting numbers), run the test word from 1 to 10, I think this should be enough.

: TEST-FOR17 11 1 DO 2E ." N=" I DUP . FOR17 CR LOOP ;
TEST-FOR17
N=1 3.0000000
N=2 7.0000000
N=3 15.000000
N=4 31.000000
N=5 63.000000
N=6 127.00000
N=7 255.00000
N=8 511.00000
N=9 1023.0000
N=10 2047.0000
 Ok

Experiment with other foundations yourself.

Example eighteen. We obtain from the previous one by multiplying each term of the sum by minus one in a loop, thus changing its sign we get an alternating series (as in the condition).

FVARIABLE F1
: FOR18 ( F: A I: N -> )  \ 1.5E 5 FOR18 – пример вызова, результат 1-A+A^2-A^3+...+(-1) ^N*A^N
1E FDUP F1 F!       \ F: A -> A 1=P – дублируем 1 и сохраняем в F1 = сумма ряда
0                   \ I: N -> N 0 – границы параметра цикла
DO                  \ N 0 ->
    FOVER F*        \ A P -> A P*A
    -1E F* FDUP     \ A P*A -> A P*A*(-1) P*A*(-1)
    F1 F@ F+ F1 F!  \ F1=F1+(P*A*(-1))
LOOP
F1 F@ F.            \ распечатка итоговой суммы ряда
FDROP FDROP ;       \ очистка оставшихся данных
: TEST-FOR18 11 1 DO 2E I DUP . FOR18 CR LOOP ;
\ Результат работы тест слова TEST-FOR18:
TEST-FOR18
1 -1.0000000
2 3.0000000
3 -5.0000000
4 11.000000
5 -21.000000
6 43.000000
7 -85.000000
8 171.00000
9 -341.00000
10 683.00000
 Ok

Two's squares: 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024.

We get the series: 1-2=-1; -1+4=3; 3-8=-5; -5+16=11; 11-32=-21; -21+64=43; 43-128=-85; -85+256=171…

Example nineteen. We calculate the factorial in real format (so to speak, we determine its order, and not the exact integer value).

: FOR19 ( N>0 -> ) \ N! – print factorial
1+ 1 \ N –> N+1 1 – cycle parameter limits
1E \ F: 1E=Fact – the value of the factorial is initially equal to one
DO\N+1 1 ->
    I 0 D>F F*
    \ F: Fact -> Fact*I – I is converted into a double precision word by simply adding
    \ zero, so that we can then transfer it to the real stack, and multiply by Fact
LOOP
G. ; \printout of the result

The "F" word changed it to “G.” so that there is an indicator. Since the format of the first one does not show it. Final work word test:

: TEST-FOR19 20 1 DO I DUP . ." != " FOR19 CR LOOP ;
TEST-FOR19
1 != 1.0000000
2 != 2.0000000
3 != 6.0000000
4 != 24.000000
5 != 120.00000
6 != 720.00000
7 != 5040.0000
8 != 40320.000
9 != 362880.00
10 != 3628800.0
11 != 39916800.
12 != 4.7900160e08
13 != 6.2270208e09
14 != 8.7178291e10
15 != 1.3076744e12
16 != 2.0922790e13
17 != 3.5568743e14
18 != 6.4023737e15
19 != 1.2164510e17
 Ok

Example twenty. We can get it from the previous one with a small modification. Add S - the sum of factorials (line three). And in the ninth line we add the current factorial to this amount. Before printing the answer, reset the last current factorial.

: FOR20 ( N>0 -> )  \ 1!+2!+…+N! – вывести сумму факториалов
1+ 1        \ I: N –> N+1 1 – пределы параметра цикла
0E          \ F: 0E= S – значение суммы факториалов вначале равно нулю
1E          \ F: S -> S 1=F - текущий факториал
DO          \ N+1 1 ->
    I 0 D>F F*
    \ F: S F -> S F*I – I переводим в слово двойной точности, просто добавив
    \ ноль, чтобы потом перевести его на вещественный стек, и умножаем на F
    FSWAP FOVER F+ FSWAP             \ S F*I -> S+F*I F*I
LOOP
FDROP G. ;  \ распечатка результата
\ Тест слово для FOR20 на диапазоне данных от 1 до 10:
: TEST-FOR20 21 1 DO I FOR20 CR LOOP ;
TEST-FOR20
1.0000000
3.0000000
9.0000000
33.000000
153.00000
873.00000
5913.0000
46233.000
409113.00
4037913.0
43954713.
5.2295631e08
6.7499771e09
9.3928268e10
1.4016026e12
2.2324393e13
3.7801182e14
6.7803855e15
1.2842549e17
2.5613275e18
 Ok