QB64 Phoenix Edition
Knapsack 0-1 & rosettacode & qbasic qb64 & WE - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3)
+---- Forum: Programs (https://qb64phoenix.com/forum/forumdisplay.php?fid=7)
+---- Thread: Knapsack 0-1 & rosettacode & qbasic qb64 & WE (/showthread.php?tid=463)



Knapsack 0-1 & rosettacode & qbasic qb64 & WE - DANILIN - 05-22-2022

Knapsack 0-1 & rosettacode & qbasic qb64 & WE

For all people: send yours algorithms to rosettacode
otherwise forum may disappear even in google search

Classic Knapsack problem is solved in many ways

Contents: http://rosettacode.org/wiki/Knapsack_problem
Long read: rosettacode.org/wiki/Knapsack_problem/0-1

Previous topics and long programs: Knapsack
https://qb64forum.alephc.xyz/index.php?topic=3091
Ordered Combinations Generator
https://qb64forum.alephc.xyz/index.php?topic=2999

My newest program synthesizes all ciphers from 0 & 1
adding an extra register and 0 remain on left in cipher

Number of comparisons decreases from N! to 2^N
for example N=5 N!=120 >> 2^N=32

Random values origin are automatically assigned
quantity and quality and integral of value is obtained
and in general: integral of quantity and quality
and it is possible to divide only anyone will not understand

Program write results to qb64 directory

Code: (Select All)
Open "knapsack.txt" For Output As #1
N=7: L=5: a = 2^(N+1): Randomize Timer 'knapsack.bas DANILIN
Dim L(N), C(N), j(N), q(a), q$(a), d(a)

For m=a-1 To (a-1)/2 Step -1: g=m: Do ' sintez shifr
    q$(m)=LTrim$(Str$(g Mod 2))+q$(m)
    g=g\2: Loop Until g=0
    q$(m)=Mid$(q$(m), 2, Len(q$(m)))
Next

For i=1 To N: L(i)=Int(Rnd*3+1) ' lenght & cost
C(i)=10+Int(Rnd*9): Print #1, i, L(i), C(i): Next ' origin

For h=a-1 To (a-1)/2 Step -1
    For k=1 To N: j(k)=Val(Mid$(q$(h), k, 1)) ' from shifr
        q(h)=q(h)+L(k)*j(k)*C(k) ' 0 or 1
        d(h)=d(h)+L(k)*j(k)
    Next
    If d(h) <= L Then Print #1, d(h), q(h), q$(h)
Next
max=0: m=1: For i=1 To a
    If d(i)<=L Then If q(i) > max Then max=q(i): m=i
Next
Print #1,: Print #1, d(m), q(m), q$(m): End

Main thing is very brief and clear to even all

Results is reduced manually:

Code: (Select All)
1             2             17
2             2             14
3             2             17
4             1             11
5             2             18
6             3             14
7             3             10

5             73           1101000
4             62           1100000
2             28           0100000
5             81           0011100 !!!
3             45           0011000
5             76           0010010
2             36           0000100

5             81           0011100



RE: Knapsack 0-1 & rosettacode & qbasic qb64 & WE - DANILIN - 06-01-2022

Knapsack 0-1 binary Python & C# have been created
without textbooks using my best practices
and I will post later but for now...

Learning how to write QB64 programs on rosettacode.org

Register and / or / xor log in rosettacode.org

We find page where your topic is

We look for QB64 in list and find

Having not found QB64 we edit nearest
one alphabetically above
and page will distribute itself


=={{header|QB64}}==

{{NAME|QB64}}

<lang QB64> PROGRAM </lang>

{{out}}
<pre> RESULTS </pre>

PICTURE
[[File:Screenshot.png]]


RE: Knapsack 0-1 & rosettacode & qbasic qb64 & WE - DANILIN - 08-05-2022

YaBasic online: Danilin Knapsack 0-1

https://www.jdoodle.com/iembed/v0/suj


RE: Knapsack 0-1 & rosettacode & qbasic qb64 & WE - TempodiBasic - 08-07-2022

No problem, there are so many tasks not coded into QB64!
you can find them following this path:
starting from the homepage select from menu EXPLORE and then LANGUAGES, then QB64
Well in this last appeared page  look at the second box from the high to the bottom on the right edge of page.
You can read "YOUR HELP NEEDED" and into it there is a link that let get you the last (the more adjourned) list of tasks that lack of QB64 code .
If you register and then login into Rosetta code you can EDIT (so also add a QB64 section)....
I have always put  QB64 into Q and not among the different BASIC dialects into Basic section under letter B.

This set of tags is very useful... I often use the header and the open/closing tags  <lang QB64>  ....... </lang>

Quote:Having not found QB64 we edit nearest
one alphabetically above
and page will distribute itself


=={{header|QB64}}==

{{NAME|QB64}}

<lang QB64> PROGRAM </lang>

{{out}}
<pre> RESULTS </pre>

PICTURE
[[File:Screenshot.png]]