Amazon.com Widgets Chapter 6 - Correction of Exercise 7
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 22, 2013, 05:54:33 PM
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
| |-+  Exercises
| | |-+  Chapter 6
| | | |-+  Chapter 6 - Correction of Exercise 7
Pages: [1]   Go Down
Print
Author Topic: Chapter 6 - Correction of Exercise 7  (Read 417 times)
microapple
Jr. Member
**
Posts: 83



WWW Email




« on: August 17, 2012, 02:38:36 PM »

Code: (Objective-C)
// Program to generate a table of prime numbers

#import <Foundation/Foundation.h>

int main (int argc, char * argv[])
{
    @autoreleasepool
    {
        int p, d, isPrime;
        

        NSLog (@"2"); // two in the only eve number so then we can skeep all eve numbers because they can be divided by 2
        
        // we will not check eve numbers because we know that only two is prime all other ever numbers
        // can be divided by two
        
        for ( p = 3; p <= 50; p+=2 )
        {
            isPrime = 1;
         
            for ( d = 3; d < p; d+=2 )
                
                if ( p % d == 0 )
                {
                    isPrime = 0;
                    // if we dicover that the number can be divided by other number
                    // why conitue ?
                    break ;
                }
            
                if ( isPrime != 0 )
                NSLog (@"%i ", p);
        }
    }
    return 0;
}
« Last Edit: August 21, 2012, 02:56:31 PM by microapple » Logged

EthanChu
Newbie
*
Posts: 5


Email




« Reply #1 on: February 05, 2013, 09:04:50 AM »

 Smiley
Break is not use for this kind application.
I think you can change the condition
 
for ( d = 3; d < p; d+=2 )   --> will still run
     if ( p % d == 0 ) 
                { 
                    isPrime = 0; 
 // if we dicover that the number can be divided by other number 
                    // why conitue ?   
                    break ; 
                } 
 
like below
 for ( d = 3; d < p && isPrime == 1; d+=2 )
 program will go next d+=2, after isPrime = 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.