waltermilner

Untitled

Apr 5th, 2016
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3.  
  4.   <script>
  5.     'use strict';
  6.  
  7.     function PrimeField(n)
  8.     {
  9.         this.elements = [];
  10.         for (var q = 0; q < n; q++)
  11.             this.elements.push(q);
  12.         this.order = n;
  13.  
  14.         this.add = function (x, y)
  15.         {
  16.             return (x + y) % this.order;
  17.         };
  18.  
  19.         this.mult = function (x, y)
  20.         {
  21.             return (x * y) % this.order;
  22.         };
  23.  
  24.         this.isElement = function (x)
  25.         {
  26.             return x > -1 && x < this.order;
  27.         };
  28.  
  29.         // is this closed? so x+y and x.y are elements for all x and y
  30.         this.isClosed = function ()
  31.         {
  32.             for (var x = 0; x < this.order; x++)
  33.                 for (var y = 0; y < this.order; y++)
  34.                 { // closed under addition
  35.                     if (!this.isElement(this.add(x, y)))
  36.                         return false;
  37.                 }
  38.             for (var x = 0; x < this.order; x++)
  39.                 for (var y = 0; y < this.order; y++)
  40.                 { // closed under multiplication
  41.                     if (!this.isElement(this.mult(x, y)))
  42.                         return false;
  43.                 }
  44.             return true;
  45.         };
  46.  
  47.         // is this associative?
  48.         this.isAssoc = function ()
  49.         {
  50.             for (var x = 0; x < this.order; x++)
  51.                 for (var y = 0; y < this.order; y++)
  52.                     for (var z = 0; z < this.order; z++)
  53.                     { // for all x,y,z, x+(y+z) = (x+y)+z
  54.                         if (this.add(x, this.add(y, z)) !== this.add(this.add(x, y), z))
  55.                             return false;
  56.                     }
  57.             for (var x = 0; x < this.order; x++)
  58.                 for (var y = 0; y < this.order; y++)
  59.                     for (var z = 0; z < this.order; z++)
  60.                     { // for all x,y,z, x.(y.z) = (x.y).z
  61.                         if (this.mult(x, this.mult(y, z)) !== this.mult(this.mult(x, y), z))
  62.                             return false;
  63.                     }
  64.             return true;
  65.         };
  66.  
  67.         // is this commutative?
  68.         this.isCommut = function ()
  69.         {
  70.             for (var x = 0; x < this.order; x++)
  71.                 for (var y = 0; y < this.order; y++)
  72.  
  73.                 { // for all x,y,z, x+(y+z) = (x+y)+z
  74.                     if (this.add(x, y) !== this.add(y, x))
  75.                         return false;
  76.                 }
  77.             for (var x = 0; x < this.order; x++)
  78.                 for (var y = 0; y < this.order; y++)
  79.  
  80.                 { // for all x,y,z, x.(y.z) = (x.y).z
  81.                     if (this.mult(x, y) !== this.mult(y, x))
  82.                         return false;
  83.                 }
  84.             return true;
  85.         };
  86.  
  87.         // has additive identity
  88.         this.hasAddIdent = function ()
  89.         {
  90.             for (var x = 0; x < this.order; x++)
  91.             {
  92.                 var found = true;
  93.                 for (var y = 0; y < this.order; y++)
  94.                     if (this.add(x, y) !== y)
  95.                         found = false;
  96.                 if (found)
  97.                     return true;
  98.             }
  99.             return false;
  100.         };
  101.  
  102.         // has multiplicative identity
  103.         this.hasMultIdent = function ()
  104.         {
  105.             for (var x = 0; x < this.order; x++)
  106.             {
  107.                 var found = true;
  108.                 for (var y = 0; y < this.order; y++)
  109.                     if (this.mult(x, y) !== y)
  110.                         found = false;
  111.                 if (found)
  112.                     return true;
  113.             }
  114.             return false;
  115.         };
  116.  
  117.         this.hasIdentities = function ()
  118.         {
  119.             return this.hasAddIdent() && this.hasMultIdent();
  120.         };
  121.  
  122. // has additive inverse
  123.         this.hasAddInverse = function ()
  124.         {
  125.             for (var x = 0; x < this.order; x++) // for each element x
  126.             {
  127.                 var found = false;
  128.                 for (var y = 0; y < this.order; y++) // y is candidate inverse
  129.                     if (this.add(x, y) === 0)
  130.                     {
  131.                         found = true; // got it
  132.                         break; // stop looking
  133.                     }
  134.                 if (!found) // failed to find inverse
  135.                     return false;
  136.             }
  137.             return true; // all x have an inverse
  138.         };
  139.  
  140.  
  141.  
  142.         // has multiplicative inverse
  143.         this.hasMultInverse = function ()
  144.         {
  145.             for (var x = 1; x < this.order; x++) // for each element x
  146.             {
  147.                 var found = false;
  148.                 for (var y = 0; y < this.order; y++) // y is candidate inverse
  149.                     if (this.mult(x, y) === 1)
  150.                     {
  151.                         found = true; // got it
  152.                         break; // stop looking
  153.                     }
  154.                 if (!found) // failed to find inverse
  155.                 {
  156.                     document.write(x);
  157.                     return false;
  158.                 }
  159.             }
  160.             return true; // all x have an inverse
  161.         };
  162.  
  163.         // has inverses
  164.         this.hasInverses = function ()
  165.         {
  166.             return this.hasAddInverse() && this.hasMultInverse();
  167.         };
  168.  
  169.         // distributive
  170.         this.isDistributive = function ()
  171.         {
  172.             for (var x = 0; x < this.order; x++) // for each element x
  173.                 for (var y = 0; y < this.order; y++)
  174.                     for (var z = 0; z < this.order; z++)
  175.                         if (this.mult(x, this.add(y, z)) !== this.add(this.mult(x, y), this.mult(x, z)))
  176.                             return false;
  177.  
  178.             return true;
  179.         };
  180.  
  181.     }
  182.  
  183.     function show(x)
  184.     {
  185.         if (x === 0)
  186.             document.write("O");
  187.         else {
  188.             var num = 0x2460 + x - 1;
  189.             document.write(String.fromCharCode(num));
  190.         }
  191.     }
  192.  
  193.     var G5 = new PrimeField(5);
  194.  
  195.  
  196.     document.write(G5.isDistributive());
  197.  
  198.  
  199.   </script>
  200.   <head>
  201.     <title>Field</title>
  202.   </head>
  203.   <body>
  204.   </body>
  205. </html>
Add Comment
Please, Sign In to add comment