Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Test {
- public static void main(String[] args) {
- final int LIMIT = 100; // how many bulbs
- boolean[] bulbs = new boolean[LIMIT]; // the bulbs
- // switch them all on
- for (int i = 0; i < LIMIT; i++) {
- bulbs[i] = true;
- }
- //the different men
- for (int man = 2; man < LIMIT + 1; man++) {
- for (int position = man; position < LIMIT; position += man) {
- bulbs[position] = !bulbs[position]; // flip bulb true <--> false
- }
- }
- //how many on now?
- int count = 0;
- for (int i = 0; i < LIMIT; i++) {
- if (bulbs[i]) {
- count++;
- }
- }
- System.out.println(count);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment