java - Could somebody please explain me how the following code actually works -


could please explain me how following code works.

problem: have 64 doors in row closed. make 64 passes doors. first time through, visit every door , toggle door (if door closed, open it; if open, close it). second time visit every 2nd door (door #2, #4, #6, ...). third time, every 3rd door (door #3, #6, #9, ...), etc, until visit 64th door.

the following code found works, i'd know happening. don't understand loop.

public class doors { public static void main(string args[]) {     // assume true=open, false=closed. doors default closed.     boolean[] doors = new boolean[64];     (int i=0; i<64; i++) {         (int j=i; j<64; j=j+i+1) {             doors[j] = !doors[j];         }     }     // @ end, print out doors closed     system.out.println("these doors opened:");     (int i=0;i<64;i++){         if (doors[i]) {             // printing out i+1 negate zero-indexing             system.out.println(i+1);         }     } } } 

assuming know how for loops works, still, lets take @ following example:

first assume of doors closed , close means false f, open means true t. simpler understanding assuming that, total number of doors = 4.

index       :     0       1       2       3               +-----+ +-----+ +-----+ +-----+ begin       : |  f  | |  f  | |  f  | |  f  | // doors closed               +-----+ +-----+ +-----+ +-----+ door number :    1       2       3       4 

now

 (int i=0; i<4; i++) { // our example has 4 doors instead of 64         // first iteration of outer loop, = 0         (int j=i; j<4; j=j+i+1) {             // = 0, j = 0             // , j = j+i+1 = j + 0 + 1 = j +1              // j increment 1             // hence, j < 4 means loop rotate 4 (j = 0 3) times              doors[j] = !doors[j]; // line trick, see bellow details.         }     } 

the doors[j] = !doors[j]; toggles current state. how? suppose doors[j] contains false means door closed, !doors[j] change value false true means closed open! ta da, that's want!

   index(value of j) :    0       1       2       3                        +-----+ +-----+ +-----+ +-----+ after 1st iteration  : |  t  | |  t  | |  t  | |  t  |                        +-----+ +-----+ +-----+ +-----+          door number :    1       2       3       4 

all 4 doors opens!

now, 2nd iteration of outer loop,

 (int i=0; i<4; i++) { // our example has 4 doors instead of 64         // 2nd iteration of outer loop, = 1         (int j=i; j<4; j=j+i+1) {             // = 1, j = 1             // , j = j+i+1 = j + 1 + 1 = j + 2              // j increment 2             // hence, j < 4 means loop rotate 2 (j = 1 , j = 3) times              doors[j] = !doors[j];          }     } 

so,

   index(value of j) :    0       1       2       3                        +-----+ +-----+ +-----+ +-----+ after 2nd iteration  : |  t  | |  f  | |  t  | |  f  |                        +-----+ +-----+ +-----+ +-----+          door number :    1       2       3       4 

only 2 , 4 number doors closed! yes, on right track!

now understand in third iteration of outer loop, j start value 2 , increment 3, means door 3 toggled!

   index(value of j) :    0       1       2       3                        +-----+ +-----+ +-----+ +-----+ after 3rd iteration  : |  t  | |  f  | |  f  | |  f  |                        +-----+ +-----+ +-----+ +-----+          door number :    1       2       3       4 

hope understand how problem solved code 64 doors!

the final (4th) iteration makes door, looks like:

   index(value of j)   :    0       1       2       3                          +-----+ +-----+ +-----+ +-----+ after final iteration  : |  t  | |  f  | |  f  | |  t  |                          +-----+ +-----+ +-----+ +-----+          door number   :    1       2       3       4 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -