Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <script>
- 'use strict';
- function gcd(x, y)
- { //Euler's Algorithm to find GCD
- var a, b, r;
- var a = x < y ? x : y;
- var b = x < y ? y : x;
- // a is smaller
- do
- {
- r = a % b;// find remainder
- a = b; // swap : smaller to bigger
- b = r; // remainder to smaller
- } while (r !== 0)
- return a;
- }
- function relPrime(x, y)
- { // are x and y relatively prime?
- return (gcd(x, y) === 1);
- }
- function phi(n)
- { // count how many are relatively prime
- var count = 0;
- for (var i = 1; i <= n; i++)
- if (relPrime(i, n))
- count++;
- return count;
- }
- for (var n = 1; n < 100; n++)
- document.writeln(n + " " + phi(n) + "<br>");
- </script>
- <head>
- <title>Euler's Phi</title>
- </head>
- <body>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment