Skip to content

feat(maths): juggler sequence #120

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

Merged
merged 9 commits into from
Mar 31, 2023
Merged
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
23 changes: 23 additions & 0 deletions maths/juggler_sequence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* The juggler sequence is a integer sequence that starts with an positive integer a and the subsequent terms are
* described as following:
* if a_k is even:
* a_k+1 = floor(sqrt(a_k))
* else:
* a_k+1 = floor(sqrt(a_k^3))
*
* Time Complexity: linear (O(n))
*
* @param a The number to start with
* @param n The index of the searched number in the sequence.
* @returns The number at index n in the sequence.
* @see https://en.wikipedia.org/wiki/Juggler_sequence
*/
export const jugglerSequence = (a: number, n: number) => {
let k: number = a;
for (let i: number = 0; i < n; i++) {
k = Math.floor(Math.pow(k, (k % 2 === 0 ? 1 : 3) / 2));
}

return k;
};
12 changes: 12 additions & 0 deletions maths/test/juggler_sequence.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { jugglerSequence } from '../juggler_sequence';

describe('jugglerSequence', () => {
it.each([
[3, 3, 36],
[3, 5, 2],
[7, 3, 2],
[5, 1, 11],
])('%i at index %i should equal %i', (a, n, k) => {
expect(jugglerSequence(a, n)).toBe(k);
});
});