waltermilner

Untitled

Mar 27th, 2016
256
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 gcd(x, y)
  8.     { //Euler's Algorithm to find GCD
  9.         var a, b, r;
  10.         var a = x < y ? x : y;
  11.         var b = x < y ? y : x;
  12.         // a is smaller
  13.         do
  14.         {
  15.             r = a % b;// find remainder
  16.             a = b; // swap : smaller to bigger
  17.             b = r; // remainder to smaller
  18.  
  19.         } while (r !== 0)
  20.         return a;
  21.     }
  22.  
  23.     function relPrime(x, y)
  24.     { // are x and y relatively prime?
  25.         return (gcd(x, y) === 1);
  26.     }
  27.  
  28.     function phi(n)
  29.     { // count how many are relatively prime
  30.         var count = 0;
  31.         for (var i = 1; i <= n; i++)
  32.             if (relPrime(i, n))
  33.                 count++;
  34.         return count;
  35.     }
  36.  
  37.     for (var n = 1; n < 100; n++)
  38.         document.writeln(n + " " + phi(n) + "<br>");
  39.   </script>
  40.   <head>
  41.     <title>Euler's Phi</title>
  42.  </head>
  43.  <body>
  44.  </body>
  45. </html>
Advertisement
Add Comment
Please, Sign In to add comment