BASIC User Group > Code Challenges

fibonacci(4784969)

<< < (3/22) > >>

jalih:

--- Quote from: AIR on April 20, 2019, 10:04:47 am ---Can you code this so that it outputs the actual Fibonacci number for 1000000?

--- End quote ---

Sure, here is a version that takes numbers as command-line parameter to calculate fibonacci number. Included is source + binaries for most systems.

jalih:
Here is a REXX version that might be easier to read than my 8th version:


--- Code: ---numeric digits 1000000
say fibo(4784969)
exit


fibo:
  parse arg n

  a = 0
  b = 1
  do i = 1 to length(strip(d2b(n), 'L','0'))
    d = a * (b * 2 - a)
    e = a * a + b * b
    a = d
    b = e
    if substr(strip(d2b(n), 'L','0'), i, 1) = '1' then
      do
        c = a + b
        a = b
        b = c
      end
  end
  return a


d2b: return x2b(d2x(arg(1)))

--- End code ---

John:
AIR,

Attached is the output to a file of the fibonacci(4784969) challenge. (from the following C code - gmp assisted)

I tried to add this as a function to the tools extension module. I have gmp-dev installed. The desired result of the function would be a SB string.


--- Code: C ---#include <stdio.h>#include <gmp.h>#include <stdlib.h> void fibo(int n) {    mpz_t res;    mpz_init(res);     mpz_fib_ui(res, n);     gmp_printf( "%Zd\n", res );} int main(int argc, char* argv[] ){    int n = 4784969;   // The first Fibonacci number with a million digits     if (argc >= 2) {        n = atol(argv[1]);    }     fibo(n);    return (0);} 
Can you give me a hand with this? Maybe creating a new gmp extention module is a better way to go.

AIR:
The challenge is getting the output into a string that SB can understand.

This should give you an idea:


--- Code: C ---void fibo(int n) {    char buf[n+1];    memset(buf,0,1);    mpz_t res;    mpz_init(res);     mpz_fib_ui(res, n);     gmp_snprintf( buf,sizeof(buf),"%Zd\n", res );    printf("%s\n",buf);}
So now the output is in the 'buf' char array (string).  Work on returning that and you should be okay.

AIR.

John:
This is what I have so far but doesn't return anything.


--- Code: C ---/* GMP Extension ModuleUXLIBS: -lgmp*/ #include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <gmp.h>#include "../../basext.h" /************************** Extension Module Functions**************************/ typedef struct _ModuleObject {  void *HandleArray;}ModuleObject,*pModuleObject;  besVERSION_NEGOTIATE  return (int)INTERFACE_VERSION;besEND  besSUB_START  pModuleObject p;   besMODULEPOINTER = besALLOC(sizeof(ModuleObject));  if( besMODULEPOINTER == NULL )return 0;   p = (pModuleObject)besMODULEPOINTER;  return 0;besEND  besSUB_FINISH  pModuleObject p;   p = (pModuleObject)besMODULEPOINTER;  if( p == NULL )return 0;  return 0;besEND  /************* GMP Functions*************/  besFUNCTION(fibo)  mpz_t res;  mpz_init(res);  int fval;   besARGUMENTS("i")    &fval  besARGEND   mpz_fib_ui(res, fval);   besRETURN_STRING(res);besEND 

--- Code: Script BASIC ---declare sub fibo alias "fibo" lib "gmp"PRINT fibo(24),"\n" 
besRETURN_STRING(&res);     Doesn't work either.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version