Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Tutorials > Tutorial Discussion

Reply
 
LinkBack Thread Tools Display Modes
Old 01-01-2011, 10:23 PM   #1 (permalink)
Indie Developer
 
iSDK's Avatar
 
Join Date: Jul 2010
Posts: 1,346
iSDK is on a distinguished road
Send a message via AIM to iSDK
Default Snippet: Prime Number Generator

Hi,

I promised one of my friends I would make this for him, as he spent a long time creating a python algorithm to work it out and then realised python cannot run on iPhones!

Please do not take this code, and make a Prime Number generating application.


So here is the code:

Finder.h:

Code:
- (int)getPrimeNumberofLength:(int)length;
Finder.m

Code:
- (int)getPrimeNumberofLength:(int)length {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	int primenumber, d;
    BOOL isPrime;
    
    for (primenumber = 2; primenumber <= length; ++primenumber)
    {
        isPrime = YES;
        
        for (d = 2; d < primenumber; ++d)
        {
            if (primenumber % d == 0)
                isPrime = NO;
        }
		
        if (isPrime)
			
		NSLog(@"%i", primenumber);
    }
    
    [pool drain];
    return 0;
}

Please do not take this code, and make a Prime Number generating application.

Last edited by iSDK; 01-02-2011 at 05:11 PM.
iSDK is offline   Reply With Quote
Old 01-02-2011, 10:53 AM   #2 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,176
Kalimba is on a distinguished road
Default

Quote:
Originally Posted by iSDK View Post
Code:
- (int)getPrimeNumberofLength:(int)length {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	int primenumber, d;
    BOOL isPrime;
    
    for (p = 2; p <= length; ++p)
    {
        isPrime = YES;
        
        for (d = 2; d < primenumber; ++d)
        {
            if (primenumber % d == 0)
                isPrime = NO;
        }
		
        if (isPrime)
			
		NSLog(@"%i", primenumber);
    }
    
    [pool drain];
    return 0;
}
[/BOLD]
This code won't compile as it is. Also, there some a couple minor optimizations that can be made to make the code run faster.
__________________
~~ Word Flurry ~~ App Store / Website / Facebook
Kalimba is offline   Reply With Quote
Old 01-02-2011, 02:59 PM   #3 (permalink)
Indie Developer
 
iSDK's Avatar
 
Join Date: Jul 2010
Posts: 1,346
iSDK is on a distinguished road
Send a message via AIM to iSDK
Default

Hey, Thanks for the feedback. Its wierd, as it works for me and my friend. What seems to be the reason why it wont compile?

Thanks again.

Quote:
Originally Posted by Kalimba View Post
This code won't compile as it is. Also, there some a couple minor optimizations that can be made to make the code run faster.
iSDK is offline   Reply With Quote
Old 01-02-2011, 03:34 PM   #4 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,176
Kalimba is on a distinguished road
Default

Quote:
Originally Posted by iSDK View Post
Hey, Thanks for the feedback. Its wierd, as it works for me and my friend. What seems to be the reason why it wont compile?

Thanks again.
The variable "p" in your for loop is never declared. My guess is that it should be the variable "primenumber" instead.
__________________
~~ Word Flurry ~~ App Store / Website / Facebook
Kalimba is offline   Reply With Quote
Old 01-16-2011, 07:28 PM   #5 (permalink)
Indie Developer
 
iSDK's Avatar
 
Join Date: Jul 2010
Posts: 1,346
iSDK is on a distinguished road
Send a message via AIM to iSDK
Default

Yup, thats correct. I had previously edited the code, and that line had obviously escaped my eyes... The code is now fixed, and working.

Quote:
Originally Posted by Kalimba View Post
The variable "p" in your for loop is never declared. My guess is that it should be the variable "primenumber" instead.
iSDK is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Stats
Members: 175,696
Threads: 94,139
Posts: 402,961
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jasper_muc
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:56 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0