Skip to content

Instantly share code, notes, and snippets.

@alwaisy
Last active November 26, 2022 09:04
Show Gist options
  • Save alwaisy/39c1a9a12146347d6058d2bc7e141169 to your computer and use it in GitHub Desktop.
Save alwaisy/39c1a9a12146347d6058d2bc7e141169 to your computer and use it in GitHub Desktop.
/**
Objective
Today, we're learning about Interfaces. Check out the Tutorial tab for learning materials and an instructional video!
Task
The AdvancedArithmetic interface and the method declaration for the abstract divisorSum(n) method are provided for you in the editor below.
Complete the implementation of Calculator class, which implements the AdvancedArithmetic interface. The implementation for the divisorSum(n) method must return the sum of all divisors of .
Example
The divisors of are . Their sum is .
The divisors of are and their sum is .
Input Format
A single line with an integer, .
Constraints
Output Format
You are not responsible for printing anything to stdout. The locked template code in the editor below will call your code and print the necessary output.
Sample Input
6
Sample Output
I implemented: AdvancedArithmetic
12
Explanation
The integer is evenly divisible by , , , and . Our divisorSum method should return the sum of these numbers, which is . The Solution class then prints on the first line, followed by the sum returned by divisorSum (which is ) on the second line.
*/
// solution - all tests [0 to 6] were passed
'use strict';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString: string = '';
let inputLines: string[] = [];
let currentLine: number = 0;
process.stdin.on('data', function(inputStdin: string): void {
inputString += inputStdin;
});
process.stdin.on('end', function(): void {
inputLines = inputString.split('\n');
inputString = '';
new Calculate().main();
});
function readLine(): string {
return inputLines[currentLine++];
}
interface AdvancedArithmetic {
divisorSum(n: number): number;
}
class Calculate implements AdvancedArithmetic {
divisorSum(n: number) {
const c = console.log.bind(console)
let divisors: number[] = []
for(let i = 1; i <= n; i++) {
if(n%i == 0) {
divisors.push(i)
}
}
const sum = divisors.reduce((a: number, b: number) => {
return a + b
}, 0)
return c(sum)
}
main() {
const n = parseInt(readLine())
console.log('I implemented: AdvancedArithmetic')
this.divisorSum(n)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment