Finding number of open water taps after n chances using JavaScript

Problem

Suppose a school organises this game on their Annual Day celebration −

There are "n" water taps and "n" students are chosen at random. The instructor asks the first student to go to every tap and open it. Then he has the second student go to every second tap and close it. The third goes to every third tap and, if it is closed, he opens it, and if it is open, he closes it. The fourth student does this to every fourth tap, and so on. After the process is completed with the "n"th student, how many taps are open?

We are required to write a JavaScript function that takes in the number n and returns the number of open water taps.

Understanding the Pattern

Let's analyze what happens to each tap:

  • Tap 1: Touched by student 1 only ? Open
  • Tap 4: Touched by students 1, 2, 4 (odd number of times) ? Open
  • Tap 9: Touched by students 1, 3, 9 (odd number of times) ? Open
  • Tap 16: Touched by students 1, 2, 4, 8, 16 (odd number of times) ? Open

A tap remains open if it's touched an odd number of times. This happens only when the tap number is a perfect square, because perfect squares have an odd number of divisors.

Solution

const num = 15;
const openTaps = (num = 1) => {
    const arr = [];
    let index = 1;
    while(index ** 2 

3

Step-by-Step Explanation

const openTapsDetailed = (num) => {
    console.log(`Finding open taps for n = ${num}`);
    const perfectSquares = [];
    let index = 1;
    
    while(index ** 2 

Finding open taps for n = 15
Perfect square found: 1
Perfect square found: 4
Perfect square found: 9
Total open taps: 3
---
Finding open taps for n = 100
Perfect square found: 1
Perfect square found: 4
Perfect square found: 9
Perfect square found: 16
Perfect square found: 25
Perfect square found: 36
Perfect square found: 49
Perfect square found: 64
Perfect square found: 81
Perfect square found: 100
Total open taps: 10

Alternative Approach

const openTapsSimple = (n) => {
    // Count perfect squares from 1 to n
    // This is equivalent to Math.floor(Math.sqrt(n))
    return Math.floor(Math.sqrt(n));
};

console.log("Using Math.sqrt approach:");
console.log(`n=15: ${openTapsSimple(15)} taps open`);
console.log(`n=100: ${openTapsSimple(100)} taps open`);
console.log(`n=50: ${openTapsSimple(50)} taps open`);
Using Math.sqrt approach:
n=15: 3 taps open
n=100: 10 taps open
n=50: 7 taps open

Why This Works

The key insight is that only taps numbered as perfect squares (1, 4, 9, 16, 25...) remain open. This is because:

  • Most numbers have an even number of divisors (pairs)
  • Perfect squares have an odd number of divisors (the square root pairs with itself)
  • A tap changes state each time it's touched, so odd touches = open, even touches = closed

Conclusion

The number of open water taps equals the count of perfect squares from 1 to n, which can be calculated as Math.floor(Math.sqrt(n)). This elegant mathematical solution avoids simulating the entire process.

Updated on: 2026-03-15T23:19:00+05:30

142 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements