From 6443a56de84ba508df0687a29f3340d83c2953e5 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Fri, 26 Mar 2021 13:27:47 -0400 Subject: [PATCH] Added getDivisors function --- Algorithms.ts | 79 +++++++++++++++++++++++++++++++++++++++++++++++ TestAlgorithms.ts | 19 +++++++++++- 2 files changed, 97 insertions(+), 1 deletion(-) diff --git a/Algorithms.ts b/Algorithms.ts index dbe2a68..bd31fa6 100644 --- a/Algorithms.ts +++ b/Algorithms.ts @@ -389,6 +389,85 @@ export function getFactorsBig(goalNumber: bigint): bigint[]{ return factors; } +export function getDivisors(goalNumber: number): number[]{ + let divisors: number[] = []; + + //Start by checking that the number is positive + if(goalNumber <= 0){ + return divisors; + } + //If the number is 1 return just itself + else if(goalNumber == 1){ + divisors.push(1); + return divisors; + } + + //Start at 3 and loop through all numbers < sqrt(goalNumber) looking for a number that divides it evenly + let topPossibleDivisor: number = Math.ceil(Math.sqrt(goalNumber)); + for(let possibleDivisor = 1;possibleDivisor <= topPossibleDivisor;++possibleDivisor){ + //If you find one add it and the number it creates to the list + if((goalNumber % possibleDivisor) == 0){ + divisors.push(possibleDivisor); + //Account for the possibility of sqrt(goalNumber) being a divisor + if(possibleDivisor != topPossibleDivisor){ + divisors.push(goalNumber / possibleDivisor); + } + if(divisors[divisors.length - 1] == (possibleDivisor + 1)){ + ++possibleDivisor; + } + } + } + + //Sort the list before returning it for neatness + divisors.sort((a, b) => a - b); + //Return the list + return divisors; +} +export function getDivisorsBig(goalNumber: bigint): bigint[]{ + let divisors: bigint[] = []; + + //Start by checking that the number is positive + if(goalNumber <= 0n){ + return divisors; + } + //If the number is 1 return just itself + else if(goalNumber == 1n){ + divisors.push(1n); + return divisors; + } + + //Start at 3 and loop through all numbers < sqrt(goalNumber) looking for a number that divides it evenly + let topPossibleDivisor: bigint = sqrtBig(goalNumber); + for(let possibleDivisor = 1n;possibleDivisor <= topPossibleDivisor;++possibleDivisor){ + //If you find one add it and the number it creates to the list + if((goalNumber % possibleDivisor) == 0n){ + divisors.push(possibleDivisor); + //Account for the possibility of sqrt(goalNumber) being a divisors + if(possibleDivisor != topPossibleDivisor){ + divisors.push(goalNumber / possibleDivisor); + } + if(divisors[divisors.length - 1] == (possibleDivisor + 1n)){ + ++possibleDivisor; + } + } + } + + //Sort the list before returning it for neatness + divisors.sort((a, b) => { + if(a > b){ + return 1; + } + else if(a < b){ + return -1; + } + else{ + return 0; + } + }); + //Return the list + return divisors; +} + export function getSum(ary: number[]): number{ let sum: number = 0; ary.forEach(a => sum += a); diff --git a/TestAlgorithms.ts b/TestAlgorithms.ts index dad1cb5..d0654bb 100644 --- a/TestAlgorithms.ts +++ b/TestAlgorithms.ts @@ -24,7 +24,7 @@ Copyright (C) 2021 Matthew Ellison import assert = require("assert"); import { SSL_OP_SSLEAY_080_CLIENT_DH_BUG } from "constants"; import { arrayEquals, getAllFib, getAllFibBig, getFactors, getFactorsBig, getPrimes, getPrimesBig, getNumPrimes, getNumPrimesBig, - sqrtBig, getSum, getSumBig, getProd, getProdBig } from "./Algorithms"; + sqrtBig, getSum, getSumBig, getProd, getProdBig, getDivisors, getDivisorsBig } from "./Algorithms"; function testArrayEquals(){ @@ -170,6 +170,22 @@ function testGetFactors(){ console.log("getFactors passed"); } +function testGetDivisors(){ + //Test 1 + let correctAnswer: number[] = [1, 2, 4, 5, 10, 20, 25, 50, 100]; + let topNum: number = 100; + let answer: number[] = getDivisors(topNum); + assert.ok(arrayEquals(answer, correctAnswer), "getDivisors number failed"); + + //Test 2 + let bigCorrectAnswer: bigint[] = [1n, 2n, 4n, 5n, 10n, 20n, 25n, 50n, 100n]; + let bigTopNum: bigint = 100n; + let bigAnswer: bigint[] = getDivisorsBig(bigTopNum); + assert.ok(arrayEquals(bigAnswer, bigCorrectAnswer), "getDivisors BigInt failed"); + + console.log("getDivisors passed"); +} + function testGetSum(){ //Test 1 let correctAnswer: number = 0; @@ -219,5 +235,6 @@ testGetAllFib(); testGetPrimes(); testGetNumPrimes(); testGetFactors(); +testGetDivisors(); testGetSum(); testGetProd();