Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add euler totient function, issue #10 #93

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/.nyc_output
/npm-debug.log
dist
.lvimrc
30 changes: 29 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# [algorithms-js](https://github.com/manrajgrover/algorithms-js#readme) *0.0.8*
# [algorithms-js](https://github.com/manrajgrover/algorithms-js#readme) *0.0.13*

> Algorithms Library in JavaScript

Expand Down Expand Up @@ -138,6 +138,34 @@ https://en.wikibooks.org/wiki/Algorithm_Implementation/Geometry/Tangents_between



### src/algorithms/math/euler_totient.js


#### phi(n)

Euler's totient function (phi) counts the number of integers less than n
which are relatively prime to n




##### Parameters

| Name | Type | Description | |
| ---- | ---- | ----------- | -------- |
| n | `Number` | the number |   |




##### Returns


- `Number` the number of integers less than n which are relatively prime to n




### src/algorithms/math/extended_euclidean.js


Expand Down
96 changes: 94 additions & 2 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1">
<title>algorithms-js 0.0.8 &#8212; Algorithms Library in JavaScript</title>
<title>algorithms-js 0.0.13 &#8212; Algorithms Library in JavaScript</title>
<meta name="description" content="Algorithms Library in JavaScript">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/github.min.css">
Expand Down Expand Up @@ -163,7 +163,7 @@

<h1>
<a href="https://github.com/manrajgrover/algorithms-js#readme">algorithms-js</a>
<small>0.0.8</small>
<small>0.0.13</small>
</h1>

<p>Algorithms Library in JavaScript</p>
Expand Down Expand Up @@ -235,6 +235,21 @@ <h1>
</li>


<li>
<a href="#src/algorithms/math/euler_totient.js" class="file"><b>src/algorithms/math/euler_totient.js</b></a>
<ul>


<li class="method-link scope-public">
<a href="#src-algorithms-math-euler_totient.js-phi" title="phi">phi</a>
</li>


</ul>

</li>


<li>
<a href="#src/algorithms/math/extended_euclidean.js" class="file"><b>src/algorithms/math/extended_euclidean.js</b></a>
<ul>
Expand Down Expand Up @@ -1628,6 +1643,83 @@ <h3>Returns</h3>



<a id="src/algorithms/math/euler_totient.js"></a>


<div class="method scope-public">

<h2 id="src-algorithms-math-euler_totient.js-phi">
<a href="#src-algorithms-math-euler_totient.js-phi" class="permalink">#</a>
<span class="method-name" title="phi(n)">phi(n)</span>
</h2>

<div class="description lead">

<p>Euler's totient function (phi) counts the number of integers less than n
which are relatively prime to n</p>


</div>


<section class="parameters">

<h3>Parameters</h3>

<div class="table-responsive">
<table class="table">

<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Description</th>
<th></th>
</tr>
</thead>

<tbody>
<tr>
<td>n</td>
<td>
<code>Number</code>
</td>
<td><p>the number</p>
</td>
<td>
</td>
</tr>
</tbody>

</table>
</div>

</section>




<section class="returns">

<h3>Returns</h3>



<p>
<code>Number</code>
</p>

<p>the number of integers less than n which are relatively prime to n</p>




</section>

</div>



<a id="src/algorithms/math/extended_euclidean.js"></a>


Expand Down
34 changes: 34 additions & 0 deletions src/algorithms/math/euler_totient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Euler's totient function (phi) counts the number of integers less than n
* which are relatively prime to n
*
* @param {Number} n the number
* @return {Number} the number of integers less than n which are relatively prime to n
*/


const phi = (n) => {
if (n < 0) return null;

let num = n;
let result = n;
let p = 2;

while (p * p <= num) {
if (num % p === 0) {
while (num % p === 0) {
num = Math.floor(num / p);
}
result -= Math.floor(result / p);
}
p += 1;
}

if (num > 1) {
result -= Math.floor(result / num);
}

return result;
};

module.exports = phi;
4 changes: 3 additions & 1 deletion src/algorithms/math/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ const gcd = require('./gcd');
const fastexp = require('./fast_exp');
const lcm = require('./lcm');
const modularInverse = require('./modular_inverse');
const eulerTotient = require('./euler_totient');

module.exports = {
extendedEuclidean,
gcd,
fastexp,
lcm,
modularInverse
modularInverse,
eulerTotient
};
28 changes: 28 additions & 0 deletions test/algorithms/math/testEulerTotient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* eslint-env mocha */
const phi = require('../../../src').algorithms.math.eulerTotient;
const assert = require('assert');

describe('Euler Totient (phi)', () => {
it('should return null for n < 0', () => {
assert.equal(phi(-1), null);
assert.equal(phi(-10), null);
assert.equal(phi(-Infinity), null);
});

it('should return 0 for n = 0', () => {
assert.equal(phi(0), 0);
});

it('should return phi(n) for n > 0', () => {
assert.equal(phi(1), 1);
assert.equal(phi(2), 1);
assert.equal(phi(3), 2);
assert.equal(phi(4), 2);
});

it('should return n-1 if n is prime', () => {
assert.equal(phi(5), 4);
assert.equal(phi(13), 12);
assert.equal(phi(29), 28);
});
});