Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8272279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:04:58+00:00 2026-06-08T07:04:58+00:00

I want to write a really simple prime app. But i am having some

  • 0

I want to write a really simple prime app. But i am having some problems with the code below it should generate primes from 0 to 99. But instead it just shows 99.

One problem is that only the last number is displayed. But the other one is that the prime “check” doesn’t work. How can I fix those problems.

for (i=0; i<100; i++) {
    for (n=2; n<i; n++) {
        if (i%n == 0) break;
        else primetext.text = [NSString stringWithFormat:@"%i, ", i];
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T07:04:59+00:00Added an answer on June 8, 2026 at 7:04 am

    There were a number of problems, I’ll try to explain them

    • The problem with prime checking was that you had the if-else statement inside of the for loop that iterated through the numbers to check. So if your number wasn’t divisible by ANY of the numbers you check it would be registered as prime. It would also be registered n times, where n is the number of times it was found to not be divisible.
    • All even numbers are not prime (aside from 2), so you can do i+=2 to double your speed
    • You only need to check up to the square root of a number to see if it’s prime.
    • 1 isn’t a prime so you don’t need to include it (especially because it can make your logic more complex).
    • You were reassigning the string instead of adding a component to it
    • You were using %i instead of %d. This can sometimes cause weird errors.

    Here is your method revised with these points in mind.

    int maxNum = 100;
    primetext.text = @"2 ";             //Start knowing that 2 is the lowest prime (avoid evaluating evens)
    for (i=3; i<=maxNum; i+=2)          //Start at 3 and add 2 (avoid 1 and 2 as well as even numbers) 
    {
        bool isPrime = YES;             //Assume that i is prime
        for (n=2; n<sqrt(i); n++)       //Divide by every number up to square root
        {
            if (i%n == 0)               //If evenly divisible by n, not a prime
            {
                isPrime = NO;
                break;                  //Don't need to check any more factors, so break
            }
        }
        if (isPrime)                    //If isPrime was never set to NO, i is prime
        {
            NSString *temp = [NSString stringWithFormat:@"%d ", i];
            primetext.text = [primetext.text stringByAppendingString:temp];
        }
    }
    

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want write a simple query which will fetch data from a table (which
I want write a little code analyzer which parses nested structures and translates into
Hello I want write my own desktop sharing application in Java. The application should
I want to write a file on my iPhone app. My file is in
I've been working with jQuery for a while, but now I want to write
New to ASP.NET MVC 3. This seems like it should be a really simple
I want to write a simple program which can customize iphone icons like their
I really really want to write a GreaseMonkey extension. I have written a lot
Setup I am trying to write a really simple double entry accounting system for
I want to write a simple condition that states if a AND b are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.