Guest User

Untitled

a guest
Jan 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. #import <Foundation/Foundation.h>
  2.  
  3. BOOL isPalindrome(NSInteger num);
  4.  
  5. int main (int argc, const char * argv[])
  6. {
  7.  
  8. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  9.  
  10. NSInteger max = 0;
  11. for(NSInteger i = 100; i <= 999; i++) {
  12. for (NSInteger j = 100; j <= 999; j++) {
  13. NSInteger product = j*i;
  14. if( isPalindrome(product) == TRUE && max < product) {
  15. max = product;
  16. }
  17. }
  18. }
  19.  
  20. NSLog(@"%ld is the largest palindrome", max);
  21.  
  22. [pool drain];
  23. return 0;
  24. }
  25.  
  26. BOOL isPalindrome(NSInteger num) {
  27.  
  28. // turn the number into a string and check for palindromic activity
  29. NSString * string = [[NSString alloc] initWithFormat:@"%d", num];
  30.  
  31. // check for palindrome
  32. NSInteger j = [string length] - 1;
  33. for(NSInteger i = 0; i != [string length]; i++) {
  34. if([string characterAtIndex:i] != [string characterAtIndex:j--])
  35. return false;
  36. }
  37.  
  38.  
  39. return true;
  40. }
Add Comment
Please, Sign In to add comment