Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import <Foundation/Foundation.h>
- BOOL isPalindrome(NSInteger num);
- int main (int argc, const char * argv[])
- {
- NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
- NSInteger max = 0;
- for(NSInteger i = 100; i <= 999; i++) {
- for (NSInteger j = 100; j <= 999; j++) {
- NSInteger product = j*i;
- if( isPalindrome(product) == TRUE && max < product) {
- max = product;
- }
- }
- }
- NSLog(@"%ld is the largest palindrome", max);
- [pool drain];
- return 0;
- }
- BOOL isPalindrome(NSInteger num) {
- // turn the number into a string and check for palindromic activity
- NSString * string = [[NSString alloc] initWithFormat:@"%d", num];
- // check for palindrome
- NSInteger j = [string length] - 1;
- for(NSInteger i = 0; i != [string length]; i++) {
- if([string characterAtIndex:i] != [string characterAtIndex:j--])
- return false;
- }
- return true;
- }
Add Comment
Please, Sign In to add comment