一百個例題 ( 66 ~ 70 )
Ching-Tang Tseng
Hamilton, New Zealand
24 September 2024
\ (66)RecursiveGCD.f : GCD ( n1 n2 -- n3 ) ?DUP IF SWAP OVER MOD RECURSE THEN ; variable TotalSum : n- ( n -- ) ?dup if dup TotalSum +! 1- recurse then ; : ss ( n -- ) 0 TotalSum ! n- TotalSum @ . ;
\ (67)exponential regression
2 integers n i
9 reals s1 s2 s3 s4 s5 x y a b
20 array xx
20 array yy
: initialize
basic
10 let { s1 = 0.0e0 }
20 let { s2 = s1 }
30 let { s3 = s1 }
40 let { s4 = s1 }
50 let { s5 = s1 }
60 end
;
: InputData
basic
10 let n = 9
20 let { xx ( 0 ) = 0.0e0 } :: { yy ( 0 ) = 291.0e0 }
30 let { xx ( 1 ) = 1.0e0 } :: { yy ( 1 ) = 324.0e0 }
40 let { xx ( 2 ) = 2.0e0 } :: { yy ( 2 ) = 571.0e0 }
50 let { xx ( 3 ) = 3.0e0 } :: { yy ( 3 ) = 830.0e0 }
60 let { xx ( 4 ) = 4.0e0 } :: { yy ( 4 ) = 1287.0e0 }
70 let { xx ( 5 ) = 5.0e0 } :: { yy ( 5 ) = 1975.0e0 }
80 let { xx ( 6 ) = 6.0e0 } :: { yy ( 6 ) = 2744.0e0 }
90 let { xx ( 7 ) = 7.0e0 } :: { yy ( 7 ) = 4599.0e0 }
100 let { xx ( 8 ) = 8.0e0 } :: { yy ( 8 ) = 6078.0e0 }
200 end
;
: regression
basic
10 run initialize InputData
20 for i = 0 to n - 1
30 let { x = xx ( i ) }
40 let { y = ln ( yy ( i ) ) }
50 let { s1 = s1 + x }
60 let { s2 = s2 + y }
70 let { s3 = s3 + x * x }
80 let { s4 = s4 + y * y }
90 let { s5 = s5 + x * y }
100 next i
110 let { b = ( i>r ( n ) * s5 - s2 * s1 ) / ( i>r ( n ) * s3 - s1 * s1 ) }
120 let { a = exp ( ( s2 - B * s1 ) / i>r ( n ) ) }
130 print " exponential regression: y = a * exp ( b * t ) "
140 run cr ." a = " a f.
150 run cr ." b = " b f. cr
160 let { y = a * exp ( b * 8.0e0 ) }
170 run cr ." y(8) = " y f. cr
180 let { y = a * exp ( b * 9.0e0 ) }
190 run cr ." y(9) = " y f. cr
200 end
;
regression
\s
程式的執行結果如下:
exponential regression: y = a * exp ( b * t )
a = 254.72339048
b = 0.4020241052
y(8) = 6351.0221997
y(9) = 9493.8088062
\ factors.f (( '********************************************* '* Factorization of an integer number * '* ----------------------------------------- * '* Sample run: * '* * '* ? 394616 * '* * '* 2 2 2 107 461 * '* * '* ----------------------------------------- * '* Ref.: "Mathmatiques par l'informatique * '* individuelle (1) By H. Lehning * '* et D. Jakubowicz, Masson, Paris, * '* 1982" [BIBLI 06]. * '********************************************* )) 4 integers N D I L : test page basic 10 print " Enter integer number to be factorized " 20 inputi N \ 'Test if multiple of 2 40 let D = N mod 2 50 if D = 0 then 70 60 goto 100 70 print 2 80 let N = N / 2 90 goto -40 \ 'Test if multiple of 3 100 let D = N mod 3 110 if D = 0 then 130 120 goto 200 130 print 3 140 let N = N / 3 150 goto -100 \ 'Test of divisors 6i-1 and 6i+1 up to sqr(N) \ 'Prime numbers are of the form 6i-1 or 6i+1 200 let L = sqrt ( N ) + 1 210 for I = 6 to L step 6 220 let D = N mod ( I - 1 ) 230 if D = 0 then 250 240 goto 300 250 print ( I - 1 ) 260 let N = N / ( I - 1 ) 270 goto -220 300 let D = N mod ( I + 1 ) 310 if D = 0 then 330 320 goto 400 330 print ( I + 1 ) 340 let N = N / ( I + 1 ) 350 goto -300 400 next I 500 if N > 1 then 520 510 goto 600 520 print N 600 end ;
\ quotation.f
: ends> r> ;
: end >r ; immediate
\ quotations: 局部副程式
: >EXEC >R ; \ for Win32Forth only
: [:
postpone ahead here -rot
ends> postpone exit postpone then postpone literal
; immediate
: ;] >r ; immediate \ or an alias of end
: greet [: ." world" ;]
." hello"
>exec ; \ for Win32forth
\ execute ; \ for other forth
\ prime1.f
4 integers D N L I
: test
basic
10 print " Input an integer number N = "
20 inputi N
30 let D = N mod 2
40 if D = 0 then 220
50 let D = N mod 3
60 if D = 0 then 220
70 let L = sqrt ( N ) + 1
80 for I = 6 to L step 6
90 let D = N mod ( I - 1 )
100 if D = 0 then 210
110 let D = N mod ( I + 1 )
120 if D = 0 then 210
130 next I
140 print N ; " is a prime number."
150 goto 300
210 run 2drop
220 print N ; " is not a prime number."
300 end ;
\ prime2.f
3 integers N I L
: outrun 2drop ;
: test
PAGE
BASIC
10 print " Input an integer number N = "
20 inputi N
30 if ( N mod 2 = 0 ) OR ( N mod 3 = 0 ) then 220
40 let L = sqrt ( N ) + 1
50 for I = 6 to L step 6
60 if ( N mod ( I - 1 ) = 0 ) OR ( N mod ( I + 1 ) = 0 ) then 210
70 next I
110 print " Yes, " ; N ; " is a prime number."
120 goto 300
210 run outrun
220 print " No, " ; N ; " is not a prime number."
300 end ;
\S
test
Input an integer number N : ? 600
No, 600 is not a prime number. ok
test
Input an integer number N : ? 1999
Yes, 1999 is a prime number. ok
\ prime3.f
6 integers N I J L C D
: outrun 2drop ;
: test
PAGE
BASIC
10 print " Input start and stop numbers C and D = "
20 inputi C , D
30 for J = C to D
40 let N = J
50 if ( N mod 2 = 0 ) OR ( N mod 3 = 0 ) then 220
60 let L = sqrt ( N ) + 1
70 for I = 6 to L step 6
80 if ( N mod ( I - 1 ) = 0 ) OR ( N mod ( I + 1 ) = 0 ) then 210
90 next I
110 print N
120 goto 220
210 run outrun
220 next J
300 end ;
TEST
\ prime4.f
6 integers N I J L C D
: outrun 2drop ;
: test
PAGE
BASIC
10 print " Input start and stop numbers C and D = "
20 inputi C , D
30 for J = C to D
40 let N = J
50 if N mod 2 = 0 then 220
60 if N mod 3 = 0 then 220
70 let L = sqrt ( N ) + 1
80 for I = 6 to L step 6
90 if N mod ( I - 1 ) = 0 then 210
100 if N mod ( I + 1 ) = 0 then 210
110 next I
120 print N
130 goto 220
210 run outrun
220 next J
300 end ;
TEST
\S
testing range must be <= 2147483647 ( get it from -1 1 rshift . )
Input start and stop numbers C and D = ? 1999999000 2000000000
1999999003
1999999013
1999999049
1999999061
1999999081
1999999087
1999999093
1999999097
1999999117
1999999121
1999999151
1999999171
1999999207
1999999219
1999999271
1999999321
1999999373
1999999423
1999999439
1999999499
1999999553
1999999559
1999999571
1999999609
1999999613
1999999621
1999999643
1999999649
1999999657
1999999747
1999999763
1999999777
1999999811
1999999817
1999999829
1999999853
1999999861
1999999871
1999999873
1999999913
1999999927
1999999943
1999999973
\ prange1.f use \lina64\nfp2\f5102
6 integers N I J L A B
: outrun 2drop ;
: test
BASIC
10 print " Input start and stop numbers A and B = "
20 inputi A , B
30 for J = A to B
40 let N = J
50 if ( N mod 2 = 0 ) OR ( N mod 3 = 0 ) then 220
60 let L = sqrt ( N ) + 1
70 for I = 6 to L step 6
80 if ( N mod ( I - 1 ) = 0 ) OR ( N mod ( I + 1 ) = 0 ) then 210
90 next I
110 print N
120 goto 220
210 run outrun
220 next J
300 end ;
TEST
\S
Input start and stop numbers A and B =
? 1000000000001000 1000000000002000
1000000000001003
1000000000001027
1000000000001063
1000000000001089
1000000000001117
1000000000001209
1000000000001269
1000000000001293
1000000000001347
1000000000001371
1000000000001413
1000000000001491
1000000000001503
1000000000001551
1000000000001617
1000000000001623
1000000000001669
1000000000001741
1000000000001749
1000000000001819
1000000000001839
1000000000001867
1000000000001897 OK
\ prange2.f use \lina64\nfp2\f5102
6 integers N I J L A B
: outrun 2drop ;
: test
BASIC
10 print " Input start and stop numbers A and B = "
20 inputi A , B
30 for J = A to B
40 let N = J
50 if N mod 2 = 0 then 220
60 if N mod 3 = 0 then 220
70 let L = sqrt ( N ) + 1
80 for I = 6 to L step 6
90 if N mod ( I - 1 ) = 0 then 210
100 if N mod ( I + 1 ) = 0 then 210
110 next I
120 print N
130 goto 220
210 run outrun
220 next J
300 end ;
TEST
\S
Input start and stop numbers A and B =
? 1000000000001000 1000000000002000
1000000000001003
1000000000001027
1000000000001063
1000000000001089
1000000000001117
1000000000001209
1000000000001269
1000000000001293
1000000000001347
1000000000001371
1000000000001413
1000000000001491
1000000000001503
1000000000001551
1000000000001617
1000000000001623
1000000000001669
1000000000001741
1000000000001749
1000000000001819
1000000000001839
1000000000001867
1000000000001897 OK
\ prange3.f use \lina64\nfp2\f5102
7 integers N I J L A B C
: outrun 2drop ;
: test
BASIC
10 print " Input start and stop numbers A and B = "
20 inputi A , B
30 LET C = 0
40 for J = A to B
50 let N = J
60 if N mod 2 = 0 then 220
70 if N mod 3 = 0 then 220
80 let L = sqrt ( N ) + 1
90 for I = 6 to L step 6
100 if N mod ( I - 1 ) = 0 then 210
110 if N mod ( I + 1 ) = 0 then 210
120 next I
130 LET C = C + 1
140 print N
150 goto 220
210 run outrun
220 next J
230 PRINT " total = " ; C
300 end ;
TEST
\S
Input start and stop numbers A and B =
? 1000000000001000 1000000000002000
1000000000001003
1000000000001027
1000000000001063
1000000000001089
1000000000001117
1000000000001209
1000000000001269
1000000000001293
1000000000001347
1000000000001371
1000000000001413
1000000000001491
1000000000001503
1000000000001551
1000000000001617
1000000000001623
1000000000001669
1000000000001741
1000000000001749
1000000000001819
沒有留言:
張貼留言