Fibonacci

Fibonacci

Postby Antonio Linares » Sun Aug 22, 2021 1:58 pm

Code: Select all  Expand view  RUN
function Main()

   local n

   for n = 0 to 25
      ? fibonacci( n )
   next          

return nil

function fibonacci( n )

  if n <= 1
     return 1
  endif      

return fibonacci( n - 1 ) + fibonacci( n - 2 )
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42203
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Fibonacci

Postby George » Thu Aug 26, 2021 10:57 am

Code: Select all  Expand view  RUN
#include "FiveWin.ch"

function Main()
   local n, nStart, nDelay, nFibonacci

   set decimal to 8
   nStart := Seconds()
   n = 32
   nFibonacci := C_FIBONACCI( n )
   nDelay := Seconds() - nStart
   ? "n, Fibonacci, delay = ", n, nFibonacci, nDelay

   RETURN nil

//--------------------------------------
// C function
//-------------------------------------
#pragma BEGINDUMP

#include <hbapi.h>
#include <math.h>
long long C_FIBONACCI( int n );

HB_FUNC( C_FIBONACCI )

   {
      int n;
      n = hb_parni(1);
      if ( n == 0 )
      hb_retnll(0);
      else if ( n == 1 )
         hb_retnll(1);
      else
         hb_retnll ( C_FIBONACCI(n-1) + C_FIBONACCI(n-2) );
   }

#pragma ENDDUMP


El compilador MS-Visual Studio 2019 funciona sin generar error.
El problema viene cuando trata de linkear la funcion C_FIBONACCI.

Aqui esta la pantalla completa del proceso:
┌────────────────────────────────────────────────────────────────────────────┐
│ FWH 64 for Harbour 21.06 (VS64bits) Jun. 2021 Harbour development power │▄
│ (c) FiveTech 1993-2021 for Microsoft Windows 9X/NT/200X/ME/XP/Vista/7/8/10 │█
└────────────────────────────────────────────────────────────────────────────┘█
  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.10.3
** Copyright (c) 2021 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x86_x64'
Compiling...
Harbour 3.2.0dev (r2011030937)
Copyright (c) 1999-2020, https://harbour.github.io/
Compiling 'c_fib3.prg' and generating preprocessed output to 'c_fib3.ppo'...
Lines 5038, Functions/Procedures 1
Generating C source output to 'c_fib3.c'... Done.
Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30038.1 for x64
Copyright (C) Microsoft Corporation. All rights reserved.

c_fib3.c
c_fib3.obj : error LNK2019: unresolved external symbol C_FIBONACCI referenced in function HB_FUN_C_FIBONACCI
c_fib3.exe : fatal error LNK1120: 1 unresolved externals
* Linking errors *

¿Alguna sugerencia de como se debe declarar la funcion prototipo de C_FIBONACCI(), en Harbour, antes de llamar la funcion recursiva en C?

Saludos,

George
George
 
Posts: 726
Joined: Tue Oct 18, 2005 6:49 pm

Re: Fibonacci

Postby Enrico Maria Giordano » Thu Aug 26, 2021 2:25 pm

Try this:

Code: Select all  Expand view  RUN
#include "FiveWin.ch"

function Main()
   local n, nStart, nDelay, nFibonacci

   set decimal to 8
   nStart := Seconds()
   n = 32
   nFibonacci := C_FIBONACCI( n )
   nDelay := Seconds() - nStart
   ? "n, Fibonacci, delay = ", n, nFibonacci, nDelay

   RETURN nil

//--------------------------------------
// C function
//-------------------------------------
#pragma BEGINDUMP

#include <hbapi.h>
#include <math.h>
long long C_Fibonacci( int n )
{
      if ( n == 0 )
      return (0);
      else if ( n == 1 )
         return (1);
      else
         return ( C_Fibonacci(n-1) + C_Fibonacci(n-2) );
}

HB_FUNC( C_FIBONACCI )

   {
      int n;
      n = hb_parni(1);
      hb_retnll ( C_Fibonacci(n) );
   }

#pragma ENDDUMP


EMG
User avatar
Enrico Maria Giordano
 
Posts: 8725
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Fibonacci

Postby George » Thu Aug 26, 2021 3:05 pm

Excellent! Thanks Enrico for you help.

Regards,

George
George
 
Posts: 726
Joined: Tue Oct 18, 2005 6:49 pm


Return to Utilities / Utilidades

Who is online

Users browsing this forum: No registered users and 3 guests