Algorithm of 10110 - Light, more light

Problem Description Link
Algorithm:
To solve this problem you need to know last bulb is on or off.
Every i'th walk, only bulbs which serial divisible by i will be toggled(means on to off or off to on).
 the last bulb are toggel for all factors of last bulb no(factors of "n")
  if n=9 factors are 1 3 9.
  if n=16 factors 1 2 4 8 16.
See this pattern like this:
 walk 1 paired with walk "n", walk 2 paired with walk "n-1",walk 3 paired with walk "n-3", and so on until walk sqrt(n).
so just check "sqrt(n)" is integer or not if integer "yes" else "no"
for check you can use
if (floor(sqrt(n))==ceil(sqrt(n)))

NB:  12/5 = 2.4    
     folor= 2  and   ceil= 3


image

No comments:

Post a Comment

Write your comment - Share Knowledge and Experience