Skip to content

Commit 60f117f

Browse files
committed
make sure its interger and make sure negative numbers are handled properly
1 parent 834d545 commit 60f117f

File tree

2 files changed

+21
-22
lines changed

2 files changed

+21
-22
lines changed

src/index.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,13 @@ const PORT = process.env.PORT || CONFIG.DEFAULT_PORT;
1818
app.use(cors());
1919
app.use(express.json());
2020

21-
app.get("/", (req: Request, res: Response) => {
22-
res.send(CONFIG.ROOT_ROUTE_MESSAGE);
23-
});
24-
2521
app.get("/api/classify-number", async (req: Request, res: Response) => {
2622
try {
2723
const numberParam = req.query.number as string;
2824
const number = parseInt(numberParam);
2925

30-
if (isNaN(number)) {
26+
// Validate integer input
27+
if (isNaN(number) || !Number.isInteger(number)) {
3128
return res.status(400).json({
3229
number: numberParam,
3330
error: true,
@@ -62,5 +59,5 @@ app.get("/api/classify-number", async (req: Request, res: Response) => {
6259
});
6360

6461
app.listen(PORT, () => {
65-
console.log(`Server is running on http://localhost:${PORT}`);
62+
console.log(`Server is running on port ${PORT}`);
6663
});

src/utils/mathUtils.ts

+18-16
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,39 @@
1-
import { CONFIG } from "../config";
2-
31
export function isPrime(n: number): boolean {
4-
if (n <= CONFIG.MIN_NUMBER || n > Number.MAX_SAFE_INTEGER) return false;
5-
for (let i = CONFIG.MIN_DIVISOR; i <= Math.sqrt(n); i++) {
6-
if (n % i === 0) return false;
2+
const absN = Math.abs(n);
3+
if (absN <= 1) return false;
4+
for (let i = 2; i <= Math.sqrt(absN); i++) {
5+
if (absN % i === 0) return false;
76
}
87
return true;
98
}
109

1110
export function isPerfect(n: number): boolean {
12-
if (n <= CONFIG.MIN_NUMBER) return false;
13-
let sum = CONFIG.MIN_NUMBER;
14-
for (let i = CONFIG.MIN_DIVISOR; i <= Math.sqrt(n); i++) {
15-
if (n % i === 0) {
11+
const absN = Math.abs(n);
12+
if (absN <= 1) return false;
13+
let sum = 1;
14+
for (let i = 2; i <= Math.sqrt(absN); i++) {
15+
if (absN % i === 0) {
1616
sum += i;
17-
if (i !== n / i) sum += n / i;
17+
if (i !== absN / i) sum += absN / i;
1818
}
1919
}
20-
return sum === n;
20+
return sum === absN;
2121
}
2222

2323
export function isArmstrong(n: number): boolean {
24-
const digits = String(n).split("");
24+
const absN = Math.abs(n);
25+
const digits = String(absN).split("");
2526
const length = digits.length;
2627
const sum = digits.reduce(
2728
(acc, digit) => acc + Math.pow(Number(digit), length),
2829
0
2930
);
30-
return sum === n;
31+
return sum === absN;
3132
}
3233

3334
export function calculateDigitSum(n: number): number {
34-
return String(n)
35-
.split("")
36-
.reduce((acc, digit) => acc + Number(digit), 0);
35+
const sign = n < 0 ? -1 : 1;
36+
const digits = Math.abs(n).toString().split("");
37+
const sum = digits.reduce((acc, digit) => acc + Number(digit), 0);
38+
return sign * sum;
3739
}

0 commit comments

Comments
 (0)