Welcome, Guest. Please login or register.
Did you miss your activation email?
March 23, 2017, 07:13:46 AM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Programming in Objective-C, 4th edition
| |-+ Chapter 6
| | |-+ my answer of Exercise 7
Pages: [1] Go Down
Print
Author Topic: my answer of Exercise 7 (Read 2290 times)
Neo
Newbie
*
Posts: 2


on: June 29, 2015, 06:56:36 AM

#import <Foundation/Foundation.h> 
 
int main(int argc, char * argv[]) 

    @autoreleasepool { 
        int p, d; 
        BOOL isPrime; 
         
        for (p = 2; p <= 50; ++p) { 
            if (p == 2 || p % 2 != 0)  //skip the even number,
            { 
                isPrime = YES; 
                 
                for (d = 2; d < p ; ++p)
               { 
                    if(p % d == 0) 
                    { 
                       isPrime = NO;   
                    } 
                    if(isPrime == NO)
                    {
                       d = p;        //If p%d already equal to 0,there is no need to calculator d+1 and so on
                    }
                 
                   if (isPrime)
                  { 
                    NSLog(@"%i", i); 
                  } 
            } 
        } 
    } 
     
    return 0; 


I didn't use "break",because "break" only can jump out of the current loop(the if loop,not the for loop).So when p%d equal to 0,I set d=p,then program will jump out the "for" loop(the condition:d<p).It will not calculator numbers after d,make this "for" loop more efficient.

Logged
Neo
Newbie
*
Posts: 2


Reply #1 on: June 29, 2015, 06:58:34 AM

#import <Foundation/Foundation.h> 
 
int main(int argc, char * argv[]) 

    @autoreleasepool { 
        int p, d; 
        BOOL isPrime; 
         
        for (p = 2; p <= 50; ++p) { 
            if (p == 2 || p % 2 != 0)  //skip the even number,
            { 
                isPrime = YES; 
                 
                for (d = 2; d < p ; ++p)
               { 
                    if(p % d == 0) 
                    { 
                       isPrime = NO;   
                    } 
                    if(isPrime == NO)
                    {
                       d = p;        //If p%d already equal to 0,there is no need to calculator d+1 and so on
                    }
                 
                   if (isPrime)
                  { 
                    NSLog(@"%i", d); 
                  } 
            } 
        } 
    } 
     
    return 0; 


I didn't use "break",because "break" only can jump out of the current loop(the if loop,not the for loop).So when p%d equal to 0,I set d=p,then program will jump out the "for" loop(the condition:d<p).It will not calculator numbers after d,make this "for" loop more efficient.


Logged
BrianLawson
Sr. Member
****
Posts: 262


Email
Reply #2 on: June 29, 2015, 11:08:01 AM

There is no such thing as an "if loop". Putting a break statement inside the if will break out of the for loop which makes the loop more efficient since it stops looking for the prime once it has been found.
Logged
djt
Newbie
*
Posts: 7



Email
Reply #3 on: July 05, 2015, 07:37:55 PM

Just a comment or two:

This line

Code: (Objective-C)
for (d = 2; d < p ; ++p) 

should perhaps read

Code: (Objective-C)
for (d = 2; d < p ; ++d) 

Also, it's probably not a good idea to change the value of a loop index variable inside of the loop.

Lastly -- in case the answer to this question is not available elsewhere -- this same exercise is in the book "Programming in C, 3rd edition" and the solution can be found on this Forum.

From the Home page of the Forum scroll down and locate "Programming in C, 3rd ed." Then select "Answers to Odd-Numbered Exercises." Next select "Answers in this post." Then scroll down to the answer to exercise 6-7.

The program is in C rather than Objective-C but it's essentially the same.

djt


Last Edit: July 05, 2015, 08:41:53 PM by djt Logged
cary_W
Newbie
*
Posts: 10


Email
Reply #4 on: June 10, 2016, 12:10:30 AM

Code: (Objective-C)
//
//  main.m
//  c6_e7
//
//  Created by cw on 5/5/16.
//  Copyright 2016 Breeze. All rights reserved.
//
// Program to generate a table of prime numbers
// second version using BOOL type and predefined values
#import <Foundation/Foundation.h>
int main (int argc, char * argv[])
{
    @autoreleasepool {
        int p, d;
        BOOL isPrime,evenNumber;
        //Print prime 1 and 2
        NSLog(@"%i\n/%i",1,2);
        for ( p = 3; p <= 50; ++p)
        {
            isPrime = YES;
            evenNumber = NO;
            //check if it's an even number
            //if is, it's not an prime
            if(!(p % 2))
            {
                evenNumber = YES;
                isPrime = NO;
            }
            //if it's an even number, skip this loop
            for ( d = 2; d < p && !evenNumber; ++d)
            {
                //if find one divisor then it's not a prime
                if ( p % d == 0 )
                {
                    isPrime = NO;
                    break;
                }
            }
           
            if ( isPrime == YES )
            NSLog (@"%i ",p);
           
        }
    }
    return 0;
}
Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.