waltermilner

Untitled

Aug 29th, 2016
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.67 KB | None | 0 0
  1. public class Test {
  2.  
  3.  
  4.   public static void main(String[] args) {
  5.     final int LIMIT = 100; // how many bulbs
  6.     boolean[] bulbs = new boolean[LIMIT]; // the bulbs
  7.     // switch them all on
  8.     for (int i = 0; i < LIMIT; i++) {
  9.       bulbs[i] = true;
  10.     }
  11.     //the different men
  12.     for (int man = 2; man < LIMIT + 1; man++) {
  13.       for (int position = man; position < LIMIT; position += man) {
  14.         bulbs[position] = !bulbs[position]; // flip bulb true <--> false
  15.       }
  16.     }
  17.     //how many on now?
  18.     int count = 0;
  19.     for (int i = 0; i < LIMIT; i++) {
  20.       if (bulbs[i]) {
  21.         count++;
  22.       }
  23.     }
  24.     System.out.println(count);
  25.  
  26.   }
  27.  
  28. }
Advertisement
Add Comment
Please, Sign In to add comment