A positive integer is an anti-prime when it has more divisors than each positive integer lesser than it.
https://www.youtube.com/watch?v=2JM2oImb9Qg
Write a function that find the largest anti-prime number not exceeding a given number.
1 - as there is no positive numbers below 1 the 1 is a first anti-prime and has one divisor.
2 - has two divisors: 1, 2 so is's anti-prime.
3 - has also two divisors: 1, 3 so it's NOT an anti-prime because 2 already had two divisors.
Other examples of anti-primes are: 1, 2, 4, 6, 12, 24.
export function solution(max: number): number {
if (max === 0) {
return 0;
}
let maxDiv: number = 0;
let lastAnti = 1;
for (let possibleAnti: number = 2; possibleAnti <= max; possibleAnti++) {
let divCount = 1;
for (let possibleDiv = 1; possibleDiv <= max; possibleDiv++) {
if (possibleAnti % possibleDiv === 0) {
divCount++;
}
}
if (divCount > maxDiv) {
maxDiv = divCount;
lastAnti = possibleAnti;
}
}
return lastAnti;
}
import { solution } from './solution';
import { assert } from 'chai';
describe("example", function() {
it("test", function() {
assert.strictEqual(solution(0), 0);
assert.strictEqual(solution(1), 1);
assert.strictEqual(solution(2), 2);
assert.strictEqual(solution(3), 2);
assert.strictEqual(solution(4), 4);
assert.strictEqual(solution(5), 4);
assert.strictEqual(solution(11), 6);
assert.strictEqual(solution(25), 24);
assert.strictEqual(solution(5040), 5040);
});
});