There is a line of lockers numbered 1 to 1024, initially all closed.
A man walks down the line, opens 1, then alternately skips and opens each closed locker (so he opens 1, 3, 5, ... , 1023). At the end of the line he walks back, opens the first closed locker, then alternately skips and opens each closed locker (so he opens 1024, skips 1022 and so on).
He continues to walk up and down the line until all the lockers are open.
Which locker is opened last?
The first few lines scrolled off the Command Prompt window, but those belong to the uninteresting every-odd-number set. What's left are:
949 951 953 955 957 959 961 963 965 967 969 971 973 975 977 979
981 983 985 987 989 991 993 995 997 999 1001 1003 1005 1007 1009
1011 1013 1015 1017 1019 1021 1023
Back:
1024 1020 1016 1012 1008 1004 1000 996 992 988 984 980 976 972
968 964 960 956 952 948 944 940 936 932 928 924 920 916 912 908
904 900 896 892 888 884 880 876 872 868 864 860 856 852 848 844
840 836 832 828 824 820 816 812 808 804 800 796 792 788 784 780
776 772 768 764 760 756 752 748 744 740 736 732 728 724 720 716
712 708 704 700 696 692 688 684 680 676 672 668 664 660 656 652
648 644 640 636 632 628 624 620 616 612 608 604 600 596 592 588
584 580 576 572 568 564 560 556 552 548 544 540 536 532 528 524
520 516 512 508 504 500 496 492 488 484 480 476 472 468 464 460
456 452 448 444 440 436 432 428 424 420 416 412 408 404 400 396
392 388 384 380 376 372 368 364 360 356 352 348 344 340 336 332
328 324 320 316 312 308 304 300 296 292 288 284 280 276 272 268
264 260 256 252 248 244 240 236 232 228 224 220 216 212 208 204
200 196 192 188 184 180 176 172 168 164 160 156 152 148 144 140
136 132 128 124 120 116 112 108 104 100 96 92 88 84 80 76 72
68 64 60 56 52 48 44 40 36 32 28 24 20 16 12 8 4
Forward:
2 10 18 26 34 42 50 58 66 74 82 90 98 106 114 122 130 138
146 154 162 170 178 186 194 202 210 218 226 234 242 250 258 266
274 282 290 298 306 314 322 330 338 346 354 362 370 378 386 394
402 410 418 426 434 442 450 458 466 474 482 490 498 506 514 522
530 538 546 554 562 570 578 586 594 602 610 618 626 634 642 650
658 666 674 682 690 698 706 714 722 730 738 746 754 762 770 778
786 794 802 810 818 826 834 842 850 858 866 874 882 890 898 906
914 922 930 938 946 954 962 970 978 986 994 1002 1010 1018
Back:
1022 1006 990 974 958 942 926 910 894 878 862 846 830 814 798
782 766 750 734 718 702 686 670 654 638 622 606 590 574 558 542
526 510 494 478 462 446 430 414 398 382 366 350 334 318 302 286
270 254 238 222 206 190 174 158 142 126 110 94 78 62 46 30 14
Forward:
6 38 70 102 134 166 198 230 262 294 326 358 390 422 454 486
518 550 582 614 646 678 710 742 774 806 838 870 902 934 966 998
Back:
1014 950 886 822 758 694 630 566 502 438 374 310 246 182 118 54
Forward:
22 150 278 406 534 662 790 918
Back:
982 726 470 214
Forward:
86 598
Back:
854
Forward:
342
showing the last opened to be 342 while going in the forward direction.
DIM locker(1024)
CLS
DO
PRINT : PRINT "Forward:"
doNext = 1
FOR i = 1 TO 1024
IF locker(i) = 0 THEN
IF doNext THEN locker(i) = 1: ct = ct + 1: PRINT i;
doNext = 1 - doNext
END IF
NEXT
IF ct = 1024 THEN EXIT DO
PRINT : PRINT "Back:"
doNext = 1
FOR i = 1024 TO 1 STEP -1
IF locker(i) = 0 THEN
IF doNext THEN locker(i) = 1: ct = ct + 1: PRINT i;
doNext = 1 - doNext
END IF
NEXT
LOOP UNTIL ct = 1024
BTW, in binary, 342 is 101010110, to complete Gamer's idea.
|
Posted by Charlie
on 2008-12-21 12:38:01 |