-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sherlock and Squares.js
71 lines (61 loc) · 1.64 KB
/
Sherlock and Squares.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//compared to for loops ,maths patterns is a simplified and fastest approach ,look at the last solution
function squares(a,b){
for(let i=a,counter = 0;i<=b;i++){
if(Number.isInteger(Math.sqrt(i))){
counter++;
return counter;
}
}
}
squares(3,9);
function squares(a,b){
let counter = 0;
for(let i=a;i<=b;i += 1){
if(Math.sqrt(i) % 1 == 0){
counter++;
}
}
console.log(counter)
// return (counter);
}
if(Math.sqrt(i) == Math.floor(Math.sqrt(i))){
ar.push(Math.sqrt(i))
counter = ar.filter((x) => x % 1 == 0)
function squares(a, b) {
// Write your code here
return Math.floor(Math.sqrt(b)) - Math.ceil(Math.sqrt(a)) + 1;
}
function squares(a, b) {
let down = Math.round(Math.sqrt(a));
let up = Math.round(Math.sqrt(b));
if(down*down < a) down++;
if(up*up > b) up--;
return (up-down)+1;
}
function squares(a,b){
let countSq=0
let counterF=a
let counterL=b
while(!(Number.isInteger(Math.sqrt(counterF)))){
counterF++
}
while(!(Number.isInteger(Math.sqrt(counterL)))){
counterL--
}
let first=Math.sqrt(counterF)
let last =Math.sqrt(counterL)
return last+1-first
}
function squares(a, b) {
var lowerCeil = Math.ceil(Math.sqrt(a));
var upperFloor = Math.floor(Math.sqrt(b))
return upperFloor-lowerCeil+1;
}
//Beautiful simplified solution
function squares(a, b) {
// Get smallest square root int in range
const minInt = Math.ceil(Math.sqrt(a));
// Get largest square root int in range
const maxInt = Math.floor(Math.sqrt(b));
return maxInt - minInt + 1;
}