difference-of-squares.ts 444 B

123456789101112131415161718192021
  1. export class Squares {
  2. constructor(private count: number) {
  3. }
  4. get sumOfSquares(): number {
  5. let sum = 0;
  6. for (let i = 0; i <= this.count; i++) {
  7. sum += i * i;
  8. }
  9. return sum;
  10. }
  11. get squareOfSum(): number {
  12. let sum = this.count * (this.count + 1) / 2;
  13. return sum * sum;
  14. }
  15. get difference(): number {
  16. return this.squareOfSum-this.sumOfSquares;
  17. }
  18. }