473,441 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Knapsack 0-1 JavaScript binary & rosettacode & WE

24 16bit
Knapsack 0-1 JavaScript binary & rosettacode & WE

Classic Knapsack problem is solved in many ways

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=10 & N!=3628800 >> 2^N=1024

Random values origin are automatically assigned
quantity and quality and integral of value is obtained
and in general: integral of quantity and quality

Copy & Save as & Run
KnapSackDa.htm

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8">
  2. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  3. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  4. <title>KNAPSACK JavaScript</title> </head> <body> <noscript>Vkluch JS</noscript>
  5.  
  6. jdoodle.com/h/2Uc
  7.  
  8. rextester.com/BQYV50962
  9.  
  10. <script>
  11.  
  12. var n=12; G=2; a = Math.pow(2,n+1); // KNAPSACKj.js
  13. var dec, i, h, k, max, m, s; 
  14. var L=[n], C=[n], j=[n], q=[a], d=[a]; e=[a];
  15.  
  16. document.write("<br><br>#  Kol Cena<br>")
  17. document.write("#  Amo Price<br><br>")
  18.  
  19. for (i=0; i<n; i++)
  20. { L[i]=1+Math.floor(Math.random()*3) 
  21.   C[i]=10+Math.floor(Math.random()*9); j[i]=0;
  22.   document.write( (i+1) +" "+ L[i] +" "+ C[i] +"<br>") 
  23. for (i=0; i<a; i++) { q[i]=0; d[i]=0;}
  24. document.write("<br>")
  25.  
  26. document.write("Mx Kol St-st Schifr<br>") 
  27. document.write("Mx Amo Price Chifer<br>") 
  28.  
  29. for (h = a-1; h>(a-1)/2; h--)
  30. { dec=h; e[h]=""
  31.  
  32. while (dec > 0)
  33. { s = Math.floor(dec % 2);
  34.   e[h] = s + e[h]; dec = Math.floor(dec/2);
  35. }
  36.  
  37. if (e[h] == "") {e[h] = "0";} 
  38. e[h]= e[h].substr(1, e[h].length-1); 
  39.  
  40. for (k=0; k<n; k++)
  41. { j[k] = Number(e[h].substr(k,1)); 
  42.   q[h]=q[h]+L[k]*j[k]*C[k];
  43.   d[h]=d[h]+L[k]*j[k];
  44. }
  45.  
  46. if (d[h] <= G)
  47. document.write("<br>"+ G +" "+ d[h] +" "+ q[h] +" "+ e[h]) 
  48. } document.write("<br>")
  49.  
  50. max=0; m=1;
  51. for (i=0; i<a; i++)
  52. { if (d[i]<=G && q[i]>max){ max=q[i]; m=i;}
  53. }
  54.  
  55. document.write("<br>"+ d[m] +" "+ q[m] +" "+ e[m] +"<br><br>") 
  56.  
  57. document.write("Mx St-st Schifr<br>")
  58. document.write("Mx Price Cipher<br><br>") 
  59.  
  60. </script>
  61.  
  62. </body> </html>
Expand|Select|Wrap|Line Numbers
  1. # Kol Cena
  2. # Amo Price
  3.  
  4. 1 1 10
  5. 2 3 13
  6. 3 2 17
  7. 4 3 12
  8. 5 3 15
  9.  
  10. Mx Kol St-st Schifr
  11. Mx Amo Price Chifer
  12.  
  13. 3 3 44 10100
  14. 3 1 10 10000
  15. 3 3 39 01000
  16. 3 2 34 00100
  17. 3 3 36 00010
  18. 3 3 45 00001
  19. 3 0 0 00000
  20.  
  21. 3 45 00001
  22.  
  23. Mx St-st Schifr
  24. Mx Price Cipher
Mar 13 '23 #1
0 319

Sign in to post your reply or Sign up for a free account.

Similar topics

27
by: Daniel Vallstrom | last post by:
I'm having problems with inconsistent floating point behavior resulting in e.g. assert( x > 0.0 && putchar('\n') && x == 0.0 ); holding. (Actually, my problem is the dual one where I get...
20
by: William | last post by:
Original question: "Give a one-line C expression to test whether a number is a power of 2. " Answer: if (x && !(x & (x-1)) == 0) My question: Why does this expression work?
17
by: orekinbck | last post by:
Hi There Say I want to check if object1.Property1 is equal to a value, but object1 could be null. At the moment I have code like this: if (object1 != null) { if (object1.Property ==...
11
by: Jeremy | last post by:
How can one stop a browser from converting &amp; to & ? We have a textarea in our system wehre a user can type in some html code and have it saved to the database. When the data is retireved...
4
by: joe | last post by:
how to resize an upload image and then change to binary & insert to db
1
by: VaidehiPawar | last post by:
I am a beginner level in xml..my output page does not convert &gt &lt it shows something like this " &lt;b&gt;Location.&lt;/b&gt;&lt;br /&gt; &lt;UL&gt;&lt;LI&gt;Park Central New York " can anyone help? here is my code ...
1
by: DANILIN | last post by:
Knapsack 0-1 C# binary & rosettacode & WE Classic Knapsack problem is solved in many ways My newest program synthesizes all ciphers from 0 & 1 adding an extra register and 0 remain on left in...
0
by: DANILIN | last post by:
Knapsack 0-1 Python binary & rosettacode & WE Classic Knapsack problem is solved in many ways My newest program synthesizes all ciphers from 0 & 1 adding an extra register and 0 remain on left...
0
by: DANILIN | last post by:
Knapsack 0-1 C++ binary & WE Classic Knapsack problem is solved in many ways My newest program synthesizes all ciphers from 0 & 1 adding an extra register and 0 remain on left in cipher ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.