export function solution(max: number): number { if (max <= 1) { return max; } let primesFromMax: Set<number> = generatePrimes(max) let resultNumber:number = 0; let resultNumberOfDividers:number = 0; for (let i = 2; i <= max; i++) { let numberOfDividers = getNumberOfDividers(i, primesFromMax) if (numberOfDividers > resultNumberOfDividers){ resultNumber = i resultNumberOfDividers = numberOfDividers } } return resultNumber; } function getNumberOfDividers(inputNumber: number, primes: Set<number>): number { if (primes.has(inputNumber)) { return 2; } var result:number = 1 var tempPrimes: Array<number> = Array.from(primes) var dividers = new Map() while(inputNumber > 1) { for (var i = 0; i <= tempPrimes.length; i++) { if (inputNumber == 1){ break } if (inputNumber%tempPrimes[i] == 0) { if (dividers.has(tempPrimes[i])) { dividers.set(tempPrimes[i], dividers.get(tempPrimes[i])+1) } else { dividers.set(tempPrimes[i], 1) } inputNumber = inputNumber/tempPrimes[i]; i--; } } } dividers.forEach( (value) => { result = result*(value+1) }) return result; } function generatePrimes(max: number): Set<number> { var tempArr = fillNumberInArray(max) tempArr.splice(0,2) var primes = new Set(tempArr); for (let i = 2; i <= max; i++) { if (!primes.has(i)) { continue; } let inLoop:number = i + i primes.forEach( () => { if (primes.has(inLoop)) { primes.delete(inLoop); } inLoop += i }) } return primes; } function fillNumberInArray(max: number): Array<number> { var ar = []; ar[max] = 1; return Array.from(ar.keys()) }
- export function solution(max: number): number {
if (max === 0) {return 0;- if (max <= 1) {
- return max;
- }
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++;- let primesFromMax: Set<number> = generatePrimes(max)
- let resultNumber:number = 0;
- let resultNumberOfDividers:number = 0;
- for (let i = 2; i <= max; i++) {
- let numberOfDividers = getNumberOfDividers(i, primesFromMax)
- if (numberOfDividers > resultNumberOfDividers){
- resultNumber = i
- resultNumberOfDividers = numberOfDividers
- }
- }
- return resultNumber;
- }
- function getNumberOfDividers(inputNumber: number, primes: Set<number>): number {
- if (primes.has(inputNumber)) {
- return 2;
- }
- var result:number = 1
- var tempPrimes: Array<number> = Array.from(primes)
- var dividers = new Map()
- while(inputNumber > 1) {
- for (var i = 0; i <= tempPrimes.length; i++) {
- if (inputNumber == 1){
- break
- }
- if (inputNumber%tempPrimes[i] == 0) {
- if (dividers.has(tempPrimes[i])) {
- dividers.set(tempPrimes[i], dividers.get(tempPrimes[i])+1)
- } else {
- dividers.set(tempPrimes[i], 1)
- }
- inputNumber = inputNumber/tempPrimes[i];
- i--;
- }
- }
- }
if (divCount > maxDiv) {maxDiv = divCount;lastAnti = possibleAnti;- dividers.forEach( (value) => {
- result = result*(value+1)
- })
- return result;
- }
- function generatePrimes(max: number): Set<number> {
- var tempArr = fillNumberInArray(max)
- tempArr.splice(0,2)
- var primes = new Set(tempArr);
- for (let i = 2; i <= max; i++) {
- if (!primes.has(i)) {
- continue;
- }
- let inLoop:number = i + i
- primes.forEach( () => {
- if (primes.has(inLoop)) {
- primes.delete(inLoop);
- }
- inLoop += i
- })
- }
}return lastAnti;- return primes;
- }
- function fillNumberInArray(max: number): Array<number> {
- var ar = [];
- ar[max] = 1;
- return Array.from(ar.keys())
- }