Sha256: a49386a34379cee6267da178f45b80e4c10e8279e16e8c56d17f3a367e55a39b

Contents?: true

Size: 1.23 KB

Versions: 66

Compression:

Stored size: 1.23 KB

Contents

class PerfectNumbers {
    static classify(n: number): string {
        let i
        let sum
        let result

        // Check if the input is valid
        if (n <= 0) {
            throw new Error('Classification is only possible for natural numbers.')
        }

        // Factorize the current number.
        const divsArray = this.getDivisors(n)

        // Sum the factors.
        sum = 0
        for (i = 0; i < divsArray.length; i++) {
            sum = sum + divsArray[i]
        }

        // Check if the number is perfect.
        if (sum === n) {
            result = 'perfect'
        } else if (sum > n) {
            result = 'abundant'
        } else {
            result = 'deficient'
        }

        return result
    }

    static getDivisors(n: number): number[] {
        const divs: number[] = []

        // Accepts only natura numbers greater than 1.
        if (n <= 1) {
            return divs
        }

        // 1 always divides everyone!
        divs.push(1)

        // Calculate the divisors up the the half of the number + 1
        for (let i = 2; i <= n / 2; i++) {
            if (n % i === 0) {
                divs.push(i)
            }
        }

        return divs
    }
}

export default PerfectNumbers

Version data entries

66 entries across 66 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/typescript/exercises/perfect-numbers/perfect-numbers.example.ts
trackler-2.2.1.179 tracks/typescript/exercises/perfect-numbers/perfect-numbers.example.ts
trackler-2.2.1.178 tracks/typescript/exercises/perfect-numbers/perfect-numbers.example.ts
trackler-2.2.1.177 tracks/typescript/exercises/perfect-numbers/perfect-numbers.example.ts
trackler-2.2.1.176 tracks/typescript/exercises/perfect-numbers/perfect-numbers.example.ts
trackler-2.2.1.175 tracks/typescript/exercises/perfect-numbers/perfect-numbers.example.ts
trackler-2.2.1.174 tracks/typescript/exercises/perfect-numbers/perfect-numbers.example.ts
trackler-2.2.1.173 tracks/typescript/exercises/perfect-numbers/perfect-numbers.example.ts
trackler-2.2.1.172 tracks/typescript/exercises/perfect-numbers/perfect-numbers.example.ts
trackler-2.2.1.171 tracks/typescript/exercises/perfect-numbers/perfect-numbers.example.ts
trackler-2.2.1.170 tracks/typescript/exercises/perfect-numbers/perfect-numbers.example.ts
trackler-2.2.1.169 tracks/typescript/exercises/perfect-numbers/perfect-numbers.example.ts
trackler-2.2.1.167 tracks/typescript/exercises/perfect-numbers/perfect-numbers.example.ts
trackler-2.2.1.166 tracks/typescript/exercises/perfect-numbers/perfect-numbers.example.ts
trackler-2.2.1.165 tracks/typescript/exercises/perfect-numbers/perfect-numbers.example.ts
trackler-2.2.1.164 tracks/typescript/exercises/perfect-numbers/perfect-numbers.example.ts
trackler-2.2.1.163 tracks/typescript/exercises/perfect-numbers/perfect-numbers.example.ts
trackler-2.2.1.162 tracks/typescript/exercises/perfect-numbers/perfect-numbers.example.ts
trackler-2.2.1.161 tracks/typescript/exercises/perfect-numbers/perfect-numbers.example.ts
trackler-2.2.1.160 tracks/typescript/exercises/perfect-numbers/perfect-numbers.example.ts