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

  • SEARCH
  • Home
  • 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 8473115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:15:35+00:00 2026-06-10T17:15:35+00:00

The following program determines prime variables. Values that return a remainder (p%d) for all

  • 0

The following program determines prime variables. Values that return a remainder (p%d) for all values of d greater than 1 and less than p set the value of isPrime equal to “1”.

- (int)someMethod
{
    @autoreleasepool {

        int isPrime;
        for (int p = 2; p <= 50; ++p) {
            isPrime = 1;

            for (int d = 2; d < p; ++d) {

                if ( p % d == 0)
                    isPrime = 0;

                if ( isPrime != 0)
                    NSLog (@"%i ", p);
            }
        }
    }

    return 0;
}

My question is, why is that the first “for” statement proceeds immediately after incrementing by “1” whereas in the 2nd “for” statement, the loop proceeds until the value of d is no longer less than p before moving on to the final “if” statement and NSLog.

If this question is not clear, for example, if the value of p is 14, the program proceeds as follows:

  1. p will increment by 1 to become 15
  2. the variable isPrime will be set to 1
  3. the value of d will be set to 2
  4. p%d will be calculated,
  5. the result will not be equal to 0, therefore the for statement will be skipped over
  6. THEN the value of d will, for some reason that I do not understand, increment to 3 and the loop will continue until d is equal to 15, INSTEAD of simply moving on to to the if statement and the NSLog, which would display “15”. Why is it that the second “for” loop continues to loop within the “if” statement, incrementing d, whereas the first loop increments p once and then moves on?
  • 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-10T17:15:36+00:00Added an answer on June 10, 2026 at 5:15 pm

    He understands for loops. It seems the other answers fail to understand the question. This loop does not correctly determine prime numbers at all. For instance, it prints out 15 as well as 49 as being prime, when they are not. Because it does printing in the inner loop it also prints out prime numbers multiple times. It SHOULD be printing out 15, and your logic is sound. I don’t know why it’s not for you, but it is for me. The correct implementation should be:

    - (int)someMethod {
        int isPrime;
        for (int p = 2; p <= 50; ++p) {
            isPrime = 1;
            for (int d = 2; d < p; ++d) {
                if ( p % d == 0)
                    isPrime = 0;
            }
            if (isPrime)
                NSLog (@"%i ", p);
        }
        return 0;
    }
    

    I’ll explain why it doesn’t work.

    To determine if a number is prime or not, this program should see if any number divides evenly into p. As soon as it finds one that does, the number is no longer prime, and it should set isPrime to 0. Once it’s checked every number less than p, and thus determined isPrime, it should check isPrime to see whether or not it should print the number out. However, it instead checks isPrime every time it checks to see if a number divides evenly into p. Since isPrime is set to 1 at the start for every value of p, any number not divisible by 2 gets printed out. It checks to see if 2 evenly divides, it doesn’t, it immediately checks isPrime which is 1, and immediately prints it out, as if it’s prime; but what if a number AFTER 2 divides evenly into that number? This is why 15 and 49 are printed as prime numbers, when they are composite.

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

Sidebar

Related Questions

The following program I'm creating will allow the user will input values to create
The following program: import multiprocessing,operator f = operator.itemgetter(0) # def f(*a): return operator.itemgetter(0)(*a) if
I recently submitted a small program for an assignment that had the following two
I've made a program for my OOP class that does the following: Defines a
I have the following program where two variables are to be passed by reference
The following program expects user input in the mixed fraction format 'whole_numbernumerator/denominator' and assigns
The following program, compiled with g++ 4.6, yields the error request for member ‘y’
the following program force quit and crashes, I don't understand why, import android.app.Activity; import
Consider following program: static void Main (string[] args) { int i; uint ui; i
In following program . I have one doubt. I have declared one global variable

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.