Author Topic: All Digits  (Read 7687 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: All Digits
« Reply #30 on: September 21, 2020, 10:12:25 pm »
Quote
When I copied the bplus six liner it was 201 bytes instead of 199, not too sure why.

Anyway, if we do away with insisting on proper formatting and take a few other tricks, you can get it down to 142 characters:

Hi Luke,

I already anticipated playing games with spaces and said this (above):
Quote
I also better explicitly state that the QB64 IDE v 1.4 stable sets the bytes and judges the syntax.

For byte count I copy and pasted QB64 IDE formatted code into Notepad++ and I bet Notepad++ removes final CR +LF that ends QB64 files so everyone's file has 2 more than I stated.

PS So you reduced SPACE$() to SPC() -AE$ nice! and got rid of a _TRIM$(), -8 very nice!


@Steve after reloading the long line 3 times and getting the dang file written and run it is done at n = 2, do you know why? because the string to compare needs to be 10 digits long plus a space not = "48" which can be found way too soon! BTW the EXE is longer than the bas file as expected.
« Last Edit: September 21, 2020, 10:23:04 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: All Digits
« Reply #31 on: September 21, 2020, 11:04:24 pm »
Test 3 Liner code revised to this for 11 character string match, see 3rd line changes

Code: QB64: [Select]
  1. a$ = " 4761328509"
  2. 1 n = n + 1
  3. IF INSTR(a$, LEFT$(STR$(n * n) + _TRIM$(STR$(n ^ 3)) + SPC(9), 11)) THEN PRINT n, n ^ 2, n ^ 3 ELSE GOTO 1
  4.  
  5.  

  [ You are not allowed to view this attachment ]  
« Last Edit: September 21, 2020, 11:07:28 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: All Digits
« Reply #32 on: September 21, 2020, 11:10:57 pm »
Here is the program that wrote the 3 lines, no human would want to!

Code: QB64: [Select]
  1. _TITLE "Permutations with value translator" 'B+ add value translator 2019-03-31
  2. 'from "Permutations wo recursion"  translate from SmallBasic to QB64 2019-03-31
  3. 'from "Permutations" translation from: PowerBASIC, tsh copy from Liberty link 2017-02-04
  4.  
  5. SCREEN _NEWIMAGE(800, 600, 32)
  6. _SCREENMOVE 300, 40
  7.  
  8. REDIM results$(0)
  9. loadPermsValues results$()
  10. 'display result$
  11.  
  12. OPEN "All Digit 3 liner by steve.bas" FOR OUTPUT AS #1
  13. PRINT #1, "a$ = "; CHR$(34);
  14.  
  15. FOR i = 0 TO UBOUND(results$)
  16.     PRINT i + 1, results$(i)
  17.     PRINT #1, results$(i) + " ";
  18.     'IF i MOD 30 = 29 THEN
  19.     '    PRINT "press any to continue..."
  20.     '    SLEEP
  21.     '    CLS
  22.     'END IF
  23. PRINT #1, CHR$(34)
  24. PRINT #1, "1 n = n + 1"
  25. PRINT #1, "IF INSTR(a$, LEFT$(STR$(n * n) + _TRIM$(STR$(n ^ 3)) + SPC(9), 11)) THEN PRINT n, n ^ 2, n ^ 3 ELSE GOTO 1"
  26.  
  27. ValuesData:
  28. DATA 0,1,2,3,4,5,6,7,8,9,"END"
  29.  
  30. 'this reads data from ValuesData line and translates Permutations to those values
  31. SUB loadPermsValues (r() AS STRING)
  32.     'load values array one way or another? read data
  33.     REDIM values(0) AS STRING
  34.     RESTORE ValuesData
  35.     DO
  36.         READ r$
  37.         IF r$ = "END" THEN
  38.             done = 1
  39.         ELSE
  40.             n = n + 1
  41.             REDIM _PRESERVE values(n) AS STRING
  42.             values(n) = r$
  43.         END IF
  44.     LOOP UNTIL done
  45.     n = UBOUND(values)
  46.     DIM a(0 TO n + 1) '+1 needed due to bug in LB that checks loop condition: until (i=0) or (a(i)<a(i+1))
  47.     FOR i = 0 TO n: a(i) = i: NEXT 'load a() with minimum values
  48.     DO
  49.         b$ = ""
  50.         FOR i = 1 TO n
  51.             b$ = b$ + values(a(i))
  52.         NEXT
  53.         REDIM _PRESERVE r(e)
  54.         r(e) = b$
  55.         e = e + 1
  56.  
  57.         i = n
  58.         DO
  59.             i = i - 1
  60.         LOOP UNTIL (i = 0) OR (a(i) < a(i + 1))
  61.         j = i + 1
  62.         k = n
  63.         WHILE j < k
  64.             SWAP a(j), a(k)
  65.             j = j + 1
  66.             k = k - 1
  67.         WEND
  68.         IF i > 0 THEN
  69.             j = i + 1
  70.             WHILE a(j) < a(i)
  71.                 j = j + 1
  72.             WEND
  73.             SWAP a(i), a(j)
  74.         END IF
  75.     LOOP UNTIL i = 0
  76.     EXIT SUB
  77.  

BTW I was halfway through the first run that I was expecting to take 3+ hours but only takes <20 mins, when I realized we didn't need any permutation that started with 0, that cuts out 1/10th of them? O well...

@SMcNeill
So Einstein your theory worked! :)
« Last Edit: September 21, 2020, 11:17:12 pm by bplus »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: All Digits
« Reply #33 on: September 21, 2020, 11:31:48 pm »
Here is the program that wrote the 3 lines, no human would want to!

Code: QB64: [Select]
  1. _TITLE "Permutations with value translator" 'B+ add value translator 2019-03-31
  2. 'from "Permutations wo recursion"  translate from SmallBasic to QB64 2019-03-31
  3. 'from "Permutations" translation from: PowerBASIC, tsh copy from Liberty link 2017-02-04
  4.  
  5. SCREEN _NEWIMAGE(800, 600, 32)
  6. _SCREENMOVE 300, 40
  7.  
  8. REDIM results$(0)
  9. loadPermsValues results$()
  10. 'display result$
  11.  
  12. OPEN "All Digit 3 liner by steve.bas" FOR OUTPUT AS #1
  13. PRINT #1, "a$ = "; CHR$(34);
  14.  
  15. FOR i = 0 TO UBOUND(results$)
  16.     PRINT i + 1, results$(i)
  17.     PRINT #1, results$(i) + " ";
  18.     'IF i MOD 30 = 29 THEN
  19.     '    PRINT "press any to continue..."
  20.     '    SLEEP
  21.     '    CLS
  22.     'END IF
  23. PRINT #1, CHR$(34)
  24. PRINT #1, "1 n = n + 1"
  25. PRINT #1, "IF INSTR(a$, LEFT$(STR$(n * n) + _TRIM$(STR$(n ^ 3)) + SPC(9), 11)) THEN PRINT n, n ^ 2, n ^ 3 ELSE GOTO 1"
  26.  
  27. ValuesData:
  28. DATA 0,1,2,3,4,5,6,7,8,9,"END"
  29.  
  30. 'this reads data from ValuesData line and translates Permutations to those values
  31. SUB loadPermsValues (r() AS STRING)
  32.     'load values array one way or another? read data
  33.     REDIM values(0) AS STRING
  34.     RESTORE ValuesData
  35.     DO
  36.         READ r$
  37.         IF r$ = "END" THEN
  38.             done = 1
  39.         ELSE
  40.             n = n + 1
  41.             REDIM _PRESERVE values(n) AS STRING
  42.             values(n) = r$
  43.         END IF
  44.     LOOP UNTIL done
  45.     n = UBOUND(values)
  46.     DIM a(0 TO n + 1) '+1 needed due to bug in LB that checks loop condition: until (i=0) or (a(i)<a(i+1))
  47.     FOR i = 0 TO n: a(i) = i: NEXT 'load a() with minimum values
  48.     DO
  49.         b$ = ""
  50.         FOR i = 1 TO n
  51.             b$ = b$ + values(a(i))
  52.         NEXT
  53.         REDIM _PRESERVE r(e)
  54.         r(e) = b$
  55.         e = e + 1
  56.  
  57.         i = n
  58.         DO
  59.             i = i - 1
  60.         LOOP UNTIL (i = 0) OR (a(i) < a(i + 1))
  61.         j = i + 1
  62.         k = n
  63.         WHILE j < k
  64.             SWAP a(j), a(k)
  65.             j = j + 1
  66.             k = k - 1
  67.         WEND
  68.         IF i > 0 THEN
  69.             j = i + 1
  70.             WHILE a(j) < a(i)
  71.                 j = j + 1
  72.             WEND
  73.             SWAP a(i), a(j)
  74.         END IF
  75.     LOOP UNTIL i = 0
  76.     EXIT SUB
  77.  

BTW I was halfway through the first run that I was expecting to take 3+ hours but only takes <20 mins, when I realized we didn't need any permutation that started with 0, that cuts out 1/10th of them? O well...

@SMcNeill
So Einstein your theory worked! :)

I knew it should.  I was just too lazy to prove it.  So what do I win?  Two cookies for my browser?  ;D
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: All Digits
« Reply #34 on: September 21, 2020, 11:38:11 pm »
Quote
I knew it should.  I was just too lazy to prove it.  So what do I win?  Two cookies for my browser?  ;D

I think you've had enough bytes. ;-)

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: All Digits
« Reply #35 on: September 21, 2020, 11:38:51 pm »
And actually, you can cut it down to 2 lines, if you use the string value directly, rather than store it in a$ first.

One counter, one IF condition checker...  I can’t see it getting any more simplified than that.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: All Digits
« Reply #36 on: September 21, 2020, 11:41:17 pm »
What!?

Oh I see 2 lines, wow!

Working...
« Last Edit: September 21, 2020, 11:48:58 pm by bplus »

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: All Digits
« Reply #37 on: September 22, 2020, 01:37:25 am »
I already anticipated playing games with spaces and said this
Ah, I didn't realise you meant the formatter and indenter as well - I took it to mean it just had to compile in QB64 1.4, and not my local branch which has a _NUMSQUARECUBEALLDIGITS function
« Last Edit: September 22, 2020, 06:01:52 am by luke »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: All Digits
« Reply #38 on: September 22, 2020, 01:34:57 pm »
After numerous tries attempting to do a 1 Liner with colon the bas program hangs in checking file... before compile and Run:
  [ You are not allowed to view this attachment ]  

Here is what the abbreviated program looks like:
Code: QB64: [Select]
  1. 1 n = n + 1: IF INSTR(" 4761328509", LEFT$(STR$(n * n) + _TRIM$(STR$(n ^ 3)) + SPC(9), 11)) THEN PRINT n, n ^ 2, n ^ 3 ELSE GOTO 1
  2.  
  3.  
" 4761328509" in INSTR call was replaced by all permutations of 10 digits minus the ones starting with 0
created with this file:

Code: QB64: [Select]
  1. _TITLE "All Digits 1 liner with colon Program Writer" 'B+ add value translator 2019-03-31
  2. 'from "Permutations wo recursion"  translate from SmallBasic to QB64 2019-03-31
  3. 'from "Permutations" translation from: PowerBASIC, tsh copy from Liberty link 2017-02-04
  4. ' 2020-09-21  OK for a 2 Liner version of All Digits following up on Steve's ideas
  5. '             OK let's use a colon and not waste space in IDE ;-))
  6. ' 2020-09-22 That program got constantly got stuck in checking file, never compliled.
  7. ' So I used another permutation generator to write a DAT file that generates all the permutations of 10 digits.
  8. ' This program will open that file and skip down to line 362,881 which ends all permutations that start with 0
  9. ' and begins with permuations that start with 1. That cuts 1/10 of 3,628,810 permutations out from line which
  10. ' should make the program easier to check and run. Let's see...
  11. '    NOPE: again gets stuck checking the file, even with 10% less permutations in the string.
  12.  
  13.  
  14. SCREEN _NEWIMAGE(800, 600, 32)
  15. _SCREENMOVE 300, 40
  16.  
  17. 'here is the simple model of the line to write tested to make sure it did find 69
  18. '1 n = n + 1: IF INSTR(" 4761328509", LEFT$(STR$(n * n) + _TRIM$(STR$(n ^ 3)) + SPC(9), 11)) THEN PRINT n, n ^ 2, n ^ 3 ELSE GOTO 1
  19.  
  20. ' now replace " 4761328509" with All Permutations of 10 digits except those that start with 0
  21. OPEN "All Digits 1 Liner with colon.bas" FOR OUTPUT AS #1 ' output .bas program file
  22. OPEN "10 Digit Permutations.DAT" FOR INPUT AS #2
  23.  
  24. PRINT #1, "1 n = n + 1: IF INSTR(" + CHR$(34);
  25.     i = i + 1
  26.     LINE INPUT #2, fline$
  27.     IF i > 362880 AND _TRIM$(fline$) <> "" THEN
  28.         PRINT fline$
  29.         'IF i < 362890 THEN   ' debugging tests
  30.         PRINT #1, " "; fline$;
  31.         'ELSE         ' debugging tests
  32.         'EXIT WHILE    ' debugging tests
  33.         'END IF           ' debugging tests
  34.     END IF
  35. PRINT #1, CHR$(34) + ", LEFT$(STR$(n * n) + _TRIM$(STR$(n ^ 3)) + SPC(9), 11)) THEN PRINT n, n ^ 2, n ^ 3 ELSE GOTO 1"
  36. PRINT "Bas program file ready."
  37.  

Attached is: 10 Digit Permutations.DAT file in case anyone else wants to take a shot at it. Might be handy for other things too.

Now you may well ask why the colon after saying no colons, well I didn't say no colons I said colons will count as extra lines, in this case this would be counted as a "2 Liner" but I just thought it would look cool to see only 1 line that says GOTO itself at the end of it. Oh well. In byte counts, colons count the same as CR + LF so there is that side of it too.

I think it's getting hung at INSTR with the extra-extra long literal string and would not be saved by actual 2 liner without colon.
« Last Edit: September 22, 2020, 01:43:39 pm by bplus »