01-01-2011, 10:23 PM
#1 (permalink )
Indie Developer
Join Date: Jul 2010
Posts: 1,346
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 .
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
Quote:
Originally Posted by
iSDK
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.
01-02-2011, 02:59 PM
#3 (permalink )
Indie Developer
Join Date: Jul 2010
Posts: 1,346
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
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.
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
Quote:
Originally Posted by
iSDK
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.
01-16-2011, 07:28 PM
#5 (permalink )
Indie Developer
Join Date: Jul 2010
Posts: 1,346
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
The variable "p" in your for loop is never declared. My guess is that it should be the variable "primenumber" instead.
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Online Users: 448
21 members and 427 guests
cgokey , dcool , Dj_kades , givensur , iekei , IOOIOIO , IphoneSdk , ipodphone , jeroenkeij , john love , karatebasker , mediaspree , patapple , peterwilli , pipposanta , Punkjumper , QuantumDoja , SLIC , Sloshmonster , sojourner , usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,696
Threads: 94,139
Posts: 402,961
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jasper_muc