How to resolve the algorithm 100 doors step by step in the Objective-C programming language
How to resolve the algorithm 100 doors step by step in the Objective-C programming language
Table of Contents
Problem Statement
There are 100 doors in a row that are all initially closed.
You make 100 passes by the doors.
The first time through, visit every door and toggle the door (if the door is closed, open it; if it is open, close it).
The second time, only visit every 2nd door (door #2, #4, #6, ...), and toggle it.
The third time, visit every 3rd door (door #3, #6, #9, ...), etc, until you only visit the 100th door.
Answer the question: what state are the doors in after the last pass? Which are open, which are closed?
Alternate:
As noted in this page's discussion page, the only doors that remain open are those whose numbers are perfect squares.
Opening only those doors is an optimization that may also be expressed;
however, as should be obvious, this defeats the intent of comparing implementations across programming languages.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm 100 doors step by step in the Objective-C programming language
Source code in the objective-c programming language
@import Foundation;
int main(int argc, const char * argv[]) {
@autoreleasepool {
// Create a mutable array
NSMutableArray *doorArray = [@[] mutableCopy];
// Fill the doorArray with 100 closed doors
for (NSInteger i = 0; i < 100; ++i) {
doorArray[i] = @NO;
}
// Do the 100 passes
for (NSInteger pass = 0; pass < 100; ++pass) {
for (NSInteger door = pass; door < 100; door += pass+1) {
doorArray[door] = [doorArray[door] isEqual: @YES] ? @NO : @YES;
}
}
// Print the results
[doorArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if ([obj isEqual: @YES]) {
NSLog(@"Door number %lu is open", idx + 1);
}
}];
}
}
@import Foundation;
#pragma mark - Classes
////////////////////////////////////////////////////
// Model class header - A we are using a category to add a method to MSMutableArray
@interface NSMutableArray (DoorModelExtension)
- (void)setNumberOfDoors:(NSUInteger)doors;
@end
// Model class implementation
@implementation NSMutableArray (DoorModelExtension)
- (void)setNumberOfDoors:(NSUInteger)doors {
// Fill the doorArray with 100 closed doors
for (NSInteger i = 0; i < doors; ++i) {
self[i] = @NO;
}
}
@end
////////////////////////////////////////////////////
// View class header - A simple class to handle printing our values
@interface DoorViewClass : NSObject
- (void)printResultsOfDoorTask:(NSMutableArray *)doors;
@end
// View class implementation
@implementation DoorViewClass
- (void)printResultsOfDoorTask:(NSMutableArray *)doors {
// Print the results, using an enumeration block for easy index tracking
[doors enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if ([obj isEqual: @YES]) {
NSLog(@"Door number %lu is open", idx + 1);
}
}];
}
@end
////////////////////////////////////////////////////
#pragma mark - main
// With our classes set we can use them from our controller, in this case main
int main(int argc, const char * argv[]) {
// Init our classes
NSMutableArray *doorArray = [NSMutableArray array];
DoorViewClass *doorView = [DoorViewClass new];
// Use our class category to add the doors
[doorArray setNumberOfDoors:100];
// Do the 100 passes
for (NSUInteger pass = 0; pass < 100; ++pass) {
for (NSUInteger door = pass; door < 100; door += pass+1) {
doorArray[door] = [doorArray[door] isEqual: @YES] ? @NO : @YES;
}
}
// Print the results
[doorView printResultsOfDoorTask:doorArray];
}
You may also check:How to resolve the algorithm String case step by step in the Jsish programming language
You may also check:How to resolve the algorithm Quine step by step in the MUMPS programming language
You may also check:How to resolve the algorithm File modification time step by step in the Objective-C programming language
You may also check:How to resolve the algorithm Loops/Do-while step by step in the Dyalect programming language
You may also check:How to resolve the algorithm Averages/Median step by step in the NewLISP programming language