Raspberry BASIC

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - John Spikowski

Pages: [1] 2 3 ... 6
1
Nim / Re: Nim Fibo
« on: Today at 05:34:13 AM »
A Fibo Nim example for our RPI  forum friends.

Code: Text
  1. import math
  2.  
  3. proc Fibonacci(n: int): int64 =
  4.   var fn = float64(n)
  5.   var p: float64 = (1.0 + sqrt(5.0)) / 2.0
  6.   var q: float64 = 1.0 / p
  7.   return int64((pow(p, fn) + pow(q, fn)) / sqrt(5.0))
  8.  
  9. echo Fibonacci(78)
  10.  


pi@RPi4B:~/nim-dev/examples $ nim c -d:release fibo78.nim
Hint: used config file '/etc/nim/nim.cfg' [Conf]
Hint: system [Processing]
Hint: fibo78 [Processing]
Hint: math [Processing]
Hint: bitops [Processing]
CC: fibo78
CC: stdlib_system
CC: stdlib_math
CC: stdlib_bitops
Hint:  [Link]
Hint: operation successful (13698 lines compiled; 4.051 sec total; 10.754MiB peakmem; Release Build) [SuccessX]
pi@RPi4B:~/nim-dev/examples $ timex ./fibo78
8944394323791488
0.00user 0.00system 0:00.00elapsed 83%CPU (0avgtext+0avgdata 1292maxresident)k
0inputs+0outputs (0major+98minor)pagefaults 0swaps
pi@RPi4B:~/nim-dev/examples $ time ./fibo78
8944394323791488

real   0m0.008s
user   0m0.008s
sys   0m0.001s
pi@RPi4B:~/nim-dev/examples $


There is a GMP extension for Nim that I might give the 1mil fibo a shot with.

Nim GMP

2
Nim / Nim IUP
« on: Today at 04:25:35 AM »
I was able to get the IUP distribution I built for the RPi and Nim to  work together. A "Hello Nim" of sorts.

Code: Text
  1. import iup
  2.  
  3. proc btn_click(ih:PIhandle):cint {.cdecl.}=
  4.     iup.message("Popup", "You clicked the button!")
  5.  
  6. discard iup.open(nil, nil)
  7.  
  8. var btn = iup.button("Click me!", nil)
  9. discard iup.setCallback(btn, "ACTION", cast[ICallback](btn_click))
  10.  
  11. var dlg = iup.dialog(iup.vbox(btn, nil))
  12. iup.setAttribute(dlg, "TITLE", "iupTabs")
  13.  
  14. discard iup.showXY(dlg, IUP_CENTER, IUP_CENTER)
  15. discard iup.mainLoop()
  16.  
  17. iup.close()
  18.  


pi@RPi4B:~/pui/iup-master/examples $ nim c -d:release callbacks.nim
Hint: used config file '/etc/nim/nim.cfg' [Conf]
Hint: system [Processing]
Hint: callbacks [Processing]
Hint: iup [Processing]
CC: iup_callbacks
CC: stdlib_system
CC: stdlib_iup
Hint:  [Link]
Hint: operation successful (13356 lines compiled; 4.586 sec total; 10.758MiB peakmem; Release Build) [SuccessX]
pi@RPi4B:~/pui/iup-master/examples $ ls -l
total 72
-rwxr-xr-x 1 pi pi 59180 Dec  6 20:17 callbacks
-rw-r--r-- 1 pi pi   413 Nov  4 02:39 callbacks.nim
-rw-r--r-- 1 pi pi   576 Nov  4 02:39 menu.nim
-rw-r--r-- 1 pi pi  1023 Nov  4 02:39 tabs.nim
pi@RPi4B:~/pui/iup-master/examples $ ./callbacks


3
Programming Challenges / Re: Just REM
« on: Today at 03:06:32 AM »
I was curious if I expanded out the expression to concatenate strings in ScriptBasic would it help?  NOT and good to know for future use.

Example

Was:  t &= s

Changed  To:  t = t & s

&= seems to be ScriptBasic's StringBuilder function.

Code: Script BASIC
  1. ' ScriptBasic - 1mil.sb
  2.  
  3. s = ""
  4. t = ""
  5. SPLITA STRING(1000001,"0") BY "" TO a
  6.  
  7. FOR x = 1 TO 1000000
  8.   s = s & CHR(((x - 1) % 26) + 65)
  9.   a[x] = x
  10.   IF LEN(s) = 26 THEN
  11.     t = t & s
  12.     s = ""
  13.   END IF
  14. NEXT
  15. t = STRREVERSE(t)
  16. PRINT "t LEN: ",LEN(t),"\n"
  17. PRINT "Front: ",LEFT(t, 26),"\n"
  18. PRINT "Back:  ",RIGHT(t, 26),"\n"
  19. PRINT "UBVal: ",a[1000000],"\n"
  20.  


pi@RPi4B:~/sbrt/examples $ timex scriba  1mil3.sb
t LEN: 999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal: 1000000
127.51user 35.17system 2:42.93elapsed 99%CPU (0avgtext+0avgdata 171488maxresident)k
0inputs+0outputs (0major+4122875minor)pagefaults 0swaps
pi@RPi4B:~/sbrt/examples $

 

4
General Discussion / Re: RPI Forum
« on: December 06, 2019, 06:31:52 AM »
Quote from: Heater@RPI
When silly discussions have gone on long enough jamesh locks the thread. Everything goes dark.

Sooner or later the realization will set in that the RPi forum is focused towards support / Q&A topics. A bunch of old programmers shooting the shit or sharing a trick or two isn't part of the program. Any of the old farts that feel the moderator kids are being obnoxious, feel free to join us here,

5
Translators / Universal Language Translator
« on: December 06, 2019, 05:09:31 AM »
Hi Brian,

Thanks for joining us on the forum.

With your language translation skills have you thought of fostering a universal programming language translator project? Something like Google translate but for programming languages.

As you can see from the string / array language challenge the syntax isn't that different between them. I would like to hear your thoughts on the challenges that might be ahead.

6
Programming Challenges / Re: Just REM
« on: December 04, 2019, 08:40:38 PM »
Here is the String / Array language challenge results table.


7
Programming Challenges / Re: Just REM
« on: December 04, 2019, 06:23:02 AM »
Java

Code: Java
  1. // Java - 1mil3.java
  2.  
  3. class onemil3
  4.   {
  5.   public static void main(String args[])
  6.   {
  7.   String s = "";
  8.   String t = "";
  9.   int c = 0;
  10.   int[] a = new int[1000001];
  11.  
  12.   for (int x = 1; x <= 1000000; x++) {
  13.     c = ((x - 1) % 26) + 65;          
  14.     s = s + (char) c;
  15.     a[x] = x;
  16.     if(s.length() == 26) {
  17.       t = t + s;
  18.       s = "";
  19.     }
  20.   }
  21.  
  22.   StringBuilder r = new StringBuilder();
  23.   r.append(t);
  24.   r = r.reverse();
  25.  
  26.   System.out.println("r LEN: " + r.length());
  27.   System.out.println("Front: " + r.substring(0, 26));
  28.   System.out.println("Back:  " + r.substring(r.length() - 26));  
  29.   System.out.println("UBVal: " + a[1000000]);
  30.   }
  31. }
  32.  


pi@RPi4B:~/java-dev/examples $ /usr/bin/time java onemil3
r LEN: 999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal: 1000000
43.79user 2.03system 0:34.78elapsed 131%CPU (0avgtext+0avgdata 144432maxresident)k
0inputs+96outputs (0major+33910minor)pagefaults 0swaps
pi@RPi4B:~/java-dev/examples $



This version is using the StringBuilder class rather than adding a character or alphabet string to the result string. Night and day.

Code: Java
  1. // Java - 1mil3.java
  2.  
  3. class onemil3
  4.   {
  5.   public static void main(String args[])
  6.   {
  7.   StringBuilder s = new StringBuilder();
  8.   StringBuilder t = new StringBuilder();
  9.   int c = 0;
  10.   int[] a = new int[1000001];
  11.  
  12.   for (int x = 1; x <= 1000000; x++) {
  13.     c = ((x - 1) % 26) + 65;          
  14.     s.append((char) c);
  15.     a[x] = x;
  16.     if(s.length() == 26) {
  17.       t.append(s);
  18.       s.delete(0, s.length());
  19.     }
  20.   }
  21.  
  22.   StringBuilder r = new StringBuilder();
  23.   r.append(t);
  24.   r = r.reverse();
  25.  
  26.   System.out.println("r LEN: " + r.length());
  27.   System.out.println("Front: " + r.substring(0, 26));
  28.   System.out.println("Back:  " + r.substring(r.length() - 26));  
  29.   System.out.println("UBVal: " + a[1000000]);
  30.   }
  31. }
  32.  


pi@RPi4B:~/java-dev/examples $ javac 1mil3.java
pi@RPi4B:~/java-dev/examples $ /usr/bin/time java onemil3
r LEN: 999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal: 1000000
0.49user 0.14system 0:00.53elapsed 119%CPU (0avgtext+0avgdata 31948maxresident)k
0inputs+64outputs (0major+5024minor)pagefaults 0swaps
pi@RPi4B:~/java-dev/examples $


8
Programming Challenges / Re: Just REM
« on: December 04, 2019, 01:29:49 AM »
Perl

Code: Perl
  1. # Perl - 1mil3.pl
  2.  
  3. my $s = "";
  4. my $t = "";
  5. my $a = [1000000];
  6.  
  7. for (my $x = 1; $x <= 1000000; $x++) {
  8.   $s = $s . chr((($x - 1) % 26) + 65);
  9.   $a[$x] = $x;
  10.   if (length($s) == 26) {
  11.     $t = $t . $s;
  12.     $s = "";
  13.   }
  14. }
  15.  
  16.  
  17. my $r = scalar reverse $t;
  18.  
  19. printf("r LEN: %d\n", length($r));
  20. printf("Front: %s\n", substr($r, 0, 26));
  21. printf("Back:  %s\n", substr($r, -26));
  22. printf("UBVal: %d\n", $a[1000000]);
  23.  


pi@RPi4B:~/perl-dev/examples $ /usr/bin/time perl 1mil3.pl
r LEN: 999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal: 1000000
1.13user 0.07system 0:01.22elapsed 99%CPU (0avgtext+0avgdata 33636maxresident)k
0inputs+0outputs (0major+7705minor)pagefaults 0swaps
pi@RPi4B:~/perl-dev/examples $


9
Programming Challenges / Re: Just REM
« on: December 03, 2019, 07:35:46 PM »
C Update  (AIR submission)

Code: C
  1. // C Update - 1mil3.c - AIR
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. char *strrev(char *str)
  8. {
  9.     if (!str || ! *str)
  10.         return str;
  11.  
  12.     int i = strlen(str) - 1, j = 0;
  13.  
  14.     char ch;
  15.     while (i > j) {
  16.         ch = str[i];
  17.         str[i] = str[j];
  18.         str[j] = ch;
  19.         i--;
  20.         j++;
  21.     }
  22.     return str;
  23. }
  24.  
  25.  
  26. int main() {
  27.     int a[1000001] = {0};
  28.     char *t=NULL;
  29.     char tmp[2] = {0};
  30.     char s[27] = {0};
  31.     FILE *t_stream;
  32.     size_t len;
  33.  
  34.     t_stream = open_memstream(&t, &len);
  35.  
  36.     for (int x = 0; x < 1000001; x++) {
  37.         a[x] = x;
  38.         tmp[0] = (char)(x%26)+65;
  39.  
  40.         strncat(s,tmp,2);
  41.  
  42.         if (strlen(s) == 26) {
  43.             fprintf(t_stream, "%s",s);
  44.             s[0] = '\0';
  45.         }
  46.  
  47.     }
  48.  
  49.     fclose(t_stream);
  50.  
  51.     strrev(t);
  52.  
  53.     printf("r LEN: %lu\n",strlen(t));
  54.     printf("Front: %.*s\n", 26, t);
  55.     printf("Back:  %s\n", t + strlen(t) - 26);
  56.     printf("UBVal: %d\n",a[1000000]);
  57.  
  58.     free(t);
  59.  
  60. }
  61.  


pi@RPi4B:~/c-dev/examples $ gcc -O3 1mil3.2.c -o 1mil3.2
pi@RPi4B:~/c-dev/examples $ ls -l 1mil3.2
-rwxr-xr-x 1 pi pi 8296 Dec  3 11:31 1mil3.2
pi@RPi4B:~/c-dev/examples $ /usr/bin/time ./1mil3.2
r LEN: 999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal: 1000000
0.14user 0.02system 0:00.18elapsed 97%CPU (0avgtext+0avgdata 5888maxresident)k
0inputs+0outputs (0major+1565minor)pagefaults 0swaps
pi@RPi4B:~/c-dev/examples $


10
General Discussion / RPI Forum
« on: December 03, 2019, 04:40:04 PM »
Quote
I don't know if John is still following this thread or not, but if he is, thanks for finding that note. Those pages covered the mathematics so I could proceed directly to the software engineering needed to write an efficient program. It also helped to look at the sample code included in the post.

@ejolson,

Glad it helped! Wish you were here.

11
Programming Challenges / Re: Just REM
« on: December 03, 2019, 05:01:29 AM »
JADE (AIR submission)

Code: C++
  1. #include <jade.hpp>
  2.  
  3. MAIN
  4.     STRING s,t,r;
  5.     ARRAY<int> a;
  6.  
  7.     FOR( VAR x = 0 TO x<1000001 STEP x++ ) DO
  8.         a.push_back(x);
  9.         s += (CHAR)(x%26)+65;
  10.  
  11.         IF ( LEN(s) == 26 ) THEN
  12.             t += s;
  13.             s = "";
  14.         END
  15.     END
  16.  
  17.     r = REVERSE$(t);
  18.  
  19.     PRINT("r LEN: ",LEN(t));
  20.     PRINT("Front: ", LEFT$(r,26));
  21.     PRINT("Back:  ", RIGHT$(r,26));
  22.     PRINT("UBVal: ",a[1000000]);
  23. END
  24.  


pi@RPi4B:~/jade-dev/examples $ g++ -O3 -std=c++11 mil.cpp -ljade -o mil
pi@RPi4B:~/jade-dev/examples $ /usr/bin/time ./mil
r LEN: 999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal: 1000000
0.08user 0.04system 0:00.12elapsed 98%CPU (0avgtext+0avgdata 9012maxresident)k
0inputs+0outputs (0major+3762minor)pagefaults 0swaps
pi@RPi4B:~/jade-dev/examples $


12
Programming Challenges / Re: Just REM
« on: December 03, 2019, 01:42:35 AM »
JavaScript

Code: Javascript
  1. // JavaScript - 1mil3.js
  2.  
  3. function reverseString(str){
  4.     let strn ="";
  5.     for(let char of str){
  6.         strn = char + strn;
  7.     }
  8.     return strn;
  9. }
  10.  
  11. var s = "";
  12. var t = "";
  13. var a = [1000001];
  14.  
  15. for (x = 1; x <= 1000000; x++) {
  16.   s += String.fromCharCode(((x - 1) % 26) + 65);
  17.   a[x] = x;
  18.   if(s.length == 26) {
  19.         t += s;
  20.         s = "";
  21.   }
  22. }
  23.  
  24. r = reverseString(t);
  25.  
  26. console.log("r LEN: " + r.length);
  27. console.log("Front: " + r.substring(0, 26));
  28. console.log("Back:  " + r.substring(-0, 26));
  29. console.log("UBVal: " + a[1000000]);
  30.  


pi@RPi4B:~/javascript-dev/examples $ /usr/bin/time node 1mil3.js
r LEN: 999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal: 1000000
2.50user 0.27system 0:03.13elapsed 88%CPU (0avgtext+0avgdata 78340maxresident)k
38664inputs+0outputs (153major+19367minor)pagefaults 0swaps
pi@RPi4B:~/javascript-dev/examples $


13
General Discussion / MS Rust
« on: December 02, 2019, 06:52:49 PM »

14
Swift / Re: Swift RPi
« on: December 02, 2019, 03:50:31 PM »
I'm going to pass my findings over to the RPi Swift maintainer and hope he can find out why Swift runs so slow on the RPi.

15
Programming Challenges / Re: Just REM
« on: December 02, 2019, 05:26:34 AM »
Nim (AIR submission)

Code: Text
  1. proc reverse*(str: string): string =
  2.   result = ""
  3.   for index in countdown(str.high, 0):
  4.     result.add(str[index])
  5.  
  6. type
  7.     milarray = array[1000001, int]
  8.  
  9. var
  10.     s = ""
  11.     t = ""
  12.     r:string
  13.     a:milarray
  14.  
  15. for x in 0..1000000:
  16.     a[x] = x
  17.  
  18.     s &= chr((x mod 26) + 65)
  19.  
  20.     if s.len == 26:
  21.         t &= s
  22.         s = ""
  23.  
  24.  
  25. r = t.reverse
  26. echo "r Len: ",r.len
  27. echo "Front: ",r[0..25]
  28. echo "Back:  ",r[r.len-26..r.len-1]
  29. echo "UBVal: ",a[1000000]
  30.  


pi@RPi4B:~/nim-dev/examples $ nim c -d:release onemil3.nim
Hint: used config file '/etc/nim/nim.cfg' [Conf]
Hint: system [Processing]
Hint: onemil3 [Processing]
Hint:  [Link]
Hint: operation successful (12410 lines compiled; 0.566 sec total; 10.754MiB peakmem; Release Build) [SuccessX]
pi@RPi4B:~/nim-dev/examples $ ls -l onemil3
-rwxr-xr-x 1 pi pi 57548 Dec  1 21:21 onemil3
pi@RPi4B:~/nim-dev/examples $ /usr/bin/time ./onemil3
r Len: 999986
Front: ZYXWVUTSRQPONMLKJIHGFEDCBA
Back:  ZYXWVUTSRQPONMLKJIHGFEDCBA
UBVal: 1000000
0.06user 0.05system 0:00.12elapsed 99%CPU (0avgtext+0avgdata 10048maxresident)k
0inputs+0outputs (0major+2294minor)pagefaults 0swaps
pi@RPi4B:~/nim-dev/examples $


Pages: [1] 2 3 ... 6