Skip to content

Commit a0c42ed

Browse files
authored
Merge pull request vJechsmayr#167 from dhkamp/maths/is_square_free
Added algorithm for checking if a number is square free or not
2 parents 80a52f1 + dad237e commit a0c42ed

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

‎Maths/isSquareFree.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function isSquareFree(num = 0) {
2+
for (let i = 2; i < Math.round(Math.sqrt(num) + 1); i++) {
3+
if (num % Math.pow(i, 2) === 0) return false;
4+
}
5+
return true;
6+
}
7+
8+
const squareFrees = [1, 3, 5, 6, 13, 15, 17, 19, 21, 22, 26, 29, 34, 35, 38];
9+
const notSquareFrees = [4, 8, 9, 12, 16, 18, 20, 24, 25, 27, 28, 32, 36, 40];
10+
11+
squareFrees.forEach(s => console.log(_isSquareFree(s)));
12+
notSquareFrees.forEach(n => console.log(_isSquareFree(n)));

0 commit comments

Comments
 (0)