473,549 Members | 2,615 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Knapsack 0-1 JavaScript binary & rosettacode & WE

24 New Member
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 340

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

Similar topics

27
3809
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 failed assertions for assertions that at first thought ought to hold, but that's not important.) At the end is a full program containing the above...
20
11246
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
3034
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 == desiredValue)
11
6393
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 and
4
2350
by: joe | last post by:
how to resize an upload image and then change to binary & insert to db
1
5472
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 <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template...
1
16975
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 cipher Number of comparisons decreases from N! to 2^N for example N=10 & N!=3628800 >> 2^N=1024
0
7157
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 in cipher Number of comparisons decreases from N! to 2^N for example N=10 & N!=3628800 >> 2^N=1024
0
403
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 Feature of algorithm: converts whole into a string and converts string into a whole
0
7521
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7451
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7473
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6044
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5088
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3501
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3483
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1061
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
764
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.