Recent Posts

Pages: 1 ... 8 9 [10]
Code Challenges / Re: fibonacci(4784969)
« Last post by AIR on April 20, 2019, 02:18:49 PM »
The challenge is getting the output into a string that SB can understand.

This should give you an idea:

Code: C
  1. void fibo(int n) {
  2.     char buf[n+1];
  3.     memset(buf,0,1);
  4.     mpz_t res;
  5.     mpz_init(res);
  7.     mpz_fib_ui(res, n);
  9.     gmp_snprintf( buf,sizeof(buf),"%Zd\n", res );
  10.     printf("%s\n",buf);
  11. }

So now the output is in the 'buf' char array (string).  Work on returning that and you should be okay.

Code Challenges / Re: fibonacci(4784969)
« Last post by John on April 20, 2019, 12:58:01 PM »

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
  1. #include <stdio.h>
  2. #include <gmp.h>
  3. #include <stdlib.h>
  5. void fibo(int n) {
  6.     mpz_t res;
  7.     mpz_init(res);
  9.     mpz_fib_ui(res, n);
  11.     gmp_printf( "%Zd\n", res );
  12. }
  14. int main(int argc, char* argv[] )
  15. {
  16.     int n = 4784969;   // The first Fibonacci number with a million digits
  18.     if (argc >= 2) {
  19.         n = atol(argv[1]);
  20.     }
  22.     fibo(n);
  23.     return (0);
  24. }

Can you give me a hand with this? Maybe creating a new gmp extention module is a better way to go.

Code Challenges / Re: fibonacci(4784969)
« Last post by jalih on April 20, 2019, 11:04:43 AM »
Here is a REXX version that might be easier to read than my 8th version:

Code: [Select]
numeric digits 1000000
say fibo(4784969)

  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
        c = a + b
        a = b
        b = c
  return a

d2b: return x2b(d2x(arg(1)))
Code Challenges / Re: fibonacci(4784969)
« Last post by jalih on April 20, 2019, 10:55:55 AM »
Can you code this so that it outputs the actual Fibonacci number for 1000000?

Sure, here is a version that takes numbers as command-line parameter to calculate fibonacci number. Included is source + binaries for most systems.
Code Challenges / Re: fibonacci(4784969)
« Last post by AIR on April 20, 2019, 10:04:47 AM »

Can you code this so that it outputs the actual Fibonacci number for 1000000?
Scripting Languages / Re: SB π
« Last post by AIR on April 19, 2019, 09:36:57 PM »
You made it much harder than it needed to be.

you could have done:

git clone sb-dev-cleanup

No creation of the folder required, and no git init required.

Then git checkout cleanup

Make your changes/additions

git add:  NEVER do '.'  That can add a bunch of additional files that may not be excluded via .gitignore.  Also, don't use the "-f" switch. That will add files that normally are excluded via the .gitignore file.

git commit -m:  ALWAYS be descriptive.  In other worlds, describe WHY you're committing, not just what.  You'll thank me in a year when you try to remember why a particular commit was made.

Also, if you modify several files, add and commit each separately with a full description.  If you ever need to revert a commit, you won't inadvertently undo other changes that are good.

In the end, all you really needed to do was run git checkout cleanup in your sb-dev folder and it would have switched that to the cleanup branch where all development has been happening for the last couple of months.

Scripting Languages / Re: SB π
« Last post by John on April 19, 2019, 06:51:18 PM »
I started over with a clean directory.

jrs@jrs-laptop:~/sb/examples/test$ cd ~/repo/sb-dev-cleanup
jrs@jrs-laptop:~/repo/sb-dev-cleanup$ git init
Initialized empty Git repository in /home/jrs/repo/sb-dev-cleanup/.git/
jrs@jrs-laptop:~/repo/sb-dev-cleanup$ git clone
Cloning into 'sb-dev'...
remote: Enumerating objects: 4127, done.
remote: Counting objects: 100% (4127/4127), done.
remote: Compressing objects: 100% (2262/2262), done.
remote: Total 4127 (delta 1773), reused 4091 (delta 1746)
Receiving objects: 100% (4127/4127), 10.28 MiB | 2.99 MiB/s, done.
Resolving deltas: 100% (1773/1773), done.
jrs@jrs-laptop:~/repo/sb-dev-cleanup$ git checkout cleanup
error: pathspec 'cleanup' did not match any file(s) known to git
jjrs@jrs-laptop:~/repo/sb-dev-cleanup$ git branch

It seems to create a sb-dev sub-directory in my new sb-dev-cleanup repo. I changed directory to sb-dev and tried the last two commands again.

jrs@jrs-laptop:~/repo/sb-dev-cleanup$ ls -l
total 12
drwxr-xr-x 16 jrs jrs 12288 Apr 19 18:48 sb-dev
jrs@jrs-laptop:~/repo/sb-dev-cleanup$ cd sb-dev
jrs@jrs-laptop:~/repo/sb-dev-cleanup/sb-dev$ git checkout cleanup
Branch 'cleanup' set up to track remote branch 'cleanup' from 'origin'.
Switched to a new branch 'cleanup'
jrs@jrs-laptop:~/repo/sb-dev-cleanup/sb-dev$ git branch
* cleanup
jrs@jrs-laptop:~/repo/sb-dev-cleanup/sb-dev$ git pull
Already up to date.
jrs@jrs-laptop:~/repo/sb-dev-cleanup/sb-dev$ git add .
jrs@jrs-laptop:~/repo/sb-dev-cleanup/sb-dev$ git commit -m "SDBG - remote debugger"
On branch cleanup
Your branch is up to date with 'origin/cleanup'.

nothing to commit, working tree clean
jrs@jrs-laptop:~/repo/sb-dev-cleanup/sb-dev$ git add extensions/sdbg/
The following paths are ignored by one of your .gitignore files:
Use -f if you really want to add them.
jrs@jrs-laptop:~/repo/sb-dev-cleanup/sb-dev$ ls -l extensions/sdbg/
-rw-rw-r-- 1 jrs jrs 1612 Apr 18 17:49 extensions/sdbg/
jrs@jrs-laptop:~/repo/sb-dev-cleanup/sb-dev$ git add -f extensions/sdbg/
jrs@jrs-laptop:~/repo/sb-dev-cleanup/sb-dev$ git commit -m "SDBG - remote debugger"
[cleanup c35b2c9] SDBG - remote debugger
 1 file changed, 65 insertions(+)
 create mode 100644 extensions/sdbg/
jrs@jrs-laptop:~/repo/sb-dev-cleanup/sb-dev$ git push
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 4 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 1.14 KiB | 1.14 MiB/s, done.
Total 5 (delta 3), reused 0 (delta 0)
remote: To create a merge request for cleanup, visit:
   7160bfd..c35b2c9  cleanup -> cleanup

My push of showed up in the cleanup branch as expected. Sure was a long road getting there.  :-\

Thanks for the help with this!

Code Challenges / Re: fibonacci(4784969)
« Last post by John on April 19, 2019, 06:00:43 PM »
The best ScriptBasic can do on 64 bit is Fibonacci(92) without overflowing  MAXINT.

Time:  0.009 seconds.
Code Challenges / Re: fibonacci(4784969)
« Last post by AIR on April 19, 2019, 04:52:01 PM »
Another way of doing it in Python.  I didn't write this, and don't fully understand all the calculations (I think it uses a MATRIX), but it's the fastest implementation I found.

Code: Python
  1. #!/usr/bin/env python
  3. def fib(n):
  4.     v1, v2, v3 = 1, 1, 0  
  5.     for rec in bin(n)[3:]:
  6.         calc = v2*v2
  7.         v1, v2, v3 = v1*v1+calc, (v1+v3)*v2, calc+v3*v3
  8.         if rec=='1':    v1, v2, v3 = v1+v2, v1, v2
  9.     return v2
  11. res = fib(4784969)

On my Macbook Pro, 2.5Ghz 4 Core i7:
$ time ./

real   0m1.100s
user   0m1.080s
sys   0m0.016s

On my RasPi:
$ time ./

real   0m25.947s
user   0m25.894s
sys   0m0.050s

Scripting Languages / Re: SB π
« Last post by AIR on April 19, 2019, 03:39:06 PM »
All you do is the following (first step is assuming you haven't cloned the repository previously):

Pages: 1 ... 8 9 [10]