Avatar of brunnock

brunnock's solution

to Perfect Numbers in the JavaScript Track

Published at Aug 19 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

Determine if a number is perfect, abundant, or deficient based on Nicomachus' (60 - 120 CE) classification scheme for natural numbers.

The Greek mathematician Nicomachus devised a classification scheme for natural numbers, identifying each as belonging uniquely to the categories of perfect, abundant, or deficient based on their aliquot sum. The aliquot sum is defined as the sum of the factors of a number not including the number itself. For example, the aliquot sum of 15 is (1 + 3 + 5) = 9

  • Perfect: aliquot sum = number
    • 6 is a perfect number because (1 + 2 + 3) = 6
    • 28 is a perfect number because (1 + 2 + 4 + 7 + 14) = 28
  • Abundant: aliquot sum > number
    • 12 is an abundant number because (1 + 2 + 3 + 4 + 6) = 16
    • 24 is an abundant number because (1 + 2 + 3 + 4 + 6 + 8 + 12) = 36
  • Deficient: aliquot sum < number
    • 8 is a deficient number because (1 + 2 + 4) = 7
    • Prime numbers are deficient

Implement a way to determine whether a given number is perfect. Depending on your language track, you may also need to implement a way to determine whether a given number is abundant or deficient.

Setup

Go through the setup instructions for JavaScript to install the necessary dependencies:

http://exercism.io/languages/javascript/installation

Running the test suite

The provided test suite uses Jasmine. You can install it by opening a terminal window and running the following command:

npm install -g jasmine

Run the test suite from the exercise directory with:

jasmine perfect-numbers.spec.js

In many test suites all but the first test have been marked "pending". Once you get a test passing, activate the next one by changing xit to it.

Source

Taken from Chapter 2 of Functional Thinking by Neal Ford. http://shop.oreilly.com/product/0636920029687.do

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

perfect-numbers.spec.js

var PerfectNumbers = require('./perfect-numbers');

describe('Exercise - Perfect Numbers', function () {
  var perfectNumbers;

  beforeEach(function () {
    perfectNumbers = new PerfectNumbers();
  });

  describe('Perfect Numbers', function () {
    it('Smallest perfect number is classified correctly', function () {
      expect(perfectNumbers.classify(6)).toEqual('perfect');
    });

    it('Medium perfect number is classified correctly', function () {
      expect(perfectNumbers.classify(28)).toEqual('perfect');
    });

    it('Large perfect number is classified correctly', function () {
      expect(perfectNumbers.classify(33550336)).toEqual('perfect');
    });
  });

  describe('Abundant Numbers', function () {
    it('Smallest abundant number is classified correctly', function () {
      expect(perfectNumbers.classify(12)).toEqual('abundant');
    });

    it('Medium abundant number is classified correctly', function () {
      expect(perfectNumbers.classify(30)).toEqual('abundant');
    });

    it('Large abundant number is classified correctly', function () {
      expect(perfectNumbers.classify(33550335)).toEqual('abundant');
    });
  });

  describe('Deficient Numbers', function () {
    it('Smallest prime deficient number is classified correctly', function () {
      expect(perfectNumbers.classify(2)).toEqual('deficient');
    });

    it('Smallest non-prime deficient number is classified correctly', function () {
      expect(perfectNumbers.classify(4)).toEqual('deficient');
    });

    it('Medium deficient number is classified correctly', function () {
      expect(perfectNumbers.classify(32)).toEqual('deficient');
    });

    it('Large deficient number is classified correctly', function () {
      expect(perfectNumbers.classify(33550337)).toEqual('deficient');
    });

    it('Edge case (no factors other than itself) is classified correctly', function () {
      expect(perfectNumbers.classify(1)).toEqual('deficient');
    });
  });

  describe('Invalid Inputs', function () {
    it('Zero is rejected (not a natural number)', function () {
      expect(perfectNumbers.classify(0)).toEqual('Classification is only possible for natural numbers.');
    });

    it('Negative integer is rejected (not a natural number)', function () {
      expect(perfectNumbers.classify(-1)).toEqual('Classification is only possible for natural numbers.');
    });
  });
});
class PerfectNumbers {
  classify(num) {
    if (num<1) return 'Classification is only possible for natural numbers.';
    if (num<6) return 'deficient';
    const sqrt= Math.sqrt(num);
    const possibleFactors= Array(~~sqrt+1).fill(true);
    let sum=1;
    if (sqrt === ~~sqrt) sum+=sqrt;
    for (let x=2; x<sqrt; ++x) {
      if (possibleFactors[x]) {
	if (num % x === 0) {
	  sum+= x + num/x;
	} else {
	  for (let y=(x+x); y<=sqrt; y+=x) possibleFactors[y]=false;
	}
      }
    }
    if (sum < num) return 'deficient';
    if (sum > num) return 'abundant';
    return 'perfect';	
  }
}
module.exports=PerfectNumbers;

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?