Wonders Electrical Engineers Do With Bulbs!

You sneak into the house of a famous Electical Engineer in your town.

You find a weird arrangement of 10000 bulbs and 10000 switches (which are numbered in ascending order) in front of you. You see the main power button to this and out of curiosity you press it and suddenly the door behind you gets locked and the bulbs start glowing and the switches start toggling on/off in a weird pattern.

After some time the glowing stops.In front of the arrangement a board reads as follows:

  • During the first pulse all the switches (with corresponding numbers) that are divisible by 1 are switched on.
  • During the second pulse all the numbers that are divisible by 2 toggle (change to on if it's off, or change to off if it's on).
  • During the third pulse all the numbers that are divisible by 3 are made to toggle and so on until 10000 pulses.
  • At the end of 1000 0 th 10000^\text{th} pulse let N N denote the number of bulbs that are glowing and let S S denote the sum of the corresponding numbers of the bulbs that are glowing.
  • Enter the answer as N + S N+S to open the door.


The answer is 338450.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

2 solutions

Behold the power of a pure functional programming language!

1
2
Prelude> let x = takeWhile (<=10000) [i^2 | i <- [1..]] in length x + sum x
338450

The time complexity of this solution is Θ ( n ) \Theta (\sqrt n)


Proof of how only squares work:

Let τ ( n ) \tau (n) denote the number of positive divisors of n n (including both 1 and n n ).

Before the first pulse, all the bulbs are off (call this the 0 state and call the on state as the 1 state). After the all of the 10000 pulses, say the i t h i^{th} bulb is in state r r . Then we claim that r τ ( n ) ( m o d 2 ) r \equiv \tau (n) \pmod 2 . The proof of this easy to see if the toggling function (T) is seen as follows : T : { 0 , 1 } { 0 , 1 } T ( i ) i + 1 ( m o d 2 ) T : \{0,1\} \to \{0,1\} \\ T(i) \equiv i+1 \pmod 2

Hence the i t h i^{th} bulb is on if and only if τ ( i ) \tau (i) is odd.

We show now that τ ( i ) \tau (i) is odd if and only if n n is a perfect square.

Let n = i p i α i n = \displaystyle \prod_{i} p_i^{\alpha_i} where p i p_i is a prime for all i i .

Then it follows that if d n d|n then d = i p i β i 0 β i α i d = \displaystyle \prod_{i} p_i^{\beta_i} \quad 0 \leq \beta_i \leq \alpha_i

Hence, τ ( n ) = i ( 1 + α i ) \tau (n) = \prod_{i} (1+\alpha_i) The desired conclusion now follows.

Nice solution, Deeparaj! How about you explain why only the squares work?

Agnishom Chattopadhyay - 4 years, 9 months ago

Log in to reply

I've added the proof now.

A Former Brilliant Member - 4 years, 9 months ago

Log in to reply

Thanks, this is a cool solution

Agnishom Chattopadhyay - 4 years, 9 months ago
Vignesh S
Aug 26, 2016

The solutions are perfect squares. Other non-programming solutions are also accepted.

Thanks ,will use it..

Vignesh S - 4 years, 9 months ago

Nice to see you working on coding problems. Do you want me to help you with the syntax for posting code on Brilliant?

By the way, here is a video explaining why only the squares work.

Agnishom Chattopadhyay - 4 years, 9 months ago

Yeah, please do help me with the syntax for posting a code on brilliant

Vignesh S - 4 years, 9 months ago

It's this: (just remove the backslash)

1
2
3
```c
Here is my c code
\```

Agnishom Chattopadhyay - 4 years, 9 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...