runtime error lazarus / 61-Beginners ---------------------------------------------------------------------- rabbit_dance · 2015-10-03 04:44 Code: [Select]PROGRAM FIB; VAR I,J,K,Y,X:INTEGER; TYPE LINK_PTR=^LINK_NODE;         LINK_NODE=RECORD         NUMBER:INTEGER;         NEXT:LINK_PTR;         END; VAR NODE,CURRENT:LINK_PTR; PROCEDURE RESTORE(L:INTEGER); BEGIN         NODE:=ALLOCMEM(SIZEOF(LINK_NODE));         NODE^.NUMBER:=L;         NODE^.NEXT:=CURRENT;         CURRENT:=NODE; END; PROCEDURE TRAVERSE(M:INTEGER;VAR L:INTEGER); VAR P:INTEGER;VAR Q,R:LINK_PTR; BEGIN         Q:=NODE;         FOR P:=1 TO K-M+1 DO         BEGIN                 L:=Q^.NUMBER;                 R:=Q^.NEXT;                 Q:=R;         END; END; FUNCTION FIB(N:INTEGER):INTEGER; VAR L:INTEGER; BEGIN         IF N=1 THEN L:=I;         IF N=2 THEN L:=J;         IF K>N THEN BEGIN                 TRAVERSE(N,L);                 FIB:=L;                 EXIT();         END;         L:=FIB(N-1)+FIB(N-2);         K:=K+1;         RESTORE(L);         FIB:=L; END; BEGIN         K:=0;         CURRENT:=NIL;         READLN(Y,I,J);         X:=FIB(Y);         WRITELN(X); END. running fib input 5 1 1 will seeCode: [Select]Runtime error 216 at $00401484   $00401484   $00401503   $00401538   $00401538   $00401538   $00401538   $00401538   $00401538   $00401696   $004070A1 ---------------------------------------------------------------------- rabbit_dance · 2015-10-03 05:22 Code: [Select]PROGRAM FIB; VAR I,J,K,Y,X:INTEGER; TYPE LINK_PTR=^LINK_NODE;         LINK_NODE=RECORD         NUMBER:INTEGER;         NEXT:LINK_PTR;         END; VAR NODE,CURRENT:LINK_PTR; FUNCTION MAX(K,N:INTEGER):INTEGER; BEGIN         IF K>N THEN MAX:=K ELSE MAX:=N; END; PROCEDURE RESTORE(L:INTEGER); BEGIN         NODE:=ALLOCMEM(SIZEOF(LINK_NODE));         NODE^.NUMBER:=L;         NODE^.NEXT:=CURRENT;         CURRENT:=NODE; END; PROCEDURE TRAVERSE(M:INTEGER;VAR L:INTEGER); VAR P:INTEGER;VAR Q,R:LINK_PTR; BEGIN         Q:=NODE;         FOR P:=1 TO K-M+1 DO         BEGIN                 L:=Q^.NUMBER;                 R:=Q^.NEXT;                 Q:=R;         END; END; FUNCTION FIB(N:INTEGER):INTEGER; VAR L:INTEGER; BEGIN         IF N=1 THEN L:=I;         IF N=2 THEN L:=J;         IF K>N THEN BEGIN                 TRAVERSE(N,L);                 FIB:=L;                 EXIT();         END;         L:=FIB(N-1)+FIB(N-2);         K:=MAX(K,N);         RESTORE(L);         FIB:=L; END; BEGIN         CURRENT:=NIL;         READLN(Y,I,J);         X:=FIB(Y);         WRITELN(X); END. THE ERRORS STILL REMAIN THERE. ---------------------------------------------------------------------- derek.john.evans · 2015-10-03 06:04 Why do you keep traversing link lists using indexes? http://physinfo.ulb.ac.be/cit_courseware/pascal/pas071.htm https://www.youtube.com/watch?v=oiW79L8VYXk http://kon-phum.com/tutors/pascal/programming_pascal_learn14.html http://rosettacode.org/wiki/Singly-linked_list/Traversal#Delphi You need to rethink that entire strategy if you want to resolve these issues. ---------------------------------------------------------------------- derek.john.evans · 2015-10-03 06:29 The strange thing I find about this question is, fibonacci numbers (historically) has been used to teach recursion. Not linked lists, so why are you using linked lists? http://progopedia.com/example/fibonacci/58/ http://www.scriptol.com/programming/fibonacci.php#pascal https://www.youtube.com/watch?v=9fMQQuK0GBI Maybe computer science education is different these days. O, my. ---------------------------------------------------------------------- rabbit_dance · 2015-10-03 11:29 Quote from: Geepster on October 03, 2015, 08:29:45 amThe strange thing I find about this question is, fibonacci numbers (historically) has been used to teach recursion. Not linked lists, so why are you using linked lists? http://progopedia.com/example/fibonacci/58/ http://www.scriptol.com/programming/fibonacci.php#pascal https://www.youtube.com/watch?v=9fMQQuK0GBI Maybe computer science education is different these days. O, my. because i want to use linked_list to record and read intermediate result to speed up the calculation. ---------------------------------------------------------------------- rabbit_dance · 2015-10-03 11:34 Quote from: Geepster on October 03, 2015, 08:29:45 amThe strange thing I find about this question is, fibonacci numbers (historically) has been used to teach recursion. Not linked lists, so why are you using linked lists? http://progopedia.com/example/fibonacci/58/ http://www.scriptol.com/programming/fibonacci.php#pascal https://www.youtube.com/watch?v=9fMQQuK0GBI Maybe computer science education is different these days. O, my. no one teach me. l teach myself. ---------------------------------------------------------------------- derek.john.evans · 2015-10-03 12:50 Quote from: rabbit_dance on October 03, 2015, 01:34:22 pmno one teach me. l teach myself. A fair enough and commendable effort. I'm mostly self taught since I was 15. I recommend you still follow a decent programming book. Quote from: rabbit_dance on October 03, 2015, 01:29:18 pmbecause i want to use linked_list to record and read intermediate result to speed up the calculation. Why do you think a linked list will be faster?  Have you written both a iterative and recursive fibonacci function? If not, then how do you know you can get faster results using linked lists? http://www.codeproject.com/Articles/21194/Iterative-vs-Recursive- Approaches Unless you are trying to code something other than a fibonacci function, I dont see how a linked list will help speed. But, if you do require a linked list, then, iterating using index counts is not the prefered method. You should iterate until you reach another pointer. nil if you want to iterate till the end, or a node which you _know_ comes after the starting point. Anyway. Take the advice or leave it. Soon you will get sick of seeing runtime errors. ---------------------------------------------------------------------- rabbit_dance · 2015-10-03 13:26 Why do you think a linked list will be faster? because i want to use linked_list to record and read the results in the intermediate stage of the calculating process to speed up the calculation. ---------------------------------------------------------------------- derek.john.evans · 2015-10-03 13:44 Quote from: rabbit_dance on October 03, 2015, 03:26:20 pmWhy do you think a linked list will be faster? because i want to use linked_list to record and read the results in the intermediate stage of the calculating process to speed up the calculation. Thats just a weird response. Anyway, some very smart people have studied this problem and documented their findings. http://www.nayuki.io/page/fast-fibonacci-algorithms None of the fastest algorithms use linked lists. But, it looks like you have an idea, so good luck with your research. ---------------------------------------------------------------------- rabbit_dance · 2015-10-03 14:24 Code: [Select]PROGRAM FIB; VAR H,I,J,K:INTEGER;VAR N:INTEGER; FUNCTION FIB(N:INTEGER):INTEGER; BEGIN         IF NK THEN FIB:=FIB(N-1)+FIB(N-2) ELSE BEGIN         IF N=K THEN BEGIN                 FIB:=J;                 EXIT;                 END;         IF N=K-1 THEN BEGIN                 FIB:=I;                 EXIT;                 END;                 END;         J:=I;I:=FIB;         K:=N; END; BEGIN         K:=2;         READLN(H,I,J);         N:=FIB(H);         WRITELN(N); END. ---------------------------------------------------------------------- rabbit_dance · 2015-10-03 15:36 Code: [Select]PROGRAM FIB; VAR H,I,J,K:INTEGER;VAR N:INTEGER; FUNCTION FIB(N:INTEGER):INTEGER; BEGIN         IF N>K THEN BEGIN                 FIB:=FIB(N-1)+FIB(N-2);                 K:=N;                 I:=J;J:=FIB;                 END;         IF N=K THEN BEGIN                 FIB:=J;                 END;         IF N=K-1 THEN BEGIN                 FIB:=I;                 END; END; BEGIN         K:=2;         READLN(H,I,J);         N:=FIB(H);         WRITELN(N); END. ---------------------------------------------------------------------- Bart · 2015-10-03 16:57 You want speed, you get it. Note that using the biggest unsigned integer type in FreePascal (UInt64), you can only get to the 93rd Fibonacci number! Code: [Select]const   FibNr: Array[FibRange] of UInt64 = (     {1} 1, {2} 1, {3} 2, {4} 3, {5} 5,     {6} 8, {7} 13, {8} 21, {9} 34, {10} 55,     {11} 89, {12} 144, {13} 233, {14} 377, {15} 610,     {16} 987, {17} 1597, {18} 2584, {19} 4181, {20} 6765,     {21} 10946, {22} 17711, {23} 28657, {24} 46368, {25} 75025,     {26} 121393, {27} 196418, {28} 317811, {29} 514229, {30} 832040,     {31} 1346269, {32} 2178309, {33} 3524578, {34} 5702887, {35} 9227465,     {36} 14930352, {37} 24157817, {38} 39088169, {39} 63245986, {40} 102334155,     {41} 165580141, {42} 267914296, {43} 433494437, {44} 701408733, {45} 1134903170,     {46} 1836311903, {47} 2971215073, {48} 4807526976, {49} 7778742049, {50} 12586269025,     {51} 20365011074, {52} 32951280099, {53} 53316291173, {54} 86267571272, {55} 139583862445,     {56} 225851433717, {57} 365435296162, {58} 591286729879, {59} 956722026041, {60} 1548008755920,     {61} 2504730781961, {62} 4052739537881, {63} 6557470319842, {64} 10610209857723, {65} 17167680177565,     {66} 27777890035288, {67} 44945570212853, {68} 72723460248141, {69} 117669030460994, {70} 190392490709135,     {71} 308061521170129, {72} 498454011879264, {73} 806515533049393, {74} 1304969544928657, {75} 2111485077978050,     {76} 3416454622906707, {77} 5527939700884757, {78} 8944394323791464, {79} 14472334024676221, {80} 23416728348467685,     {81} 37889062373143906, {82} 61305790721611591, {83} 99194853094755497, {84} 160500643816367088, {85} 259695496911122585,     {86} 420196140727489673, {87} 679891637638612258, {88} 1100087778366101931, {89} 1779979416004714189, {90} 2880067194370816120,     {91} 4660046610375530309, {92} 7540113804746346429, {93} 12200160415121876738); function FastFib(N: FibRange): UInt64; inline; begin   Result := FibNr[N]; end; Here's another (iterative) approach, which can be extended to a bigger type than UIn64 if we get that (or if you use a special big integer library): It treats the Fibonacci series as a special case of the Lucas series. Code: [Select]{ Fibonacci series:   Fib(1) = 1   Fib(2) = 1   Fib(N, N>2) = Fib(N-2) + Fib(N-1)   The Fiboacci series is a special case of the Lucasseries with starting values 1,1 } function Fib(N: UInt64): UInt64; begin   Result := LucasGen(1,1,N); end; { The Lucas series:   Lucas(1) = 2   Lucas(2) = 1   Lucas(N, N>2) = LucasN(N-2) + LucasN(N-1)   The first 36 Lucas numbers are:   [2,1,3,4,7,11,18,29,47,76,123,199,322,521,843,    1364,2207,3571,5778,9349,15127,24476,39603,64079,    103682,167761,271443,439204,710647,1149851,    1860498,3010349,4870847,7881196,12752043,20633239,    33385282]} function Lucas(N: UInt64): UInt64; begin   Result := LucasGen(2,1,N); end; { A more general form of the Lucas series, where the 2 starting values can be set using parameters } function LucasGen(L1, L2, N: UInt64): UInt64; var   i, LucMin1, LucMin2: UInt64; begin   if (N = 0) then     Raise ERangeError.Create('Lucas function is undefined for 0.');   //writeln('FibN: N = ',N);   if (N = 1) then     Exit(L1)   else if (N = 2) then     Exit(L2)   else   begin     LucMin1 := L2;     LucMin2 := L1;     i := 2;     while (i <> N) do     begin       Inc(i);       Result := LucMin2 + LucMin1;       //writeln('LucasN: i = ',i,' LucMin2 = ',LucMin2:6,' LucMin1 = ',LucMin1:6,' Result = ',Result:6);       LucMin2 := LucMin1;       LucMin1 := Result;     end;   end; end; Note that my big integer library can easily calculate Fib(1000): 4346655768693745643568852767504062580256466051737178040248172908953655 5417949051890403879840079255169295922593080322634775209689623239873322 471161642996440906533187938298969649928516003704476137795166849228875 Bart ---------------------------------------------------------------------- rabbit_dance · 2015-10-04 02:51 Code: [Select]PROGRAM FIB; VAR H,I,J,K:INTEGER;VAR N:INTEGER; FUNCTION FIB(N:INTEGER):INTEGER; BEGIN         IF N>K THEN BEGIN                 FIB:=FIB(N-1)+FIB(N-2);                 K:=N;                 I:=J;J:=FIB;                 END ELSE BEGIN                         IF N=K THEN FIB:=J;                         IF N=K-1 THEN FIB:=I;                         END;         WRITE(FIB,' '); END; BEGIN         K:=2;         READLN(H,I,J);         N:=FIB(H);         WRITELN(N); END. ---------------------------------------------------------------------- rabbit_dance · 2015-10-04 04:46 Code: [Select]PROGRAM FIB; VAR H,I,J,K:INTEGER;VAR N:INTEGER; FUNCTION FIB(N:INTEGER):INTEGER; BEGIN         IF N>K THEN BEGIN                 FIB:=FIB(N-1)+FIB(N-2);                 K:=N;                 I:=J;J:=FIB;                 END ELSE BEGIN                         IF N=K THEN FIB:=J;                         IF N=K-1 THEN FIB:=I;                         END;         WRITE(K,'::',N,'::',FIB,' '); END; BEGIN         K:=2;         READLN(H,I,J);         N:=FIB(H);         WRITELN(N); END. how to make the output colorful? ---------------------------------------------------------------------- eny · 2015-10-04 08:45 Quote from: rabbit_dance on October 04, 2015, 06:46:53 amhow to make the output colorful? Easy ---------------------------------------------------------------------- rabbit_dance · 2015-10-04 08:53 Quote from: eny on October 04, 2015, 10:45:07 amQuote from: rabbit_dance on October 04, 2015, 06:46:53 amhow to make the output colorful? Easy so, how? ---------------------------------------------------------------------- rabbit_dance · 2015-10-04 08:55 Quote from: eny on October 04, 2015, 10:45:07 amQuote from: rabbit_dance on October 04, 2015, 06:46:53 amhow to make the output colorful? Easy you have misread my question. i mean the terminal. ---------------------------------------------------------------------- derek.john.evans · 2015-10-04 08:56 Use this: http://freepascal.org/docs-html/rtl/crt/index-5.html Please don't ask for an explanation of how to use TextColor(). jeeez http://freepascal.org/docs-html/rtl/crt/textcolor.html When are you going to upgrade your dev system? ---------------------------------------------------------------------- rabbit_dance · 2015-10-04 08:57 thanks ---------------------------------------------------------------------- rabbit_dance · 2016-05-15 10:21 PROGRAM FIB; VAR S,N,A,A1,A2:INTEGER; PROCEDURE F(A1,A2:INTEGER;VAR S,A,N:INTEGER); VAR S1,S2,A3,A4,M:INTEGER; BEGIN    S1:=A1;M:=0;A3:=A2;    REPEAT       S2:=S1+A3;A4:=S1+A2;       S1:=S2;A3:=A4;       M:=M+1;    UNTIL(M>=N);    S:=S1;A:=A3; END; BEGIN    READLN(A1,A2,N);    F(A1,A2,S,A,N);    WRITELN(S,' ',A); END. ---------------------------------------------------------------------- rabbit_dance · 2016-05-15 10:50 PROGRAM FIB; VAR S,N,A,A1,A2:INTEGER; PROCEDURE F(A1,A2:INTEGER;VAR S,A,N:INTEGER); VAR S1,S2,A3,A4,M:INTEGER; BEGIN    S1:=A1;M:=0;A3:=A2;    REPEAT       S2:=S1+A3;A4:=S1+A2;       S1:=S2;A3:=A4;       M:=M+1;    UNTIL(M>=N);    S:=S1+A3;A:=A3; END; BEGIN    READLN(A1,A2,N);    F(A1,A2,S,A,N);    WRITELN(S,' ',A); END. ---------------------------------------------------------------------- Thaddy · 2016-05-15 10:52 Did you really write this through a VT100 terminal? ---------------------------------------------------------------------- rabbit_dance · 2016-05-16 03:04 Quote from: Thaddy on May 15, 2016, 12:52:11 pmDid you really write this through a VT100 terminal? not yet